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 @Schema#implementation not processed on properties when generating open api 3.1.0 #4679

Open
Lorenzo-Bracci opened this issue May 30, 2024 · 0 comments

Comments

@Lorenzo-Bracci
Copy link

Lorenzo-Bracci commented May 30, 2024

Problem

When generating OpenAPI 3.1.0 spec the @Schema#implementation value on a property is ignored.

Expected behaviour

@Schema#implementation is considered also in OAS3.1

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 @Schema#implementation is not propagated from property when using oas 3.1.0")
    public void testModelWithCustomSchemaImplementationInPropertyForOas31() {

        String expectedYaml = "ModelWithCustomSchemaImplementationInProperty:\n" +
                "  type: object\n" +
                "  properties:\n" +
                "    exampleField:\n" +
                // the expected type is int rather than string as we override the type with @Schema(implementation = Integer.class)
                "      type: integer\n" +
                "      format: int32\n";

        Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(ModelWithCustomSchemaImplementationInProperty.class);

        // fails as @Schema#implementation is not processed and therefore the original property type is used (String)
        SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml);
    }

    static class ModelWithCustomSchemaImplementationInProperty {

        @Schema(implementation = Integer.class)
        private String exampleField;

        public String getExampleField() {
            return exampleField;
        }

        public void setExampleField(String exampleField) {
            this.exampleField = exampleField;
        }

    }

Investigation with proposed solution

Looking at the ModelResolver code, it seems that @Schema annotation is not propagated when attempting to resolve a model for a property (it is excluded from context annotations in ModelResolver#682). There is an attempt to reconcile this information at ModelResolver#719 (through AnnotationsUtils.getSchemaFromAnnotation). Information from @Schema#implementation is not used here but I imagine that it is necessary to use this information before attempting to resolve the model rather than after as it seems central in the model resolution process. Therefore I wonder if possibly overriding the AnnotatedType#type used for context.resolve in ModelResolver#716 could be a proper solution for the issue?

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