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());
}
}
}
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);
}
}
}
}
}
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"));
}
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"));
}
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);
}
Aggregations