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

Properties with custom types inheritance fix

Open dreambrother opened this issue 1 year ago • 8 comments

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:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (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 ./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.

dreambrother avatar Mar 07 '24 13:03 dreambrother

thanks for the PR

cc @martin-mfg

wing328 avatar Mar 09 '24 09:03 wing328

@martin-mfg can you please review when you've time? thank you.

wing328 avatar Mar 21 '24 03:03 wing328

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.clone does internally is that a standard ObjectMapper is used above. In AnnotationsUtils.clone, the ObjectMapper is heavily customized.

  •   AnnotationsUtils.clone(new Schema().type("custom"), false);
    

    This does not work. Which means that in general, using allOf is 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.

martin-mfg avatar Mar 24 '24 11:03 martin-mfg

@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

dreambrother avatar Apr 08 '24 12:04 dreambrother

Any updates?

dreambrother avatar Apr 23 '24 08:04 dreambrother

Hi! Please notify me before merge, I'll resolve merge conflicts.

dreambrother avatar May 14 '24 07:05 dreambrother

Can you please do so and PM me via Slack when done?

wing328 avatar May 14 '24 12:05 wing328

@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).

dreambrother avatar May 14 '24 13:05 dreambrother

fyi @OpenAPITools/generator-core-team

wing328 avatar May 15 '24 04:05 wing328

PR merged. Thanks again for the PR.

wing328 avatar May 15 '24 14:05 wing328

[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

wing328 avatar Jun 05 '24 09:06 wing328