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.46k stars 6.49k forks source link

`because "schema" is null` error if schema includes `type: "null"` #15496

Open carlosli888 opened 1 year ago

carlosli888 commented 1 year ago
Description

I am working on an OpenAPI spec that involves the use of GeoJSON entities. One of these entities, (Feature) has a required property that can be null. I have checked against the JSON schema, and this is a valid type (see https://json-schema.org/understanding-json-schema/reference/type.html). However, when I try to generate the docs, I get the following error:

[main] WARN  io.swagger.v3.parser.OpenAPIV3Parser - Exception while resolving:
java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Schema.get$ref()" because "schema" is null
        at io.swagger.v3.parser.processors.SchemaProcessor.processComposedSchema(SchemaProcessor.java:162)
        at io.swagger.v3.parser.processors.SchemaProcessor.processSchemaType(SchemaProcessor.java:73)
        at io.swagger.v3.parser.processors.SchemaProcessor.processPropertySchema(SchemaProcessor.java:140)
        at io.swagger.v3.parser.processors.SchemaProcessor.processSchemaType(SchemaProcessor.java:77)
        at io.swagger.v3.parser.processors.SchemaProcessor.processSchema(SchemaProcessor.java:62)
        at io.swagger.v3.parser.processors.ComponentsProcessor.processSchemas(ComponentsProcessor.java:231)
        at io.swagger.v3.parser.processors.ComponentsProcessor.processComponents(ComponentsProcessor.java:145)
        at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:73)
        at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:59)
        at io.swagger.v3.parser.OpenAPIV3Parser.resolve(OpenAPIV3Parser.java:226)
        at io.swagger.v3.parser.OpenAPIV3Parser.readContents(OpenAPIV3Parser.java:178)
        at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:94)
        at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)
        at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:589)
        at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:647)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:479)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
[error] There is an error with OpenAPI specification parsed from the input spec file: .\openapi.yaml
[error] Please make sure the spec file has correct format and all required fields are populated with valid value.
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: 0
Errors:
        -Cannot invoke "io.swagger.v3.oas.models.media.Schema.get$ref()" because "schema" is null

        at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:620)
        at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:647)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:479)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)

I get a similar issue when validating the schema.

openapi-generator version

6.6.0

OpenAPI declaration file content or url
openapi: 3.0.2  
info:
  title: API Test
  version: 0.1.0
servers: 
  - url: 'https://localhost:3000/api/v1'

paths:
  /test:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: https://geojson.org/schema/Feature.json
Command line used for generation
openapi-generator-cli generate -g html2 -o ../docs/api -i .\openapi.yaml --generate-alias-as-model
Steps to reproduce
  1. Create a file called openapi.yaml.
  2. Place the YAML content above in the file.
  3. Run the command line above.
  4. After executing the command, I get the reported error.
Related issues/PRs
Suggest a fix/enhancement

I noticed that if I replace "type": "null" in the schema with something else, it works. The schema for a GeoJSON feature has two properties where this exists:

However, the above steps would mean that I am changing the schema and restricting what is valid. As such, I am looking to see if we can have a fix on the tool instead to support this scenario.

AhmedAbdelfaheem commented 11 months ago

I got the same problem with multiple versions as well did you come up with a better solution?

carlosli888 commented 11 months ago

@AhmedAbdelfaheem, Unfortunately, no, I did not come up with a better solution using this tool. What I did do was to explore other tools. I can't remember, but I think I used https://github.com/Redocly/redoc or something similar instead.

lrk commented 3 months ago

@carlosli888 FYI openapi3 specs states there is no "null" type in openapi line in json schema -> https://swagger.io/docs/specification/data-models/data-types/ see "null" paragraph.

I'm not sure if the openapi schema parser convert json schema to valid openapi schema, nor if you can use json schema directly as openapi schema, but i ran into a similar issue and i solved it with nullable: true.

In your case, as you reference remote schema, you might not be able to do that directly.

TPunkiee commented 1 month ago

Similar situation when any other type is present (example "obect" iso "object"). A graceful failure indicating what violated the spec might be preferred.

java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Schema.addProperties(String, io.swagger.v3.oas.models.media.Schema)" because "schema" is null
    at io.swagger.v3.parser.converter.SwaggerConverter.lambda$addProperties$11(SwaggerConverter.java:1268)
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:721)
    at io.swagger.v3.parser.converter.SwaggerConverter.addProperties(SwaggerConverter.java:1267)
    at io.swagger.v3.parser.converter.SwaggerConverter.convert(SwaggerConverter.java:1225)
    at io.swagger.v3.parser.converter.SwaggerConverter.convert(SwaggerConverter.java:262)
    at io.swagger.v3.parser.converter.SwaggerConverter.readResult(SwaggerConverter.java:104)
    at io.swagger.v3.parser.converter.SwaggerConverter.readLocation(SwaggerConverter.java:85)
wing328 commented 1 month ago

@TPunkiee can you please share the spec to reproduce the issue?

TPunkiee commented 1 month ago

The spec is kinda long and is valid when : "object" is used instead. I did cut down the spec to this piece that still triggers the exception:

{"swagger" : "2.0",
  "definitions" : {
    "ListWrapperWAdreslijstLI" : {
      "properties" : {
        "disclaimer" : {
          "type" : "string"
        }
      },
      "type" : "obect"
    }
  }
}

It was used with the swaggerparser 2.1.22, jackson 2.16.2, openJDK 18.0.2 SwaggerParseResult result = new OpenAPIParser().readLocation(filePath, null, parseOptions); The online validator just returns no messages which isn't a great help as you have no clue if something went wrong or not.