openapi-generator
openapi-generator copied to clipboard
Properties with custom types inheritance fix
Approach to deep clone Schema's introduced in https://github.com/OpenAPITools/openapi-generator/pull/16992 breaks support for properties with custom types (added using schemaMappings or type/importMappings) when such properties are inherited using allOf. AnnotationsUtils#clone returns null for them. I've replaced AnnotationsUtils#clone with ObjectMapper's typed serialization/deserialization.
@OpenAPITools/generator-core-team
PR checklist
- [x] Read the contribution guidelines.
- [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- [x] Run the following to build the project and update samples:
(For Windows users, please run the script in Git BASH) Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./mvnw clean package ./bin/generate-samples.sh ./bin/configs/*.yaml ./bin/utils/export_docs_generators.sh./bin/generate-samples.sh bin/configs/java*. IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - [x] File the PR against the correct branch:
master(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - [x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
thanks for the PR
cc @martin-mfg
@martin-mfg can you please review when you've time? thank you.
Hi, the changes look good to me. 👍
just some additional results from my experiments, in case this PR is referenced again in the future:
-
String serialized = "{\"type\" : \"custom\"}"; new ObjectMapper().readValue(serialized, io.swagger.v3.oas.models.media.Schema.class);This works. The relevant difference to what
AnnotationsUtils.clonedoes internally is that a standardObjectMapperis used above. InAnnotationsUtils.clone, theObjectMapperis heavily customized. -
AnnotationsUtils.clone(new Schema().type("custom"), false);This does not work. Which means that in general, using
allOfis not necessary to trigger the issue. Using an unknown type is sufficient. -
The changes in the R sample code are not related to the changed cloning logic. Updating the samples with the existing logic results in the same sample changes.
@wing328 I resolved conflict and removed ModelUtils.cloneSchema added here https://github.com/OpenAPITools/openapi-generator/commit/6746be9a507e68075f2bbeef148fef235efabc66 . It uses AnnotationUtils.clone with the same problems described in this PR
Any updates?
Hi! Please notify me before merge, I'll resolve merge conflicts.
Can you please do so and PM me via Slack when done?
@wing328 sorry, I don't use slack. I've resolved merge conflicts. Had to add jackson-annotations dependency explicitly, because openapi-generator right now has different minor versions of jackson in dependencies (lower for jackson-datatype-threetenbp).
fyi @OpenAPITools/generator-core-team
PR merged. Thanks again for the PR.
[main] ERROR o.o.codegen.utils.ModelUtils - Can't clone schema class DateTimeSchema {
class Schema {
type: string
format: date-time
$ref: null
description: null
title: null
multipleOf: null
maximum: null
exclusiveMaximum: null
minimum: null
exclusiveMinimum: null
maxLength: null
minLength: null
pattern: null
maxItems: null
minItems: null
uniqueItems: null
maxProperties: null
minProperties: null
required: null
not: null
properties: null
additionalProperties: null
nullable: null
readOnly: null
writeOnly: null
example: 2020-02-02T20:20:20.000222Z
externalDocs: null
deprecated: null
discriminator: null
xml: null
}
}
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Type id handling not implemented for type java.lang.Object (by serializer of type com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer) (through reference chain: io.swagger.v3.oas.models.media.DateTimeSchema["example"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1330)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:414)
at com.fasterxml.jackson.databind.JsonSerializer.serializeWithType(JsonSerializer.java:173)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:734)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:653)
at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:502)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:341)
at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4799)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:4040)
at org.openapitools.codegen.utils.ModelUtils.cloneSchema(ModelUtils.java:2191)
at org.openapitools.codegen.DefaultCodegen.lambda$mergeProperties$13(DefaultCodegen.java:2865)
at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:721)
at org.openapitools.codegen.DefaultCodegen.mergeProperties(DefaultCodegen.java:2865)
at org.openapitools.codegen.DefaultCodegen.updateModelForComposedSchema(DefaultCodegen.java:2779)
at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:3120)
at org.openapitools.codegen.languages.AbstractJavaCodegen.fromModel(AbstractJavaCodegen.java:1738)
at org.openapitools.codegen.languages.JavaClientCodegen.fromModel(JavaClientCodegen.java:1055)
at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1757)
at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:525)
at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:449)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1302)
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)
@dreambrother can you please take a look at the above exception when you've time?
To repeat it, please run ./bin/generate-samples.sh ./bin/configs/java-okhttp-gson.yaml