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 @Pattern on type parameter of properties using collections is ignored when using oas 3.1.0 #4703

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

Comments

@Lorenzo-Bracci
Copy link

Problem

When generating OpenAPI 3.1.0 spec the @Pattern annotation on a type parameter of properties with a collection type are ignored. The expected behaviour is that the generated property has the pattern property set for the items schema but no pattern is found in the generated open api.
This issue is similar to #4702

Expected behaviour

@Pattern#regexp is used to generate pattern property for the items schema of the property

Reproducer

The issues is reproduced in the following test:

    @Test(description = "Shows that @Pattern is not correctly handled in type parameters of properties using collections when using oas 3.1.0")
    public void testModelUsingCollectionTypePropertyDoesNotHandlePatternAnnotationForOas31() {

        String expectedYaml = "DtoUsingPatternOnCollection:\n" +
                "  type: object\n" +
                "  properties:\n" +
                "    myField:\n" +
                "      type: array\n" +
                "      items:\n" +
                "        pattern: myPattern\n" +
                "        type: string";

        Map<String, io.swagger.v3.oas.models.media.Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(DtoUsingPatternOnCollection.class);
        // fails as pattern will not be added to the items property of myField for oas 3.1.0
        SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml);
    }

    public class DtoUsingPatternOnCollection {
        private List<@Pattern(regexp = "myPattern") String> myField;

        public List<String> getMyField() {
            return myField;
        }

        public void setMyField(List<String> myField) {
            this.myField = myField;
        }
    }

Investigation with proposed solution

Looking at the ModelResolver.applyBeanValidatorAnnotations code (which is where information from bean validation annotations are processed), the @Pattern annotation is processed only if the property input parameter of type Schema is an instance of StringSchema or if property.getItems() is an instance of StringSchema. If the check succeeds then the information from @Pattern is propagated to the generated schema (using the pattern property). Nevertheless for open api 3.1.0 property.getItems() will be of type JsonSchema instead of StringSchema and therefore the information from @Pattern will not be used. A possible solution would be that instead of using doing an instanceOf check against StringSchema a check is performed on the type/types fields to ensure that the property is a string schema.

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