|
| 1 | +package io.swagger.v3.core.resolving; |
| 2 | + |
| 3 | +import io.swagger.v3.core.converter.AnnotatedType; |
| 4 | +import io.swagger.v3.core.converter.ModelConverterContextImpl; |
| 5 | +import io.swagger.v3.core.jackson.ModelResolver; |
| 6 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 7 | +import org.testng.annotations.Test; |
| 8 | + |
| 9 | +import javax.validation.constraints.Email; |
| 10 | +import java.net.URI; |
| 11 | + |
| 12 | +import static io.swagger.v3.core.resolving.SwaggerTestBase.mapper; |
| 13 | +import static org.testng.Assert.assertEquals; |
| 14 | +import static org.testng.Assert.assertNotNull; |
| 15 | +import static org.testng.Assert.assertTrue; |
| 16 | + |
| 17 | +public class Ticket5185Test { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testExplicitFormatOverridesTypeDerivedFormat() { |
| 21 | + final ModelResolver modelResolver = new ModelResolver(mapper()); |
| 22 | + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); |
| 23 | + |
| 24 | + io.swagger.v3.oas.models.media.Schema schema = context.resolve(new AnnotatedType(UriExample.class)); |
| 25 | + |
| 26 | + assertNotNull(schema); |
| 27 | + io.swagger.v3.oas.models.media.Schema uriReference = |
| 28 | + (io.swagger.v3.oas.models.media.Schema) schema.getProperties().get("uriReferenceValue"); |
| 29 | + assertNotNull(uriReference); |
| 30 | + assertEquals(uriReference.getFormat(), "uri-reference"); |
| 31 | + |
| 32 | + // A URI field without an explicit @Schema(format=...) still resolves to the default "uri" format |
| 33 | + io.swagger.v3.oas.models.media.Schema plainUri = |
| 34 | + (io.swagger.v3.oas.models.media.Schema) schema.getProperties().get("uriValue"); |
| 35 | + assertNotNull(plainUri); |
| 36 | + assertEquals(plainUri.getFormat(), "uri"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testExplicitFormatPrecedenceForStringAndNumericTypes() { |
| 41 | + final ModelResolver modelResolver = new ModelResolver(mapper()); |
| 42 | + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); |
| 43 | + |
| 44 | + io.swagger.v3.oas.models.media.Schema schema = context.resolve(new AnnotatedType(ExplicitFormatExample.class)); |
| 45 | + |
| 46 | + assertNotNull(schema); |
| 47 | + assertProperty(schema, "uuidValue", "string", "uuid"); |
| 48 | + assertProperty(schema, "emailValue", "string", "email"); |
| 49 | + assertProperty(schema, "emailValidatedValue", "string", "email"); |
| 50 | + assertProperty(schema, "beanValidationEmailValue", "string", "email"); |
| 51 | + assertProperty(schema, "longValue", "integer", "int64"); |
| 52 | + assertProperty(schema, "integerAsInt64Value", "integer", "int64"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testExplicitFormatOverridesTypeDerivedFormatOpenApi31() { |
| 57 | + final ModelResolver modelResolver = new ModelResolver(mapper()); |
| 58 | + modelResolver.setOpenapi31(true); |
| 59 | + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); |
| 60 | + |
| 61 | + io.swagger.v3.oas.models.media.Schema schema = context.resolve(new AnnotatedType(UriExample.class)); |
| 62 | + |
| 63 | + assertNotNull(schema); |
| 64 | + io.swagger.v3.oas.models.media.Schema uriReference = |
| 65 | + (io.swagger.v3.oas.models.media.Schema) schema.getProperties().get("uriReferenceValue"); |
| 66 | + assertNotNull(uriReference); |
| 67 | + assertEquals(uriReference.getFormat(), "uri-reference"); |
| 68 | + |
| 69 | + io.swagger.v3.oas.models.media.Schema plainUri = |
| 70 | + (io.swagger.v3.oas.models.media.Schema) schema.getProperties().get("uriValue"); |
| 71 | + assertNotNull(plainUri); |
| 72 | + assertEquals(plainUri.getFormat(), "uri"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testExplicitFormatPrecedenceForStringAndNumericTypesOpenApi31() { |
| 77 | + final ModelResolver modelResolver = new ModelResolver(mapper()); |
| 78 | + modelResolver.setOpenapi31(true); |
| 79 | + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); |
| 80 | + |
| 81 | + io.swagger.v3.oas.models.media.Schema schema = context.resolve(new AnnotatedType(ExplicitFormatExample.class)); |
| 82 | + |
| 83 | + assertNotNull(schema); |
| 84 | + assertProperty31(schema, "uuidValue", "string", "uuid"); |
| 85 | + assertProperty31(schema, "emailValue", "string", "email"); |
| 86 | + assertProperty31(schema, "emailValidatedValue", "string", "email"); |
| 87 | + assertProperty31(schema, "beanValidationEmailValue", "string", "email"); |
| 88 | + assertProperty31(schema, "longValue", "integer", "int64"); |
| 89 | + assertProperty31(schema, "integerAsInt64Value", "integer", "int64"); |
| 90 | + } |
| 91 | + |
| 92 | + private static void assertProperty(io.swagger.v3.oas.models.media.Schema schema, String propertyName, String type, String format) { |
| 93 | + io.swagger.v3.oas.models.media.Schema property = |
| 94 | + (io.swagger.v3.oas.models.media.Schema) schema.getProperties().get(propertyName); |
| 95 | + assertNotNull(property); |
| 96 | + assertEquals(property.getType(), type); |
| 97 | + assertEquals(property.getFormat(), format); |
| 98 | + } |
| 99 | + |
| 100 | + private static void assertProperty31(io.swagger.v3.oas.models.media.Schema schema, String propertyName, String type, String format) { |
| 101 | + io.swagger.v3.oas.models.media.Schema property = |
| 102 | + (io.swagger.v3.oas.models.media.Schema) schema.getProperties().get(propertyName); |
| 103 | + assertNotNull(property); |
| 104 | + assertNotNull(property.getTypes()); |
| 105 | + assertTrue(property.getTypes().contains(type)); |
| 106 | + assertEquals(property.getFormat(), format); |
| 107 | + } |
| 108 | + |
| 109 | + static class UriExample { |
| 110 | + @Schema(format = "uri-reference") |
| 111 | + public URI uriReferenceValue; |
| 112 | + |
| 113 | + public URI uriValue; |
| 114 | + } |
| 115 | + |
| 116 | + static class ExplicitFormatExample { |
| 117 | + @Schema(format = "uuid") |
| 118 | + public String uuidValue; |
| 119 | + |
| 120 | + @Schema(format = "email") |
| 121 | + public String emailValue; |
| 122 | + |
| 123 | + @Schema(format = "email") |
| 124 | + @Email |
| 125 | + public String emailValidatedValue; |
| 126 | + |
| 127 | + @Email |
| 128 | + public String beanValidationEmailValue; |
| 129 | + |
| 130 | + @Schema(format = "int64") |
| 131 | + public Long longValue; |
| 132 | + |
| 133 | + @Schema(format = "int64") |
| 134 | + public Integer integerAsInt64Value; |
| 135 | + } |
| 136 | +} |
0 commit comments