swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
788 stars 531 forks source link

nullable arrays with a default null throws an error #2125

Open yannickl88 opened 1 month ago

yannickl88 commented 1 month ago

We have a schema which can either be null or a list of object. This is mainly to allow the parameter to be optionally passed and to detect if the property was set to do case distinction on. So passing null vs [] behaves differently. (Similarly, as passing and empty string vs null)

However, when encoding this in a spec and trying to generate an typescript-fetch client for this, it results in an error.

Spec I tried:

openapi: '3.0.3'

info:
  title: Test
  version: 0.1.0

paths: {}

components:
  schemas:
    Test:
      type: array
      items:
        type: string
      default: null
      nullable: true

When running this through the OpenAPI generator, the swagger validation seems to fail.

$ java -jar "/node_modules/@openapitools/openapi-generator-cli/versions/7.9.0.jar" generate --input-spec="/test_schema.yaml" --generator-name="typescript-fetch" --output="/tmp/" --additional-properties="withInterfaces=true"
Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 1, Warning count: 3
Errors: 
     -attribute components.schemas.PaymentConfigurationResponse.default is not of type `array`
Warnings: 
     -attribute components.schemas.PaymentConfigurationResponse.default is not of type `array`

     at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:717)
     at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:744)
     at org.openapitools.codegen.cmd.Generate.execute(Generate.java:527)
     at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
     at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)

My expectation is that this would be valid. I expect null defaults are allowed when nullable, regardless of the type (array in this case).

PS: I initially reported this at OpenAPITools/openapi-generator, https://github.com/OpenAPITools/openapi-generator/issues/19825

GabeK0 commented 3 days ago

I've encountered the same thing, would love to get a fix