openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Unable to specify null value

Open justrag opened this issue 3 years ago • 0 comments

I have this case where I need to specify two fields where exactly one is given a value and the other is null (not omitted, but exactly null): {fieldA: number, fieldB; null} | {fieldA: null, fieldB: string} I can't seem to get it working.

              oneOf:
                - type: object
                  properties:
                    fieldA:
                      type: integer
                      minimum: 0
                    fieldB:
                      type: string
                      nullable: true
                      enum:
                        - null
                  required:
                    - fieldA
                    - fieldB
                - type: object
                  properties:
                    fieldA:
                      type: string
                      nullable: true
                      enum:
                        - null
                    fieldB:
                      type: string
                  required:
                    - fieldA
                    - fieldB

seems like should be the way to go, but the generator translates this into: {fieldA: number, fieldB: string | null} | {fieldA: string | null, fieldB: string}

Is this the generator's error or is my spec not doing what I think it does? I suspect a bug, as

              oneOf:
                - type: object
                  properties:
                    fieldA:
                      type: integer
                      minimum: 0
                    fieldB:
                      type: string
                      nullable: true
                      enum:
                        - dummy
                        - null
                  required:
                    - fieldA
                    - fieldB
                - type: object
                  properties:
                    fieldA:
                      type: string
                      nullable: true
                      enum:
                        - dummy
                        - null
                    fieldB:
                      type: string
                  required:
                    - fieldA
                    - fieldB

gives {fieldA: number, fieldB: 'dummy' | null} | {fieldA: 'dummy' | null, fieldB: string}

justrag avatar Feb 09 '22 08:02 justrag