openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open gturri opened this issue 2 years ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
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)

  • Expected behavior: the command runs successfully (ie: it generates code and exits with 0 status code)
  • Actual behavior: the command exits with 1 status code and display the error
 | 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:

  • the tool https://apidevtools.org/swagger-parser/online/ considers my spec is ok
  • according to the OAS spec :

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
  • https://github.com/OpenAPITools/openapi-generator/issues/16633 is related to this issue but is not really the same
  • other issues found with search is:issue default null seems completely unrelated
Suggest a fix

gturri avatar Oct 10 '23 10:10 gturri

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.

imba28 avatar May 02 '24 15:05 imba28

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.

renepupil avatar Jul 30 '24 17:07 renepupil