Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties with custom types inheritance fix #18052

Merged
merged 11 commits into from
May 15, 2024

Conversation

dreambrother
Copy link
Contributor

@dreambrother dreambrother commented Mar 7, 2024

Approach to deep clone Schema's introduced in #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

  • Read the contribution guidelines.
  • 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.
  • 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.
  • 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)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328
Copy link
Member

wing328 commented Mar 9, 2024

thanks for the PR

cc @martin-mfg

@wing328
Copy link
Member

wing328 commented Mar 21, 2024

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

@martin-mfg
Copy link
Contributor

martin-mfg commented Mar 24, 2024

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.

@dreambrother
Copy link
Contributor Author

@wing328 I resolved conflict and removed ModelUtils.cloneSchema added here 6746be9 . It uses AnnotationUtils.clone with the same problems described in this PR

@dreambrother
Copy link
Contributor Author

Any updates?

// in openapi-generator, we also store type in `type` for 3.1 schema
// to make it backward compatible with the rest of the code base.
if (schema.getType() != null) {
clone.setType(schema.getType());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this logic is completely removed, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SchemaUtils.cloneSchema must not lose a type info after clone (instead of AnnotationUtils), so yep, I've removed it :)

Copy link
Member

@wing328 wing328 May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

FYI. we've refactored the code a bit and no longer using type to store the actual type in 3.1 spec: #18577

@wing328 wing328 added this to the 7.6.0 milestone Apr 23, 2024
return schema;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally i prefer not having another class SchemaUtils just for cloneSchema?

can we merge this (or add this back) into model utils instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@dreambrother
Copy link
Contributor Author

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

@wing328
Copy link
Member

wing328 commented May 14, 2024

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

@dreambrother
Copy link
Contributor Author

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

#' @param suffix test suffix
#' @param text Some text containing white spaces
#' @param date A date
#' @param ... Other optional arguments.
#' @export
initialize = function(`id` = NULL, `outcomes` = [SUCCESS, FAILURE], `suffix` = NULL, `text` = NULL, `date` = NULL, ...) {
initialize = function(`id` = NULL, `outcomes` = ["SUCCESS","FAILURE"], `suffix` = NULL, `text` = NULL, `date` = NULL, ...) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more minor request if you don't mind. can you please add a test for the following schema:

        outcomes:
          type: array
          items:
            type: string
            enum:
            - SUCCESS
            - FAILURE
            - SKIPPED
          default:
            - SUCCESS
            - FAILURE

to ensure the default values are clone correctly for string enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure! I've added ModelUtilsTest.testCloneArrayOfEnumsSchema

@wing328
Copy link
Member

wing328 commented May 15, 2024

fyi @OpenAPITools/generator-core-team

@wing328 wing328 merged commit 70130ed into OpenAPITools:master May 15, 2024
14 checks passed
@wing328
Copy link
Member

wing328 commented May 15, 2024

PR merged. Thanks again for the PR.

renatomameli pushed a commit to renatomameli/openapi-generator that referenced this pull request May 17, 2024
* custom types support in inheritance fix

* files changed after scripts run

* remove unused method

* move cloneSchema to ModelUtils

* imports

* changes after scripts run

* test cloning array of enums schema
@wing328
Copy link
Member

wing328 commented Jun 5, 2024

[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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants