yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nullable references not being rendered as optional

alephao opened this issue · comments

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.

context["optionalType"] = type + (property.required && !property.schema.metadata.nullable ? "" : "?")

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.

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.

ezoic increase your site revenue