openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Schema.get$ref()" because "schema" is null

Open scottbusche-usps opened this issue 11 months ago • 11 comments

Using OpenAPI Generator 7.18.0, against https://developers.usps.com/sites/default/files/apidoc_specs/containers_4.yaml, I get the following error:

Generating client for spec=containers/containers.yaml lang=java -> generated-clients/containers/java
[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - No .openapi-generator-ignore file found.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: java (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'java' is considered stable.
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Environment variable JAVA_POST_PROCESS_FILE not defined so the Java code may not be properly formatted. To define it, try 'export JAVA_POST_PROCESS_FILE="/usr/local/bin/clang-format -i"' (Linux/Mac)
[main] INFO  o.o.c.languages.AbstractJavaCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] ERROR o.o.codegen.DefaultGenerator - An exception occurred in OpenAPI Normalizer. Please report the issue via https://github.com/openapitools/openapi-generator/issues/new/: 
[main] ERROR o.o.codegen.DefaultGenerator - An exception occurred in OpenAPI Normalizer. Please report the issue via https://github.com/openapitools/openapi-generator/issues/new/: 
java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Schema.get$ref()" because "schema" is null
	at org.openapitools.codegen.OpenAPINormalizer.normalizeSchema(OpenAPINormalizer.java:699)
	at org.openapitools.codegen.OpenAPINormalizer.normalizeHeaders(OpenAPINormalizer.java:561)
	at org.openapitools.codegen.OpenAPINormalizer.normalizeResponse(OpenAPINormalizer.java:545)
	at org.openapitools.codegen.OpenAPINormalizer.normalizeResponses(OpenAPINormalizer.java:533)
	at org.openapitools.codegen.OpenAPINormalizer.normalizePaths(OpenAPINormalizer.java:426)
	at org.openapitools.codegen.OpenAPINormalizer.normalize(OpenAPINormalizer.java:355)
	at org.openapitools.codegen.DefaultGenerator.configureGeneratorProperties(DefaultGenerator.java:266)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1277)
	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)
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation post-containers-with-container-id
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation delete-containers-with-container-id
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation delete-containers-with-container-id-with-packages
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation post-containers
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation post-manifest

Compilation error:

Error:  COMPILATION ERROR : 
Error:  /home/runner/work/usps-api/usps-api/generated-clients/containers/java/src/main/java/org/openapitools/client/model/MailingDateOptions.java:[105,33] error: cannot find symbol
  symbol:   method validateJsonElement(JsonElement)
  location: class LocalDate
Error:  /home/runner/work/usps-api/usps-api/generated-clients/containers/java/src/main/java/org/openapitools/client/model/MailingDateOptions.java:[117,38] error: cannot find symbol
  symbol:   method validateJsonElement(JsonElement)
  location: class OffsetDateTime
Error:  /home/runner/work/usps-api/usps-api/generated-clients/containers/java/src/main/java/org/openapitools/client/model/MailingDateOptions.java:[229,21] error: cannot find symbol
  symbol:   method validateJsonElement(JsonElement)
  location: class LocalDate
Error:  /home/runner/work/usps-api/usps-api/generated-clients/containers/java/src/main/java/org/openapitools/client/model/MailingDateOptions.java:[237,26] error: cannot find symbol
  symbol:   method validateJsonElement(JsonElement)
  location: class OffsetDateTime
