OpenAPITools / OpenAPITools/openapi-generator

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

Open
#15,496 6 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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:

  • properties ~line 31
  • geometry ~line 41

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the supplied openapi.yaml and the GeoJSON Feature.json schema, then start at SchemaProcessor.processComposedSchema and follow the calls from OpenAPIResolver. Check how properties with type: "null" are processed during validation and generation. Done means the specification validates and the html2 generation completes without the reported NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.