OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.44k stars 6.48k forks source link

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

Open CloudNiner opened 3 days ago

CloudNiner commented 3 days ago

Bug Report Checklist

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:

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 commented 9 hours ago

thanks for reporting the issue.

filed https://github.com/OpenAPITools/openapi-generator/pull/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.