Search in sources :

Example 21 with StringSchema

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);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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)

Example 22 with 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");
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) Test(org.testng.annotations.Test)

Example 23 with StringSchema

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));
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 24 with StringSchema

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);
}
Also used : ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 25 with StringSchema

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);
            }
        }
    }
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) UUIDSchema(io.swagger.v3.oas.models.media.UUIDSchema) Size(javax.validation.constraints.Size) Arrays(java.util.Arrays) JsonView(com.fasterxml.jackson.annotation.JsonView) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) DiscriminatorMapping(io.swagger.v3.oas.annotations.media.DiscriminatorMapping) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) XmlElementRef(javax.xml.bind.annotation.XmlElementRef) ObjectMapperFactory(io.swagger.v3.core.util.ObjectMapperFactory) PropertyMetadata(com.fasterxml.jackson.databind.PropertyMetadata) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) POJOPropertyBuilder(com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder) AnnotationsUtils(io.swagger.v3.core.util.AnnotationsUtils) PatternProperties(io.swagger.v3.oas.annotations.media.PatternProperties) BigDecimal(java.math.BigDecimal) JsonValue(com.fasterxml.jackson.annotation.JsonValue) RefUtils.constructRef(io.swagger.v3.core.util.RefUtils.constructRef) Map(java.util.Map) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) Max(javax.validation.constraints.Max) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) Method(java.lang.reflect.Method) XmlAccessorType(javax.xml.bind.annotation.XmlAccessorType) Hidden(io.swagger.v3.oas.annotations.Hidden) Collection(java.util.Collection) Set(java.util.Set) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Min(javax.validation.constraints.Min) Collectors(java.util.stream.Collectors) AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) XML(io.swagger.v3.oas.models.media.XML) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) InvocationTargetException(java.lang.reflect.InvocationTargetException) JsonIdentityReference(com.fasterxml.jackson.annotation.JsonIdentityReference) List(java.util.List) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) StringSchema(io.swagger.v3.oas.models.media.StringSchema) XmlElementRefs(javax.xml.bind.annotation.XmlElementRefs) Type(java.lang.reflect.Type) SchemaProperties(io.swagger.v3.oas.annotations.media.SchemaProperties) JsonUnwrapped(com.fasterxml.jackson.annotation.JsonUnwrapped) Pattern(javax.validation.constraints.Pattern) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) PatternProperty(io.swagger.v3.oas.annotations.media.PatternProperty) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) JAXB_DEFAULT(io.swagger.v3.core.jackson.JAXBAnnotationsHelper.JAXB_DEFAULT) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) AnnotationMap(com.fasterxml.jackson.databind.introspect.AnnotationMap) ModelConverter(io.swagger.v3.core.converter.ModelConverter) Json(io.swagger.v3.core.util.Json) OptionalUtils(io.swagger.v3.core.util.OptionalUtils) DecimalMin(javax.validation.constraints.DecimalMin) HashMap(java.util.HashMap) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Annotations(com.fasterxml.jackson.databind.util.Annotations) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) PrimitiveType(io.swagger.v3.core.util.PrimitiveType) Schema(io.swagger.v3.oas.models.media.Schema) JavaType(com.fasterxml.jackson.databind.JavaType) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) Discriminator(io.swagger.v3.oas.models.media.Discriminator) Logger(org.slf4j.Logger) JsonIdentityInfo(com.fasterxml.jackson.annotation.JsonIdentityInfo) Iterator(java.util.Iterator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MapSchema(io.swagger.v3.oas.models.media.MapSchema) IOException(java.io.IOException) ObjectIdGenerator(com.fasterxml.jackson.annotation.ObjectIdGenerator) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) SchemaProperty(io.swagger.v3.oas.annotations.media.SchemaProperty) Constants(io.swagger.v3.core.util.Constants) ReflectionUtils(io.swagger.v3.core.util.ReflectionUtils) Annotated(com.fasterxml.jackson.databind.introspect.Annotated) ModelConverterContext(io.swagger.v3.core.converter.ModelConverterContext) DecimalMax(javax.validation.constraints.DecimalMax) Components(io.swagger.v3.oas.models.Components) NumberUtils(org.apache.commons.lang3.math.NumberUtils) ObjectIdGenerators(com.fasterxml.jackson.annotation.ObjectIdGenerators) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) Collections(java.util.Collections) XmlElement(javax.xml.bind.annotation.XmlElement) JsonValue(com.fasterxml.jackson.annotation.JsonValue) Method(java.lang.reflect.Method) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) StringSchema(io.swagger.v3.oas.models.media.StringSchema)

Aggregations

StringSchema (io.swagger.v3.oas.models.media.StringSchema)88 Test (org.testng.annotations.Test)79 Schema (io.swagger.v3.oas.models.media.Schema)69 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)53 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)52 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 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)19 MapSchema (io.swagger.v3.oas.models.media.MapSchema)19 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)19 DateSchema (io.swagger.v3.oas.models.media.DateSchema)17 Components (io.swagger.v3.oas.models.Components)15 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)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