swagger-api / swagger-api/swagger-codegen

[Swift4] Schema with inner objects with required properties are marked optional

Open
#9,106 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

See examples below

Swagger-codegen version

3.0.3

Swagger declaration file content or url
    Person:
      required:
        - name
      type: object
      properties: 
        name:
          type: object
          required:
            - first
            - last
          properties:
            first: 
              type: string
            last:
              type: string

Generates:

public struct Person: Codable {


    public var name: PersonName
    public init(name: PersonName) { 
        self.name = name
    }

}

public struct PersonName: Codable {


    public var first: String?

    public var last: String?
    public init(first: String?, last: String?) { 
        self.first = first
        self.last = last
    }

}

It's unexpected that first and last name are optional since they are marked required. On the other hand, using refs it works:

    Person:
      required:
        - name
      type: object
      properties: 
        name:
          $ref: '#/components/schemas/Name'
    Name:
      required:
        - first
        - last
      type: object
      properties:
        first: 
          type: string
        last:
          type: string

Generates:

public struct Person: Codable {


    public var name: Name
    public init(name: Name) { 
        self.name = name
    }

}

public struct Name: Codable {


    public var first: String

    public var last: String
    public init(first: String, last: String) { 
        self.first = first
        self.last = last
    }

}
Command line used for generation
swagger-codegen generate -i test.yaml -l swift4 -Dmodels

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Swift4 model generation entry point used by swagger-codegen generate -i test.yaml -l swift4 -Dmodels, comparing inline nested objects with equivalent $ref schemas. Done means required nested properties such as first and last are generated as non-optional Swift properties in the inline case as well.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.