OpenAPITools / OpenAPITools/openapi-generator

[BUG] validate crashes with a NullReferenceException for schema errors in another file

Open
#10,793 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

When "outsourcing" your data structure into a seperate yaml file and referncing a schema from it, having unexpected attribute paths will lead to validate crashing with a null pointer exception.
Making the same error in one file (i.e. not using $ref produces a readable error)

Specifically, someone in my team misspelled properties as propertiers in a file with around 1.5k lines during a major update. This took a solid day to find...

openapi-generator version
$ java --version
openjdk 11.0.12 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2)
OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2, mixed mode, sharing)


$ java -jar ./openapi-generator-cli.jar --version
openapi-generator-cli 5.2.1
  commit : 8403e59
  built  : 2021-08-16T12:52:51Z
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url

api_standalone.yaml

openapi: 3.0.1
info:
  description: test api
  title: Test Api
  version: 1.0.0

paths:
  /hello:
    post:
      description: test function
      requestBody:
        description: Dummy API for generating Ecm Technique
        content:
          application/json:
            schema:
              description: A simple data structure
              propertiers:
                id:
                  description: a unique number
                  type: integer
                  format: int32
                name:
                  description: a name
                  type: string
      responses:
        default:
          description: no reply
          content: {}

api_with_ref.yaml

openapi: 3.0.1
info:
  description: test api
  title: Test Api
  version: 1.0.0

paths:
  /hello:
    post:
      description: test function
      requestBody:
        description: Dummy API for generating Ecm Technique
        content:
          application/json:
            schema:
              $ref: schemas.yaml#/MySchema
      responses:
        default:
          description: no reply
          content: {}

schemas.yaml

MySchema:
  description: A simple data structure
  propertiers:
    id:
      description: a unique number
      type: integer
      format: int32
    name:
      description: a name
      type: string
Generation Details

N/A

Steps to reproduce

What I expect from validate:

$ java -jar ./openapi-generator-cli.jar validate -i api_standalone.yaml
Validating spec (api_standalone.yaml)
Errors:
        - attribute paths.'/hello'(post).requestBody.content.'application/json'.schema.propertiers
          is unexpected

[error] Spec has 1 errors.

What I get when using the reference.

$ java -jar ./openapi-generator-cli.jar validate -i api_with_ref.yaml
Validating spec (api_with_ref.yaml)
[main] WARN  io.swagger.v3.parser.OpenAPIV3Parser - Exception while resolving:
java.lang.NullPointerException: null
        at io.swagger.v3.parser.util.OpenAPIDeserializer.getSchema(OpenAPIDeserializer.java:2542)
        at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:159)
        at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalSchema(ExternalRefProcessor.java:89)
        at io.swagger.v3.parser.processors.SchemaProcessor.processReferenceSchema(SchemaProcessor.java:213)
        at io.swagger.v3.parser.processors.SchemaProcessor.processSchema(SchemaProcessor.java:37)
        at io.swagger.v3.parser.processors.RequestBodyProcessor.processRequestBody(RequestBodyProcessor.java:47)
        at io.swagger.v3.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:45)
        at io.swagger.v3.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:84)
        at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:49)
        at io.swagger.v3.parser.OpenAPIV3Parser.resolve(OpenAPIV3Parser.java:175)
        at io.swagger.v3.parser.OpenAPIV3Parser.readContents(OpenAPIV3Parser.java:154)
        at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:89)
        at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)
        at org.openapitools.codegen.cmd.Validate.execute(Validate.java:52)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./schemas.yaml#/MySchema
[main] WARN  o.o.codegen.utils.ModelUtils - Failed to get the schema name: ./schemas.yaml#/MySchema
Exception in thread "main" java.lang.NullPointerException
        at org.openapitools.codegen.cmd.Validate.lambda$execute$2(Validate.java:76)
        at java.base/java.lang.Iterable.forEach(Iterable.java:75)
        at org.openapitools.codegen.cmd.Validate.execute(Validate.java:75)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Related issues/PRs

Might be related to #10321

Suggest a fix

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 api_with_ref.yaml and schemas.yaml using the validate command, then inspect Validate.java and the reported OpenAPIDeserializer.java path. The fix is done when an external schema containing the misspelled attribute produces a readable validation error rather than a NullPointerException, while the standalone case remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.