OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.29k stars 6.44k forks source link

[BUG] "default: null" is considered to be an error even with "nullable: true" #16775

Open gturri opened 11 months ago

gturri commented 11 months ago

Bug Report Checklist

Description

OpenApiGenerator CLI considers default: null is not correct for an attribute even if it has nullable: true

openapi-generator version

OpenApiGenerator CLI 7.0.1

OpenAPI declaration file content or url
openapi: 3.0.3
info:
 title: test
 version: v0.1
 description: test
paths:
 /api/path:
  get:
   responses:
    '200':
      description: ok
      content:
       application/json:
        schema:
         $ref: '#/components/schemas/MyEntity'
components:
 schemas:
  MyEntity:
   type: object
   properties:
    someField:
     type: array
     items: 
       type: string
     nullable: true
     default: null
Generation Details
Steps to reproduce

java -jar openapi-generator-cli-7.0.1.jar generate --generator-name csharp --input-spec my_spec.yaml --output out_dir

(nb: I'm writing --generator-name csharp because I have to pick a generator name in order to have a valid command, but I'm observing the issue with every generator)

 | Error count: 1, Warning count: 0                                           
Errors:
        -attribute components.schemas.MyEntity.default is not of type `array` 

Note that I would expect this command to work because:

default – the default value must conform to the specified schema.

and that value (null) conforms the schema because it has nullable: true

Related issues/PRs
Suggest a fix
imba28 commented 4 months ago

I can confirm this bug is still present in v7.5.0. However, based on a few tests it seems to only affect nullable properties of type array.

When running openapi-generator-ci validate on

openapi: 3.0.0
info:
  title: test
  version: 1.0.0
  description: test
paths:
  /foo:
    get:
      responses:
        '200':
          description: foo
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Entity'
components:
  schemas:
    Entity:
      properties:
        array:
          type: array
          nullable: true
          items:
            type: string
          default: null
        date:
          type: string
          format: date-time
          nullable: true
          default: null
      type: object

the tool only complains about the default value of array while (rightfully) considering date as valid:

Errors:
        - attribute components.schemas.Entity.default is not of type `array`

[error] Spec has 1 errors.
renepupil commented 1 month ago

However, based on a few tests it seems to only affect nullable properties of type array.

I can confirm this to happen with type object as well, but probably not scalar types.