io.swagger.v3.oas.models.media.NumberSchema Java Examples

The following examples show how to use io.swagger.v3.oas.models.media.NumberSchema. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Swift5ModelEnumTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a java model with an number enum and a default value")
public void convertNumberDefaultValueTest() {
    final NumberSchema enumSchema = new NumberSchema();
    enumSchema.setEnum(Arrays.asList(new BigDecimal(10), new BigDecimal(100), new BigDecimal(1000)));
    enumSchema.setDefault(new BigDecimal((100)));
    final Schema model = new Schema().type("object").addProperties("name", enumSchema);

    final DefaultCodegen codegen = new Swift5ClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty enumVar = cm.vars.get(0);
    Assert.assertEquals(enumVar.baseName, "name");
    Assert.assertEquals(enumVar.dataType, "Double");
    Assert.assertEquals(enumVar.datatypeWithEnum, "Name");
    Assert.assertEquals(enumVar.name, "name");
    Assert.assertEquals(enumVar.defaultValue, "._100");
    Assert.assertEquals(enumVar.baseType, "Double");
    Assert.assertTrue(enumVar.isEnum);
}
 
Example #2
Source File: ExampleBuilderTest.java    From swagger-inflector with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidExample() throws Exception {
    testInvalidExample( new IntegerSchema(), "asd",
        ExampleBuilder.SAMPLE_INT_PROPERTY_VALUE, 123 );

    testInvalidExample( new IntegerSchema().format("int64"), "asd",
        ExampleBuilder.SAMPLE_LONG_PROPERTY_VALUE, 123 );

    testInvalidExample( new NumberSchema().format("float"), "asd",
        ExampleBuilder.SAMPLE_FLOAT_PROPERTY_VALUE, 2.1f );

    testInvalidExample( new NumberSchema().format("double"), "asd",
        ExampleBuilder.SAMPLE_DOUBLE_PROPERTY_VALUE, 3.1f );

    // base types that don't implement setting a sample value
    testInvalidExample( new NumberSchema(), "asd",
        ExampleBuilder.SAMPLE_DECIMAL_PROPERTY_VALUE );

    testInvalidExample( new IntegerSchema(), "asd",
        ExampleBuilder.SAMPLE_BASE_INTEGER_PROPERTY_VALUE );
}
 
Example #3
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void should_ReturnNotNullableMap_When_GivenTypeIsAMap() {
    ResolvedType resolvedType = mockReferencedTypeOf(Map.class);
    ResolvedReferenceType resolvedReferenceType = resolvedType
            .asReferenceType();
    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> pairs = new LinkedList<>();
    pairs.add(null);
    pairs.add(new Pair<>(null, mockReferencedTypeOf(Number.class)));
    when(resolvedReferenceType.getTypeParametersMap()).thenReturn(pairs);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof MapSchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(
            schema.getAdditionalProperties() instanceof NumberSchema);

    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #4
Source File: Swift4ModelEnumTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a java model with an number enum and a default value")
public void convertNumberDefaultValueTest() {
    final NumberSchema enumSchema = new NumberSchema();
    enumSchema.setEnum(Arrays.asList(new BigDecimal(10), new BigDecimal(100), new BigDecimal(1000)));
    enumSchema.setDefault(new BigDecimal((100)));
    final Schema model = new Schema().type("object").addProperties("name", enumSchema);

    final DefaultCodegen codegen = new Swift4Codegen();
    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.vars.size(), 1);

    final CodegenProperty enumVar = cm.vars.get(0);
    Assert.assertEquals(enumVar.baseName, "name");
    Assert.assertEquals(enumVar.dataType, "Double");
    Assert.assertEquals(enumVar.datatypeWithEnum, "Name");
    Assert.assertEquals(enumVar.name, "name");
    Assert.assertEquals(enumVar.defaultValue, "._100");
    Assert.assertEquals(enumVar.baseType, "Double");
    Assert.assertTrue(enumVar.isEnum);
}
 
Example #5
Source File: ValueCoercionTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertFloatValue() throws Exception {
    List<String> values = Arrays.asList("1");

    Parameter parameter = new QueryParameter().schema(new NumberSchema());
    Object o = utils.cast(values, parameter, tf.constructType(Float.class), null);

    assertTrue(o instanceof Float);
}
 
Example #6
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void testInvalidDoubleEnum() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new NumberSchema();
    schema.setEnum(Arrays.asList(
            new Double(1).toString(),
            new Double(2).toString(),
            new Double(3).toString()));
    parameter.setSchema(schema);

    InputConverter.getInstance().validate(new Double(4), parameter);
}
 
Example #7
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidDoubleEnum() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new NumberSchema();
    schema.setEnum(Arrays.asList(
            new Double(1).toString(),
            new Double(2).toString(),
            new Double(3).toString()));
    parameter.setSchema(schema);

    InputConverter.getInstance().validate(new Double(3), parameter);
}
 
Example #8
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void testInvalidLongEnum() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new NumberSchema();
    schema.setEnum(Arrays.asList("1", "2", "3"));
    parameter.setSchema(schema);

    InputConverter.getInstance().validate(new Long(4), parameter);
}
 
Example #9
Source File: NumericValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidLongEnum() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new NumberSchema();
    schema.setEnum(Arrays.asList("1", "2", "3"));
    parameter.setSchema(schema);

    InputConverter.getInstance().validate(new Long(3), parameter);
}
 
Example #10
Source File: ExampleBuilderTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testXmlFloat() throws Exception {
    NumberSchema sp = new NumberSchema();
    Example ex = ExampleBuilder.fromSchema(sp.format("float"), null);
    String xmlString = new XmlExampleSerializer().serialize(ex);
    assertEqualsIgnoreLineEnding(xmlString, "<?xml version='1.1' encoding='UTF-8'?><float>1.1</float>");
}
 
Example #11
Source File: ExampleBuilderTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testXmlDecimal() throws Exception {
    NumberSchema sp = new NumberSchema();
    Example ex = ExampleBuilder.fromSchema(sp, null);
    String xmlString = new XmlExampleSerializer().serialize(ex);
    assertEqualsIgnoreLineEnding(xmlString, "<?xml version='1.1' encoding='UTF-8'?><decimal>1.5</decimal>");
}
 
Example #12
Source File: ValueCoercionTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ConversionException.class)
public void testConvertInvalidDoubleValue() throws Exception {
    List<String> values = Arrays.asList("abcdefg");

    Parameter parameter = new QueryParameter().schema(new NumberSchema());
    Object o = utils.cast(values, parameter, tf.constructType(Double.class), null);

    assertNull(o);
}
 
Example #13
Source File: ValueCoercionTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertDoubleValue() throws Exception {
    List<String> values = Arrays.asList("1");

    Parameter parameter = new QueryParameter().schema(new NumberSchema());
    Object o = utils.cast(values, parameter, tf.constructType(Double.class), null);

    assertTrue(o instanceof Double);
}
 
Example #14
Source File: ValueCoercionTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ConversionException.class)
public void testConvertInvalidFloatValue() throws Exception {
    List<String> values = Arrays.asList("1;;lkaj;lasjkdfs");

    Parameter parameter = new QueryParameter().schema(new NumberSchema());
    Object o = utils.cast(values, parameter, tf.constructType(Float.class), null);

    assertNull(o);
}
 
Example #15
Source File: ValueCoercionTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ConversionException.class)
public void testConvertInvalidLongValue() throws Exception {
    List<String> values = Arrays.asList("1zzzzz");

    Parameter parameter = new QueryParameter().schema(new NumberSchema());
    Object o = utils.cast(values, parameter, tf.constructType(Long.class), null);

    assertNull(o);
}
 
Example #16
Source File: ValueCoercionTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertLongValue() throws Exception {
    List<String> values = Arrays.asList("1");

    Parameter parameter = new QueryParameter().schema(new NumberSchema());
    Object o = utils.cast(values, parameter, tf.constructType(Long.class), null);

    assertTrue(o instanceof Long);
}
 
