Search in sources :

Example 76 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class ModelResolver method applyBeanValidatorAnnotations.

protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annotations, Schema parent) {
    Map<String, Annotation> annos = new HashMap<>();
    if (annotations != null) {
        for (Annotation anno : annotations) {
            annos.put(anno.annotationType().getName(), anno);
        }
    }
    if (parent != null && annotations != null) {
        boolean requiredItem = Arrays.stream(annotations).anyMatch(annotation -> NOT_NULL_ANNOTATIONS.contains(annotation.annotationType().getSimpleName()));
        if (requiredItem) {
            addRequiredItem(parent, property.getName());
        }
    }
    if (annos.containsKey("javax.validation.constraints.Min")) {
        if ("integer".equals(property.getType()) || "number".equals(property.getType())) {
            Min min = (Min) annos.get("javax.validation.constraints.Min");
            property.setMinimum(new BigDecimal(min.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Max")) {
        if ("integer".equals(property.getType()) || "number".equals(property.getType())) {
            Max max = (Max) annos.get("javax.validation.constraints.Max");
            property.setMaximum(new BigDecimal(max.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Size")) {
        Size size = (Size) annos.get("javax.validation.constraints.Size");
        if ("integer".equals(property.getType()) || "number".equals(property.getType())) {
            property.setMinimum(new BigDecimal(size.min()));
            property.setMaximum(new BigDecimal(size.max()));
        } else if (property instanceof StringSchema) {
            StringSchema sp = (StringSchema) property;
            sp.minLength(Integer.valueOf(size.min()));
            sp.maxLength(Integer.valueOf(size.max()));
        } else if (property instanceof ArraySchema) {
            ArraySchema sp = (ArraySchema) property;
            sp.setMinItems(size.min());
            sp.setMaxItems(size.max());
        }
    }
    if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
        DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
        if (property instanceof NumberSchema) {
            NumberSchema ap = (NumberSchema) property;
            ap.setMinimum(new BigDecimal(min.value()));
            ap.setExclusiveMinimum(!min.inclusive());
        }
    }
    if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
        DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
        if (property instanceof NumberSchema) {
            NumberSchema ap = (NumberSchema) property;
            ap.setMaximum(new BigDecimal(max.value()));
            ap.setExclusiveMaximum(!max.inclusive());
        }
    }
    if (annos.containsKey("javax.validation.constraints.Pattern")) {
        Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");
        if (property instanceof StringSchema) {
            property.setPattern(pattern.regexp());
        }
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) BigDecimal(java.math.BigDecimal) Min(javax.validation.constraints.Min) DecimalMin(javax.validation.constraints.DecimalMin) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) DecimalMax(javax.validation.constraints.DecimalMax)

Example 77 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class ModelResolver method resolveDiscriminatorProperty.

protected void resolveDiscriminatorProperty(JavaType type, ModelConverterContext context, Schema model) {
    // add JsonTypeInfo.property if not member of bean
    JsonTypeInfo typeInfo = type.getRawClass().getDeclaredAnnotation(JsonTypeInfo.class);
    if (typeInfo != null) {
        String typeInfoProp = typeInfo.property();
        if (StringUtils.isNotBlank(typeInfoProp)) {
            Schema modelToUpdate = model;
            if (StringUtils.isNotBlank(model.get$ref())) {
                modelToUpdate = context.getDefinedModels().get(model.get$ref().substring(21));
            }
            if (modelToUpdate.getProperties() == null || !modelToUpdate.getProperties().keySet().contains(typeInfoProp)) {
                Schema discriminatorSchema = new StringSchema().name(typeInfoProp);
                modelToUpdate.addProperties(typeInfoProp, discriminatorSchema);
                if (modelToUpdate.getRequired() == null || !modelToUpdate.getRequired().contains(typeInfoProp)) {
                    modelToUpdate.addRequiredItem(typeInfoProp);
                }
            }
        }
    }
}
Also used : UUIDSchema(io.swagger.v3.oas.models.media.UUIDSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo)

Example 78 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class EnumPropertyTest method testExtractEnumReturnType.

@Test(description = "it should extract enum values from method return types")
public void testExtractEnumReturnType() {
    final Map<String, Schema> models = ModelConverters.getInstance().read(ModelWithEnumProperty.class);
    final Schema model = models.get("ModelWithEnumProperty");
    final Schema enumProperty = (Schema) model.getProperties().get("enumValue");
    assertTrue(enumProperty instanceof StringSchema);
    final StringSchema stringProperty = (StringSchema) enumProperty;
    assertEquals(stringProperty.getEnum(), Arrays.asList("PRIVATE", "PUBLIC", "SYSTEM", "INVITE_ONLY"));
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest)

Example 79 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class EnumPropertyTest method testExtractJacksonEnumFields.

@Test(description = "it should extract enum values from fields using JsonProperty and JsonValue")
public void testExtractJacksonEnumFields() {
    final Map<String, Schema> models = ModelConverters.getInstance().read(ModelWithJacksonEnumField.class);
    final Schema model = models.get("ModelWithJacksonEnumField");
    final Schema firstEnumProperty = (Schema) model.getProperties().get("firstEnumValue");
    assertTrue(firstEnumProperty instanceof StringSchema);
    final StringSchema stringProperty = (StringSchema) firstEnumProperty;
    assertEquals(stringProperty.getEnum(), Arrays.asList("p1", "p2", "SYSTEM", "INVITE_ONLY"));
    final Schema secondEnumProperty = (Schema) model.getProperties().get("secondEnumValue");
    assertTrue(secondEnumProperty instanceof StringSchema);
    final StringSchema secondStringProperty = (StringSchema) secondEnumProperty;
    assertEquals(secondStringProperty.getEnum(), Arrays.asList("one", "two", "three"));
    final Schema thirdEnumProperty = (Schema) model.getProperties().get("thirdEnumValue");
    assertTrue(thirdEnumProperty instanceof StringSchema);
    final StringSchema thirdStringProperty = (StringSchema) thirdEnumProperty;
    assertEquals(thirdStringProperty.getEnum(), Arrays.asList("2", "4", "6"));
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest)

Example 80 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.

the class ParameterSerializationTest method serializeQueryParameter.

@Test(description = "it should serialize a QueryParameter")
public void serializeQueryParameter() {
    final Parameter p = new QueryParameter().schema(new StringSchema());
    final String json = "{\"in\":\"query\",\"schema\":{\"type\":\"string\"}}";
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Aggregations

StringSchema (io.swagger.v3.oas.models.media.StringSchema)89 Test (org.testng.annotations.Test)79 Schema (io.swagger.v3.oas.models.media.Schema)70 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)54 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)53 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)47 OpenAPI (io.swagger.v3.oas.models.OpenAPI)40 PathItem (io.swagger.v3.oas.models.PathItem)22 Operation (io.swagger.v3.oas.models.Operation)21 MediaType (io.swagger.v3.oas.models.media.MediaType)21 Content (io.swagger.v3.oas.models.media.Content)20 MapSchema (io.swagger.v3.oas.models.media.MapSchema)20 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)20 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)19 DateSchema (io.swagger.v3.oas.models.media.DateSchema)17 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)16 Components (io.swagger.v3.oas.models.Components)15 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)15 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)14