use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.
the class ModelPropertyTest method extractProperties.
@Test
public void extractProperties() {
final Map<String, Schema> models = ModelConverters.getInstance().readAll(Family.class);
assertEquals(models.size(), 3);
final Schema person = models.get("Person");
final Schema employer = (Schema) person.getProperties().get("employer");
assertTrue(employer instanceof ArraySchema);
final ArraySchema employerProperty = (ArraySchema) employer;
final Schema items = employerProperty.getItems();
assertEquals(items.get$ref(), "#/components/schemas/Employer");
final Schema awards = (Schema) person.getProperties().get("awards");
assertTrue(awards instanceof ArraySchema);
assertTrue(((ArraySchema) awards).getItems() instanceof StringSchema);
}
use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.
the class JodaLocalDateConverterTest method testJodaLocalDate.
@Test
public void testJodaLocalDate() {
final Map<String, Schema> models = ModelConverters.getInstance().read(ModelWithJodaLocalDate.class);
assertEquals(models.size(), 1);
final Schema model = models.get("ModelWithJodaLocalDate");
final Schema dateTimeProperty = (Schema) model.getProperties().get("createdAt");
assertTrue(dateTimeProperty instanceof DateSchema);
assertTrue(model.getRequired().contains("createdAt"));
assertEquals(dateTimeProperty.getDescription(), "creation localDate");
final Schema nameProperty = (Schema) model.getProperties().get("name");
assertTrue(nameProperty instanceof StringSchema);
assertEquals(nameProperty.getDescription(), "name of the model");
}
use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.
the class ModelWithRangesTest method modelWithRangesTest.
@Test(description = "test model with @ApiModelProperty.allowableValues")
public void modelWithRangesTest() {
final Map<String, Schema> properties = ModelConverters.getInstance().read(ModelWithRanges.class).get("ModelWithRanges").getProperties();
final IntegerSchema inclusiveRange = (IntegerSchema) properties.get("inclusiveRange");
assertEquals(inclusiveRange.getMinimum(), new BigDecimal(1));
assertEquals(inclusiveRange.getMaximum(), new BigDecimal(5));
assertNull(inclusiveRange.getExclusiveMaximum());
assertNull(inclusiveRange.getExclusiveMinimum());
final IntegerSchema exclusiveRange = (IntegerSchema) properties.get("exclusiveRange");
assertEquals(exclusiveRange.getMinimum(), new BigDecimal(1));
assertEquals(exclusiveRange.getMaximum(), new BigDecimal(5));
assertEquals(exclusiveRange.getExclusiveMinimum(), Boolean.TRUE);
assertEquals(exclusiveRange.getExclusiveMaximum(), Boolean.TRUE);
final IntegerSchema positiveInfinityRange = (IntegerSchema) properties.get("positiveInfinityRange");
assertEquals(positiveInfinityRange.getMinimum(), new BigDecimal(1.0));
assertNull(positiveInfinityRange.getMaximum());
assertNull(positiveInfinityRange.getExclusiveMaximum());
assertNull(positiveInfinityRange.getExclusiveMinimum());
final IntegerSchema negativeInfinityRange = (IntegerSchema) properties.get("negativeInfinityRange");
assertNull(negativeInfinityRange.getMinimum());
assertEquals(negativeInfinityRange.getMaximum(), new BigDecimal(5.0));
assertNull(negativeInfinityRange.getExclusiveMaximum());
assertNull(negativeInfinityRange.getExclusiveMinimum());
final StringSchema stringValues = (StringSchema) properties.get("stringValues");
assertEquals(stringValues.getEnum(), Arrays.asList("str1", "str2"));
final NumberSchema doubleValues = (NumberSchema) properties.get("doubleValues");
assertEquals(doubleValues.getMinimum(), new BigDecimal("1.0"));
assertEquals(doubleValues.getMaximum(), new BigDecimal("8.0"));
assertEquals(doubleValues.getExclusiveMaximum(), Boolean.TRUE);
assertNull(doubleValues.getExclusiveMinimum());
final IntegerSchema intAllowableValues = (IntegerSchema) properties.get("intAllowableValues");
assertEquals(intAllowableValues.getEnum(), Arrays.asList(1, 2));
final IntegerSchema intAllowableValuesWithNull = (IntegerSchema) properties.get("intAllowableValuesWithNull");
assertEquals(intAllowableValuesWithNull.getEnum(), Arrays.asList(1, 2, null));
}
use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.
the class ModelExampleTest method createModel.
@Test(description = "it should create a model")
public void createModel() {
ObjectSchema model = new ObjectSchema();
model.addProperties("name", new StringSchema().example("Tony"));
model.addProperties("id", new IntegerSchema().example(123));
assertNotNull(model);
}
use of io.swagger.v3.oas.models.media.StringSchema in project swagger-core by swagger-api.
the class ModelResolver method _addEnumProps.
protected void _addEnumProps(Class<?> propClass, Schema property) {
final boolean useIndex = _mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX);
final boolean useToString = _mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
Optional<Method> jsonValueMethod = Arrays.stream(propClass.getMethods()).filter(m -> m.isAnnotationPresent(JsonValue.class)).filter(m -> m.getAnnotation(JsonValue.class).value()).findFirst();
@SuppressWarnings("unchecked") Class<Enum<?>> enumClass = (Class<Enum<?>>) propClass;
Enum<?>[] enumConstants = enumClass.getEnumConstants();
if (enumConstants != null) {
String[] enumValues = _intr.findEnumValues(propClass, enumConstants, new String[enumConstants.length]);
for (Enum<?> en : enumConstants) {
String n;
String enumValue = enumValues[en.ordinal()];
String s = jsonValueMethod.flatMap(m -> ReflectionUtils.safeInvoke(m, en)).map(Object::toString).orElse(null);
if (s != null) {
n = s;
} else if (enumValue != null) {
n = enumValue;
} else if (useIndex) {
n = String.valueOf(en.ordinal());
} else if (useToString) {
n = en.toString();
} else {
n = _intr.findEnumValue(en);
}
if (property instanceof StringSchema) {
StringSchema sp = (StringSchema) property;
sp.addEnumItem(n);
}
}
}
}
Aggregations