Example #17
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableNumberSchema_When_GivenTypeIsANumber() {
    ResolvedType numberType = mockReferencedTypeOf(Number.class);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(numberType);
    Assert.assertTrue(schema instanceof NumberSchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #18
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableNumberSchema_When_GivenTypeIsAPrimitiveInt() {
    ResolvedType numberType = mockPrimitiveTypeOf(
            ResolvedPrimitiveType.INT);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(numberType);
    Assert.assertTrue(schema instanceof NumberSchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #19
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
Schema parseResolvedTypeToSchema(ResolvedType resolvedType) {
    if (resolvedType.isArray()) {
        return createArraySchema(resolvedType);
    }
    if (isNumberType(resolvedType)) {
        return new NumberSchema();
    } else if (isStringType(resolvedType)) {
        return new StringSchema();
    } else if (isCollectionType(resolvedType)) {
        return createCollectionSchema(resolvedType.asReferenceType());
    } else if (isBooleanType(resolvedType)) {
        return new BooleanSchema();
    } else if (isMapType(resolvedType)) {
        return createMapSchema(resolvedType);
    } else if (isDateType(resolvedType)) {
        return new DateSchema();
    } else if (isDateTimeType(resolvedType)) {
        return new DateTimeSchema();
    } else if (isOptionalType(resolvedType)) {
        return createOptionalSchema(resolvedType.asReferenceType());
    } else if (isUnhandledJavaType(resolvedType)) {
        return new ObjectSchema();
    } else if (isTypeOf(resolvedType, Enum.class)) {
        return createEnumTypeSchema(resolvedType);
    }
    return createUserBeanSchema(resolvedType);
}
 
Example #20
Source File: DataGenerator.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
public boolean isSupported(Schema<?> schema) {
    return schema instanceof ArraySchema
            || schema instanceof BooleanSchema
            || schema instanceof FileSchema
            || schema instanceof IntegerSchema
            || schema instanceof MapSchema
            || schema instanceof NumberSchema
            || schema instanceof StringSchema;
}
 
Example #21
Source File: InputModeller.java    From tcases with MIT License 5 votes vote down vote up
/**
 * Returns the enumerated number values defined by the given schema.
 */
private Set<BigDecimal> getNumberEnum( Schema<?> schema)
  {
  return
    schema instanceof NumberSchema
    ? asOrderedSet( ((NumberSchema) schema).getEnum())
    : getNumberEnum( schema.getEnum());
  }
 
Example #22
Source File: OASUtilsTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testDouble() {
  MetaPrimitiveType type = new MetaPrimitiveType();
  type.setName("double");
  Schema schema = OASUtils.transformMetaResourceField(type);
  Assert.assertTrue(schema instanceof NumberSchema);
  Assert.assertEquals("double", schema.getFormat());
}
 
Example #23
Source File: OASUtilsTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testFloat() {
  MetaPrimitiveType type = new MetaPrimitiveType();
  type.setName("float");
  Schema schema = OASUtils.transformMetaResourceField(type);
  Assert.assertTrue(schema instanceof NumberSchema);
  Assert.assertEquals("float", schema.getFormat());
}
 
Example #24
Source File: AbstractEndpointGenerationTest.java    From flow with Apache License 2.0 4 votes vote down vote up
private void assertSchema(Schema actualSchema,
        Class<?> expectedSchemaClass) {
    if (assertSpecificJavaClassSchema(actualSchema, expectedSchemaClass)) {
        return;
    }

    if (actualSchema.get$ref() != null) {
        assertNull(actualSchema.getProperties());
        schemaReferences.add(actualSchema.get$ref());
    } else {
        if (actualSchema instanceof StringSchema) {
            assertTrue(String.class.isAssignableFrom(expectedSchemaClass)
                    || Enum.class.isAssignableFrom(expectedSchemaClass));
        } else if (actualSchema instanceof BooleanSchema) {
            assertTrue((boolean.class.isAssignableFrom(expectedSchemaClass)
                    || Boolean.class
                            .isAssignableFrom(expectedSchemaClass)));
        } else if (actualSchema instanceof NumberSchema) {
            assertTrue(JSON_NUMBER_CLASSES.stream()
                    .anyMatch(jsonNumberClass -> jsonNumberClass
                            .isAssignableFrom(expectedSchemaClass)));
        } else if (actualSchema instanceof ArraySchema) {
            if (expectedSchemaClass.isArray()) {
                assertSchema(((ArraySchema) actualSchema).getItems(),
                        expectedSchemaClass.getComponentType());
            } else {
                assertTrue(Collection.class
                        .isAssignableFrom(expectedSchemaClass));
            }
        } else if (actualSchema instanceof MapSchema) {
            assertTrue(Map.class.isAssignableFrom(expectedSchemaClass));
        } else if (actualSchema instanceof DateTimeSchema) {
            assertTrue(Instant.class.isAssignableFrom(expectedSchemaClass)
                    || LocalDateTime.class
                            .isAssignableFrom(expectedSchemaClass));
        } else if (actualSchema instanceof DateSchema) {
            assertTrue(Date.class.isAssignableFrom(expectedSchemaClass)
                    || LocalDate.class
                            .isAssignableFrom(expectedSchemaClass));
        } else if (actualSchema instanceof ComposedSchema) {
            List<Schema> allOf = ((ComposedSchema) actualSchema).getAllOf();
            if (allOf.size() > 1) {
                // Inherited schema
                for (Schema schema : allOf) {
                    if (expectedSchemaClass.getCanonicalName()
                            .equals(schema.getName())) {
                        assertSchemaProperties(expectedSchemaClass, schema);
                        break;
                    }
                }
            } else {
                // Nullable schema for referring schema object
                assertEquals(1, allOf.size());
                assertEquals(expectedSchemaClass.getCanonicalName(),
                        allOf.get(0).getName());
            }
        } else if (actualSchema instanceof ObjectSchema) {
            assertSchemaProperties(expectedSchemaClass, actualSchema);
        } else {
            throw new AssertionError(
                    String.format("Unknown schema '%s' for class '%s'",
                            actualSchema.getClass(), expectedSchemaClass));
        }
    }
}
 
Example #25
Source File: SerializableParamExtractionTest.java    From swagger-inflector with Apache License 2.0 4 votes vote down vote up
@Test
public void getFloatParameterClassTest() throws Exception {
    JavaType jt = utils.getTypeFromParameter(new QueryParameter().schema(new NumberSchema().format("float")), null);
    assertEquals(jt.getRawClass(), Float.class);
}
 
Example #26
Source File: SerializableParamExtractionTest.java    From swagger-inflector with Apache License 2.0 4 votes vote down vote up
@Test
public void getDoubleParameterClassTest() throws Exception {
    JavaType jt = utils.getTypeFromParameter(new QueryParameter().schema(new NumberSchema().format("double")), null);
    assertEquals(jt.getRawClass(), Double.class);
}
 
Example #27
Source File: SchemaTypeUtil.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public static Schema createSchema(String type, String format) {

        if(INTEGER_TYPE.equals(type)) {
            if(StringUtils.isBlank(format)){
                return new IntegerSchema().format(null);
            }else {
                return new IntegerSchema().format(format);
            }
        }
        else if(NUMBER_TYPE.equals(type)) {
            if (StringUtils.isBlank(format)){
                return new NumberSchema();
            } else {
                return new NumberSchema().format(format);
            }
        }
        else if(BOOLEAN_TYPE.equals(type)) {
            if (StringUtils.isBlank(format)){
                return new BooleanSchema();
            } else {
                return new BooleanSchema().format(format);
            }
        }
        else if(STRING_TYPE.equals(type)) {
            if(BYTE_FORMAT.equals(format)) {
                return new ByteArraySchema();
            }
            else if(BINARY_FORMAT.equals(format)) {
                return new BinarySchema();
            }
            else if(DATE_FORMAT.equals(format)) {
                return new DateSchema();
            }
            else if(DATE_TIME_FORMAT.equals(format)) {
                return new DateTimeSchema();
            }
            else if(PASSWORD_FORMAT.equals(format)) {
                return new PasswordSchema();
            }
            else if(EMAIL_FORMAT.equals(format)) {
                return new EmailSchema();
            }
            else if(UUID_FORMAT.equals(format)) {
                return new UUIDSchema();
            }
            else {
                if (StringUtils.isBlank(format)){
                    return new StringSchema().format(null);
                }else {
                    return new StringSchema().format(format);
                }
            }
        }
        else if(OBJECT_TYPE.equals(type)) {
            return new ObjectSchema();
        }
        else {
            return new Schema();
        }
    }