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

BUG properties of type Object do not have any type information when generating open api 3.1.0 #4682

Open
Lorenzo-Bracci opened this issue Jun 3, 2024 · 0 comments

Comments

@Lorenzo-Bracci
Copy link

Problem

When generating OpenAPI 3.1.0 spec, properties with the java type Object do not have any type information in the generated open api but are instead empty. As an example assume the following java model:

private static class PojoUsingObjectField {
        private Object myField;

        public Object getMyField() {
            return myField;
        }

        public void setMyField(Object myField) {
            this.myField = myField;
        }
}

When generating open api for 3.0.1 the generated model would be:

PojoUsingObjectField:
  type: object
  properties:
    myField:
      type: object

But when generating open api 3.1.0 the generated model is:

PojoUsingObjectField:
  type: object
  properties:
    myField: {}

Expected behaviour

Same as in open api 3.0.1, properties have a type field with value object.

Reproducer

The issues is reproduced in the following test (note that the test is successful if the model resolvers generate OAS3.0.1 instead):

    @Test(description = "Shows that type information in properties using a raw Object is lost when using oas 3.1.0")
    public void testModelUsingObjectTypedPropertyLosesTypeInformationForOas31() {

        String expectedYaml = "PojoUsingObjectField:\n" +
                "  type: object\n" +
                "  properties:\n" +
                "    myField:\n" +
                "      type: object";

        Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(PojoUsingObjectField.class);
        // fails as the type: object will not be added for oas 3.1.0, instead we will have "myField: {}"
        SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml);
    }

    private static class PojoUsingObjectField {
        private Object myField;

        public Object getMyField() {
            return myField;
        }

        public void setMyField(Object myField) {
            this.myField = myField;
        }
    }

Investigation

Looking at the ModelResolver code, it seems that the issue is the following: the model for a property with java type Object is generated using PrimitiveType.OBJECT#createProperty which creates a io.swagger.v3.oas.models.media.Schema with type set to object (i.e using oas 3.0.1 representation). Nevertheless in ModelResolver#717 we clone this representation before further processing and because cloning is done by serializing and then deserializing using the jackson settings for oas 3.1.0 (which expects the property types to be used instead of type) this information is lost and the cloned schema has neither type nor types set

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

No branches or pull requests

1 participant