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

AnyOf normalization/simplification isNullTypeSchema issue

Open beikov opened this issue 1 year ago • 1 comments

Bug Report Checklist

  • [ ] Have you provided a full/minimal spec to reproduce the issue?
  • [ ] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When importing the schema for the GitHub API, wrong Java code is generated for FullRepository#license. While debugging, I figured that both schemas, repository and full-repository have a property license that points to the same subschema. Originally, that license schema had 2 anyOf elements, one is type = null and the other is ref = ....

After the first normalization/simplification step for the repository schema, the license schema is made nullable and the anyOf element with type = null is removed. Since there is only a single anyOf left, the code in OpenAPINormalizer#processSimplifyAnyOf sets nullable = true on the other anyOf with ref = ..., and returns that for the license property of the repository schema. By this time, the damage was already done, since the license property schema of full-repository now only has a single anyOf left which on top of all that is also marked nullable = true. When entering OpenAPINormalizer#processSimplifyAnyOf again for the license property of full-repository, the anyOf is removed, because isNullTypeSchema only checks if the nullable flag is set. Since it was set by the previous normalization, the schema for license is left with no possible types.

openapi-generator version

7.8.0-SNAPSHOT

OpenAPI declaration file content or url

https://github.com/github/rest-api-description/raw/main/descriptions-next/api.github.com/api.github.com.json

Generation Details
Steps to reproduce

Just generate code with the jersey3 library and jackson serializationLibrary.

Related issues/PRs
Suggest a fix

There are multiple possible fixes, though I have not yet tried any of them.

  • Don't mutate schemas during normalization by i.e. copying state
  • Don't mark other anyOf schemas as nullable = true, only the containing schema
  • Add a condition schema.get$ref() == null in the check in isNullTypeSchema

beikov avatar Aug 23 '24 19:08 beikov

  1. Adding the condition seems to do the trick. Will do some further testing and provide a PR with a fix as soon as I fixed all the issues with importing the GitHub API.

beikov avatar Aug 23 '24 21:08 beikov

Add a condition schema.get$ref() == null in the check in isNullTypeSchema

Thanks for reporting the issue. I've filed https://github.com/OpenAPITools/openapi-generator/pull/19781 to improve isNullTypeSchema

wing328 avatar Oct 04 '24 11:10 wing328

Thanks. Looks like https://github.com/OpenAPITools/openapi-generator/pull/19781 fixed the problem.

beikov avatar Oct 10 '24 16:10 beikov