Error:  Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project openapi-java-client: Compilation failure: Compilation failure: 
Error:  /home/runner/work/usps-api/usps-api/generated-clients/containers/java/src/main/java/org/openapitools/client/model/MailingDateOptions.java:[105,33] error: cannot find symbol
Error:    symbol:   method validateJsonElement(JsonElement)
Error:    location: class LocalDate
Error:  /home/runner/work/usps-api/usps-api/generated-clients/containers/java/src/main/java/org/openapitools/client/model/MailingDateOptions.java:[117,38] error: cannot find symbol
Error:    symbol:   method validateJsonElement(JsonElement)
Error:    location: class OffsetDateTime
Error:  /home/runner/work/usps-api/usps-api/generated-clients/containers/java/src/main/java/org/openapitools/client/model/MailingDateOptions.java:[229,21] error: cannot find symbol
Error:    symbol:   method validateJsonElement(JsonElement)
Error:    location: class LocalDate
Error:  /home/runner/work/usps-api/usps-api/generated-clients/containers/java/src/main/java/org/openapitools/client/model/MailingDateOptions.java:[237,26] error: cannot find symbol
Error:    symbol:   method validateJsonElement(JsonElement)
Error:    location: class OffsetDateTime
Error:  -> [Help 1]
Error:  
Error:  To see the full stack trace of the errors, re-run Maven with the -e switch.
Error:  Re-run Maven using the -X switch to enable full debug logging.
Error:  
Error:  For more information about the errors and possible solutions, please read the following articles:
Error:  [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Error: Process completed with exit code 1.

No error is present in 7.17.0 and the code is successfully generated and compiles. This is happening in numerous APIs after upgrading, but it's not clear to me where the error is coming from.

scottbusche-usps avatar Dec 22 '25 15:12 scottbusche-usps

thanks for reporting the issue with the details.

we will look into it and try to come up with a fix before the upcoming release

wing328 avatar Dec 22 '25 15:12 wing328

Thanks, let me know if I can provide any other details. I'm guessing it's related to our usage of a field that takes in a date or a date-time, as they've been problematic in the past.

scottbusche-usps avatar Dec 22 '25 16:12 scottbusche-usps

I've got the same issue and I was able to pin it down to one likely cause: In https://github.com/OpenAPITools/openapi-generator/blob/cd01ba9bc141184ddd1d8fec822d116e7e8b79f5/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java#L560 the header from the map is assumed to be the actual instance of the header definition. This is not the case when it is a reference to a header component. To cover this case we have to add a call to ModelUtils.getReferencedHeader:

            Header h = ModelUtils.getReferencedHeader(openAPI, headers.get(headerKey));
            Schema updatedHeader = normalizeSchema(h.getSchema(), new HashSet<>());

stefan-hensel-jambit avatar Dec 30 '25 08:12 stefan-hensel-jambit

I have pushed a fix for the null pointer exception due to missing header de-referencing in #22613 to address issues I am having with the 7.18.0 upgrade.

The oneOf string (format: date) | string (formate: date-time) seems to be a separate issue.

Out of curiosity, it does not generate valid code for me with v7.17.0 via java -jar openapi-generator-cli.jar generate -i containers_4.yaml -g java -o generated in the sense that it compiles but it references an empty MailingDateOptions parent class, and on v7.18.0 it attempts to call validateJsonElement on LocalDate/OffsetDateTime via some nested typefactory causing issues. The behaviour was introduced via the changes in #22364 based on git bisect between the two versions.

I am not sure what code is expected to be generated for this though as you are trying to model a field of type string that would somehow need to be serialized/deserialized as being either an instance of LocalDate or OffsetDateTime based on the format, i.e. private ??? mailingDate.

adrianhj avatar Jan 02 '26 12:01 adrianhj

I'm having the same issue with 7.18.0:

 openapi: 3.1.0
 info:
   title: Minimal Test
   version: 1.0.0
 paths:
   /test:
     post:
       operationId: createTest
       responses:
         '201':
           description: Created
           headers:
             Location:
               $ref: '#/components/headers/Location'
 components:
   headers:
     Location:
       description: URL of created resource
       schema:
         type: string
         format: uri

yields:

  java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Schema.get$ref()" because "schema" is null
      at org.openapitools.codegen.OpenAPINormalizer.normalizeSchema(OpenAPINormalizer.java:699)
      at org.openapitools.codegen.OpenAPINormalizer.normalizeHeaders(OpenAPINormalizer.java:561)
      at org.openapitools.codegen.OpenAPINormalizer.normalizeResponse(OpenAPINormalizer.java:545)
      at org.openapitools.codegen.OpenAPINormalizer.normalizeResponses(OpenAPINormalizer.java:533)
      at org.openapitools.codegen.OpenAPINormalizer.normalizePaths(OpenAPINormalizer.java:426)
      at org.openapitools.codegen.OpenAPINormalizer.normalize(OpenAPINormalizer.java:355)

of course, inlining the header definition instead of using $ref removes the error.

flozano avatar Jan 04 '26 16:01 flozano

thanks for reporting the issue with the details.

we will look into it and try to come up with a fix before the upcoming release

@wing328 any update to this issue - when do you expect the upcoming release?

JohannesProbst avatar Jan 08 '26 14:01 JohannesProbst

i'll try to address it coming weekend and try to cut a release next week if possible

please use v7.17.0 for the time being

wing328 avatar Jan 08 '26 14:01 wing328

UPDATE: I've merged https://github.com/OpenAPITools/openapi-generator/pull/22671 (better null check and sample update to catch the issue moving forward).

Please give it a try with the latest master: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-test-with-the-latest-master-of-openapi-generator

wing328 avatar Jan 11 '26 14:01 wing328

I tested with a couple more spec and no longer able to repeat the issue

will try to cut a release this weekend if no one has further feedback on the fix

wing328 avatar Jan 14 '26 06:01 wing328

Specs that were previously failing are generating fine for me with latest master.

adrianhj avatar Jan 14 '26 13:01 adrianhj

Thanks, confirmed this was fixed in the v7.19.0 release.

scottbusche-usps avatar Jan 21 '26 20:01 scottbusche-usps