Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] generate throws NullPointerException on OAS 3.1 (JSON Schema 2020-12) arrays with only prefixItems #19704

Closed
5 of 6 tasks
CloudNiner opened this issue Sep 27, 2024 · 3 comments

Comments

@CloudNiner
Copy link

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

The OAS 3.1 valid specification attached below throws:

java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Schema.get$ref()" because "p" is null
        at org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen.getSchemaType(AbstractTypeScriptClientCodegen.java:774)

when attempting to generate a client for at least the following client generators:

  • typescript-axios
  • typescript-fetch
  • python

Interestingly enough, it passes validation via the validate command.

The seemingly problematic chunk of the spec is valid JSON Schema 2020-12 per the OAS 3.1 spec:

{"bbox":{"anyOf":[{"prefixItems":[{"type":"number"},{"type":"number"},{"type":"number"},{"type":"number"}],"type":"array","maxItems":4,"minItems":4},{"prefixItems":[{"type":"number"},{"type":"number"},{"type":"number"},{"type":"number"},{"type":"number"},{"type":"number"}],"type":"array","maxItems":6,"minItems":6},{"type":"null"}],"title":"Bbox"}}
openapi-generator version

7.8.0

OpenAPI declaration file content or url

openapi_3.1.0_pydantic-v2.json

Generation Details

validate works???

❯ java -jar openapi-generator-cli.jar validate -i openapi_3.1.0_pydantic-v2.json
Validating spec (openapi_3.1.0_pydantic-v2.json)
No validation issues detected.

generate fails:

docker run -v .:/home openapitools/openapi-generator-cli:latest generate -i /home/openapi_3.1.0_pydantic-v2.json -o /home/dist/openapi-demo-typescript-client-openapi-generator/ -g typescript-axios
[main] WARN  o.o.codegen.DefaultCodegen - Generation using 3.1.0 specs is in development and is not officially supported yet. If you would like to expedite development, please consider working on the open issues in the 3.1.0 project: https://github.com/orgs/OpenAPITools/projects/4/views/1 and reach out to our team on Slack at https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g
[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - Output directory (/home/dist/openapi-demo-typescript-client-openapi-generator) does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: typescript-axios (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'typescript-axios' is considered stable.
[main] INFO  o.o.c.l.AbstractTypeScriptClientCodegen - Hint: Environment variable 'TS_POST_PROCESS_FILE' (optional) not defined. E.g. to format the source code, please try 'export TS_POST_PROCESS_FILE="/usr/local/bin/prettier --write"' (Linux/Mac)
[main] INFO  o.o.c.l.AbstractTypeScriptClientCodegen - Note: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] WARN  o.o.codegen.DefaultCodegen - The value (generator's option) must be either boolean or string. Default to `false`.
[main] ERROR o.o.codegen.utils.ModelUtils - Undefined array inner type for `null`. Default to String.
[main] ERROR o.o.codegen.utils.ModelUtils - Undefined array inner type for `null`. Default to String.
[main] ERROR o.o.codegen.utils.ModelUtils - Undefined array inner type for `null`. Default to String.
[main] ERROR o.o.codegen.utils.ModelUtils - Undefined array inner type for `null`. Default to String.
[main] INFO  o.o.codegen.InlineModelResolver - Inline schema created as ValidationError_loc_inner. To have complete control of the model name, set the `title` field or use the modelNameMapping option (e.g. --model-name-mappings ValidationError_loc_inner=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings ValidationError_loc_inner=NewModel,ModelA=NewModelA in CLI).
[main] WARN  o.o.codegen.DefaultCodegen - Generation using 3.1.0 specs is in development and is not officially supported yet. If you would like to expedite development, please consider working on the open issues in the 3.1.0 project: https://github.com/orgs/OpenAPITools/projects/4/views/1 and reach out to our team on Slack at https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.DefaultGenerator - Model Position2D not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
[main] INFO  o.o.codegen.DefaultGenerator - Model Position3D not generated since it's an alias to array (without property) and `generateAliasAsModel` is set to false (default)
Exception in thread "main" java.lang.RuntimeException: Could not process model 'Bbox'.Please make sure that your schema is correct!
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:521)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:446)
        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 "io.swagger.v3.oas.models.media.Schema.get$ref()" because "p" is null
        at org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen.getSchemaType(AbstractTypeScriptClientCodegen.java:774)
        at org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen.lambda$getTypesFromSchemas$6(AbstractTypeScriptClientCodegen.java:1149)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
        at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
        at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
        at java.base/java.util.stream.ReferencePipeline.collect(Unknown Source)
        at org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen.getTypesFromSchemas(AbstractTypeScriptClientCodegen.java:1152)
        at org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen.toAnyOfName(AbstractTypeScriptClientCodegen.java:1114)
        at org.openapitools.codegen.DefaultCodegen.getSchemaType(DefaultCodegen.java:2229)
        at org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen.getSchemaType(AbstractTypeScriptClientCodegen.java:784)
        at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:3036)
        at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1751)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:516)
        ... 5 more


Steps to reproduce
docker run -v .:/home openapitools/openapi-generator-cli:latest generate -i /home/openapi_3.1.0_pydantic-v2.json -o /home/dist/openapi-demo-typescript-client-openapi-generator/ -g typescript-axios

where openapi_3.1.0_pydantic-v2.json is the json schema linked in the gist above

Related issues/PRs

Couldn't find any

Suggest a fix
@wing328
Copy link
Member

wing328 commented Oct 1, 2024

thanks for reporting the issue.

filed #19735 to better handle NPE when handling prefixItems.

tested with your spec but there are other issues though so I'll file separate PRs to handle additional issues uncovered by my tests.

@wing328
Copy link
Member

wing328 commented Oct 4, 2024

update: filed and merged #19758 with handling of anyOf and oneOf in 3.1 spec

@CloudNiner
Copy link
Author

Thanks for the quick fix! The actual OpenAPI 3.1 document that the example in this issue was derived from generated successfully 🎉 🎉. A bit difficult to diff the generated typescript-axios clients but there don't seem to be any meaningful changes in the generated interfaces that are related to BBox.

I'll test further once 7.9.0 is released!

@wing328 wing328 closed this as completed Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants