Search in sources :

Example 1 with NumberSchema

use of io.swagger.v3.oas.models.media.NumberSchema in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testAnyOfSchema.

@Test
public void testAnyOfSchema(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0'\n" + "components:\n" + "  schemas:\n" + "    id:\n" + "      anyOf: \n" + "       - type: string\n" + "       - type: number\n";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readContents(yaml, auths, options);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    Schema idSchema = result.getOpenAPI().getComponents().getSchemas().get("id");
    assertTrue(idSchema != null);
    assertTrue(idSchema instanceof ComposedSchema);
    ComposedSchema idCompSchema = (ComposedSchema) idSchema;
    List<Schema> anyOfSchemas = idCompSchema.getAnyOf();
    assertTrue(anyOfSchemas != null);
    assertEquals(anyOfSchemas.size(), 2);
    Schema stringSchema = anyOfSchemas.get(0);
    assertTrue(stringSchema != null);
    assertEquals(stringSchema.getType(), "string");
    Schema numberSchema = anyOfSchemas.get(1);
    assertTrue(numberSchema != null);
    assertEquals(numberSchema.getType(), "number");
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) 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) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 2 with NumberSchema

use of io.swagger.v3.oas.models.media.NumberSchema 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 3 with NumberSchema

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

the class PropertySerializationTest method serializeDoubleProperty.

@Test(description = "it should serialize a DoubleProperty")
public void serializeDoubleProperty() throws IOException {
    final NumberSchema p = new NumberSchema()._default(new BigDecimal("3.14159"));
    p.format("double");
    final String json = "{\"type\":\"number\",\"format\":\"double\",\"default\":3.14159}";
    assertEquals(m.writeValueAsString(p), json);
}
Also used : NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with NumberSchema

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

the class ParameterSerializationTest method testFloatValue.

@Test(description = "should serialize float value")
public void testFloatValue() {
    final QueryParameter param = new QueryParameter();
    param.setSchema(new NumberSchema()._default(new BigDecimal("12.34")).format("float"));
    final String json = "{\"in\":\"query\",\"schema\":{\"type\":\"number\",\"format\":\"float\",\"default\":12.34}}";
    SerializationMatchers.assertEqualsToJson(param, json);
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 5 with NumberSchema

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

the class ParameterSerializationTest method testIncorrectDouble.

@Test(description = "should not serialize incorrect double value")
public void testIncorrectDouble() {
    final QueryParameter param = (QueryParameter) new QueryParameter().required(false);
    Schema schema = new NumberSchema().format("double");
    schema.setDefault("test");
    param.setSchema(schema);
    final String json = "{" + "   \"in\":\"query\"," + "   \"required\":false," + "   \"schema\":{" + "      \"type\":\"number\"," + "       \"format\":\"double\"" + "   }" + "}";
    SerializationMatchers.assertEqualsToJson(param, json);
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Test(org.testng.annotations.Test)

Aggregations

NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)11 StringSchema (io.swagger.v3.oas.models.media.StringSchema)8 Test (org.testng.annotations.Test)8 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)7 Schema (io.swagger.v3.oas.models.media.Schema)7 BigDecimal (java.math.BigDecimal)7 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)4 DateSchema (io.swagger.v3.oas.models.media.DateSchema)4 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)4 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)4 MapSchema (io.swagger.v3.oas.models.media.MapSchema)4 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)4 HashMap (java.util.HashMap)4 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)3 BooleanSchema (io.swagger.v3.oas.models.media.BooleanSchema)3 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)3 Test (org.junit.Test)3 Type (com.github.javaparser.ast.type.Type)1 ResolvedArrayType (com.github.javaparser.resolution.types.ResolvedArrayType)1 ResolvedPrimitiveType (com.github.javaparser.resolution.types.ResolvedPrimitiveType)1