OpenAPITools / OpenAPITools/openapi-generator

[BUG] Presence of path parameter in parent reference triggers NPE while processing examples

Open
#21,409 1 comment 3 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?
  • 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?
    • Expected: code is generated without any issues
    • Actual: a NullPointerException is thrown during generation
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

After upgrading from v7.12.0 to v7.13.0 of openapi-generator-cli code generation of one of our OpenAPIs throws the following exception:

Exception: Cannot invoke "java.util.Map.get(Object)" because "actualComponentsExamples" is null
        at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1606)
        at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1474)
        at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:663)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1296)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:535)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.Map.get(Object)" because "actualComponentsExamples" is null
        at org.openapitools.codegen.utils.ExamplesUtils.unaliasExamples(ExamplesUtils.java:75)
        at org.openapitools.codegen.DefaultCodegen.unaliasExamples(DefaultCodegen.java:2355)
        at org.openapitools.codegen.DefaultCodegen.fromResponse(DefaultCodegen.java:5028)
        at org.openapitools.codegen.DefaultCodegen.fromOperation(DefaultCodegen.java:4685)
        at org.openapitools.codegen.languages.AbstractJavaCodegen.fromOperation(AbstractJavaCodegen.java:2225)
        at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1574)
        ... 6 more

It's not related to a particular spec, I've tested with 2 different languages (jaxrs-spec and go) and both gave the same exception. While creating the reproducer I noticed that removing the path parameter from the operation path, and thus also from its reference, made the NPE disappear:

So the following throws the NPE:

  /reproducers/{id}:
    $ref: "operations.yaml#/paths/~1reproducers~1{id}"

but this doesn't:

  /reproducers:
    $ref: "operations.yaml#/paths/~1reproducers"
openapi-generator version
  • NOK: v7.13.0
  • OK: v7.12.0
OpenAPI declaration file content or url

https://gist.github.com/clone1612/e9ca124ca323843d0175c67a4890d5ee

Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate -i /local/reproducer.yaml -g jaxrs-spec -o /local/out/jaxrs-spec      
Steps to reproduce
  1. Check out the linked gist and make sure all 3 files are located within the same directory
  2. Run the docker cli command
Related issues/PRs

The ExampleUtils class that throws the NPE was introduced in #20933 so might be related to that PR

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

Start with org.openapitools.codegen.utils.ExamplesUtils.java at the reported line 75, then trace the calls through DefaultCodegen and DefaultGenerator.processOperation. Reproduce with the linked gist and the Docker CLI command using a path parameter in the referenced operation, and compare v7.12.0 with v7.13.0. Done means generation completes for both the parameterized and non-parameterized references 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
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.