swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

`nullable: true` ignored if `type: object` and a property has `nullable: true`

Open Jackman3005 opened this issue 2 years ago • 4 comments

What's wrong?

Hello again. I have a situation where I had an object with nullable: true and all was working well, then I needed to add some properties that had nullable: true and for some reason the | null on the main object was removed from the types.

This Schema:

MyDomainObjectSchema:
  required:
    - user
  properties:
    user:
      type: object
      nullable: true
      required:
        - id
        - displayName
        - email
      properties:
        id:
          type: string
        displayName:
          type: string
        email:
          type: string
          nullable: true

Produces the following incorrect type:

export interface MyDomainObjectSchema {
  user: {
    id: string;
    displayName: string;
    email: string | null;
  }; // where is the union with `null`?
}

What works

If there are no properties that are nullable then the parent object is correctly typed as shown below: This Schema:

MyDomainObjectSchema:
  required:
    - user
  properties:
    user:
      type: object
      nullable: true
      required:
        - id
        - displayName
        - email
      properties:
        id:
          type: string
        displayName:
          type: string
        email:
          type: string
          # removing `nullable: true` here fixes the incorrect behavior
          # nullable: true 

Produces the following correct result:

export interface MyDomainObjectSchema {
  user: {
    id: string;
    displayName: string;
    email: string;
  } | null;
}

What is expected

I expect that nullable: true will always apply to an object schema regardless of the properties and their conditions. I expect the original YAML schema provided would produce the following type:

export interface MyDomainObjectSchema {
  user: {
    id: string;
    displayName: string;
    email: string | null;
  } | null;
}

Thanks for the hard work on this. We're still happy to have it to work with :)

Jackman3005 avatar May 23 '23 04:05 Jackman3005

Experiencing the same

akosradler avatar Aug 03 '23 14:08 akosradler

Still struggling with this as I add new schemas and the | null union is not added just because some properties of the object are nullable: true... We have a "workaround" but it's just darn ugly!

# Replace any properties with `nullable: true` on the object with an `anyOf` 
# that includes the type you want and a nullable version of that type.
email:
  anyOf:
    - $ref: "schemas.yaml#/NullableString"
    - type: string
    
NullableString:
  nullable: true
  type: string

Jackman3005 avatar Aug 21 '23 03:08 Jackman3005

@js2me any thoughts on why this might be happening and what should be done to resolve it?

Jackman3005 avatar Aug 21 '23 03:08 Jackman3005

Is there a workaround for this? @smorimoto maybe?

cars10 avatar Aug 09 '24 08:08 cars10