yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
626 stars 146 forks source link

Nullable references not being rendered as optional #215

Closed alephao closed 4 years ago

alephao commented 4 years ago

Given the following components:

components:
  schemas:
    ObjectHoldingNullableReference:
      type: object
      properties:
        this_property_should_be_optional:
          $ref: "#/components/schemas/SomeNullableObject"
      required:
        - this_property_should_be_optional
    SomeNullableObject:
      type: object
      required:
        - value
      properties:
        value:
          type: string
      nullable: true

I would expect ObjectHoldingNullableReference to have an optional thisPropertyShouldBeOptional like the example below:

//
// ObjectHoldingNullableReference.swift
//

public class ObjectHoldingNullableReference: APIModel {
    public var thisPropertyShouldBeOptional: SomeNullableObject?
    ...
}

But SwagGen currently generates a non-optional property.

Apparently, the property.schema.metadata does not contain the correct information if schema.type is a reference, and the correct metadata is wrapped inside schema.type.

https://github.com/yonaskolb/SwagGen/blob/a8e170cb045e33e99cfd2d0c578c1db62387d9b0/Sources/SwagGenKit/SwiftFormatter.swift#L228

ymhuang0808 commented 3 years ago

Hi, I'm confusing with the issue and the merge request.

Why would @alephao expect that the this_property_should_be_optional property in ObjectHoldingNullableReference class should be optional in the codegen result? The this_property_should_be_optional should be removed from required in OpenAPI definition.

alephao commented 3 years ago

Hi @ymhuang0808, it's because of the nullable: true property in SomeNullableObject. I agree it's a bit weird that the object is defining whether itself can be null or not, but that's also part of the specification.