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.78k stars 6.57k forks source link

[BUG] Generator breaks on empty properties fields in components. #17172

Closed davegarred closed 11 months ago

davegarred commented 11 months ago

Bug Report Checklist

Description

Generator breaks on empty properties fields in components.

openapi-generator version

7.2.0-SNAPSHOT commit: 0d2a01611c8350b596a2b883964da2589e7baa93

OpenAPI declaration file content or url

The Discord API contains multiple empty properties which break in this tool but are valid. I've pushed up a simpler version taken taken from that.

Generation Details

Attempt to generate a client from a spec that includes a schema with property that is empty. E.g.,

"components": {
  "schemas": {
    "GithubWebhook": {
      "type": "object",
      "properties": {
        "pull_request": {}
      }
    }
  }
}

A NullPointerException is produced.

Exception: null
    at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1269)
    at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1163)
    at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:609)
    at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:994)
    at org.openapitools.codegen.cmd.Generate.execute(Generate.java:519)
    at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
    at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.NullPointerException
    at org.openapitools.codegen.examples.ExampleGenerator.resolvePropertyToExample(ExampleGenerator.java:228)
    at org.openapitools.codegen.examples.ExampleGenerator.resolveModelToExample(ExampleGenerator.java:352)
    at org.openapitools.codegen.examples.ExampleGenerator.resolvePropertyToExample(ExampleGenerator.java:311)
    at org.openapitools.codegen.examples.ExampleGenerator.resolveModelToExample(ExampleGenerator.java:352)
    at org.openapitools.codegen.examples.ExampleGenerator.generate(ExampleGenerator.java:159)
    at org.openapitools.codegen.DefaultCodegen.fromOperation(DefaultCodegen.java:4673)
    at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1237)
    ... 6 more
Steps to reproduce

Generate using the cli tool (any language).

docker run --rm -v ./:/local \
  openapitools/openapi-generator-cli \
  generate \
  -g rust \
  -o /local/output_directory \
  -i /local/openapi.json
Related issues/PRs

None found yet.

Suggest a fix

Edit: https://github.com/OpenAPITools/openapi-generator/pull/17173/files

wing328 commented 11 months ago

thanks for reporting the issue.

I've filed https://github.com/OpenAPITools/openapi-generator/issues/17172 to address the NPE in example generator.

Will file another PR later to better support any type in 3.1 spec.

wing328 commented 11 months ago

merged the fix to the master. please pull the latest master to give it a try.