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

The following examples show how to use io.swagger.v3.oas.models.media.DateSchema. 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: StringTypeValidatorTest.java    From swagger-inflector with Apache License 2.0 6 votes vote down vote up
@Test
public void testLocalDateConversionEnum() throws Exception {
    List<String> values = new ArrayList<String>();
    for(int i = 1; i <= 3; i++) {
        String str = "2015-01-0" + i;
        values.add(str);
    }

    QueryParameter parameter = new QueryParameter();
        parameter.setName("test");
        Schema schema = new DateSchema();
        schema.setEnum(values);
        parameter.setSchema(schema);

    converter.validate(new LocalDate("2015-01-02"), parameter);
}
 
Example #2
Source File: StringTypeValidatorTest.java    From swagger-inflector with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void testInvalidLocalDateConversionEnum() throws Exception {
    List<String> values = new ArrayList<String>();
    for(int i = 1; i <= 3; i++) {
        String str = "2015-01-0" + i;
        values.add(str);
    }

    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    Schema schema = new DateSchema();
    schema.setEnum(values);

    parameter.setSchema(schema);

    converter.validate(new LocalDate("2015-01-04"), parameter);
}
 
Example #3
Source File: OpenAPIDeserializer.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes the given string and returns an object applicable to the given schema.
 * Throws a ParseException if no applicable object can be recognized.
 */
private Object getDecodedObject( Schema schema, String objectString) throws ParseException {
    Object object =
        objectString == null?
        null :

        schema.getClass().equals( DateSchema.class)?
        toDate( objectString) :

        schema.getClass().equals( DateTimeSchema.class)?
        toDateTime( objectString) :

        schema.getClass().equals( ByteArraySchema.class)?
        toBytes( objectString) :

        objectString;

    if( object == null && objectString != null) {
        throw new ParseException( objectString, 0);
    }

    return object;
}
 
Example #4
Source File: OASUtilsTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testDate() {
  MetaPrimitiveType type = new MetaPrimitiveType();
  type.setName("date");
  Schema schema = OASUtils.transformMetaResourceField(type);
  Assert.assertTrue(schema instanceof DateSchema);
}
 
Example #5
Source File: OASUtilsTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalDate() {
  MetaPrimitiveType type = new MetaPrimitiveType();
  type.setName("localDate");
  Schema schema = OASUtils.transformMetaResourceField(type);
  Assert.assertTrue(schema instanceof DateSchema);
}
 
Example #6
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 #7
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableDate_When_GivenTypeIsADate() {
    ResolvedType resolvedType = mockReferencedTypeOf(Date.class);
    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof DateSchema);
    Assert.assertNull(schema.getNullable());

    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #8
Source File: ValueCoercionTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertDateValue() throws Exception {
    List<String> values = Arrays.asList("2005-12-31");

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

    assertEquals(o.toString(), "2005-12-31");
    assertTrue(o instanceof LocalDate);
}
 
Example #9
Source File: ValueCoercionTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ConversionException.class)
public void testConvertInvalidDateValue() throws Exception {
    List<String> values = Arrays.asList("Booyah!");

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

    assertNull(o);
}
 
Example #10
Source File: DartDioModelTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test(description = "convert a simple dart-dit model with datelibrary")
public void simpleModelWithTimeMachineTest() {
    final Schema model = new Schema()
        .description("a sample model")
        .addProperties("id", new IntegerSchema())
        .addProperties("name", new StringSchema())
        .addProperties("createdAt", new DateTimeSchema())
        .addProperties("birthDate", new DateSchema())
        .addRequiredItem("id")
        .addRequiredItem("name");

    final DartDioClientCodegen codegen = new DartDioClientCodegen();
    codegen.additionalProperties().put(DartDioClientCodegen.DATE_LIBRARY, "timemachine");
    codegen.setDateLibrary("timemachine");
    codegen.processOpts();

    OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
    codegen.setOpenAPI(openAPI);

    final CodegenModel cm = codegen.fromModel("sample", model);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.description, "a sample model");
    Assert.assertEquals(cm.vars.size(), 4);
    // {{imports}} is not used in template
    //Assert.assertEquals(cm.imports.size(), 1);

    final CodegenProperty property1 = cm.vars.get(0);
    Assert.assertEquals(property1.baseName, "id");
    Assert.assertEquals(property1.dataType, "int");
    Assert.assertEquals(property1.name, "id");
    Assert.assertEquals(property1.defaultValue, "null");
    Assert.assertEquals(property1.baseType, "int");
    Assert.assertTrue(property1.hasMore);
    Assert.assertTrue(property1.required);
    Assert.assertTrue(property1.isPrimitiveType);
    Assert.assertFalse(property1.isContainer);

    final CodegenProperty property2 = cm.vars.get(1);
    Assert.assertEquals(property2.baseName, "name");
    Assert.assertEquals(property2.dataType, "String");
    Assert.assertEquals(property2.name, "name");
    Assert.assertEquals(property2.defaultValue, "null");
    Assert.assertEquals(property2.baseType, "String");
    Assert.assertTrue(property2.hasMore);
    Assert.assertTrue(property2.required);
    Assert.assertTrue(property2.isPrimitiveType);
    Assert.assertFalse(property2.isContainer);

    final CodegenProperty property3 = cm.vars.get(2);
    Assert.assertEquals(property3.baseName, "createdAt");
    Assert.assertEquals(property3.complexType, "OffsetDateTime");
    Assert.assertEquals(property3.dataType, "OffsetDateTime");
    Assert.assertEquals(property3.name, "createdAt");
    Assert.assertEquals(property3.defaultValue, "null");
    Assert.assertEquals(property3.baseType, "OffsetDateTime");
    Assert.assertTrue(property3.hasMore);
    Assert.assertFalse(property3.required);
    Assert.assertFalse(property3.isContainer);

    final CodegenProperty property4 = cm.vars.get(3);
    Assert.assertEquals(property4.baseName, "birthDate");
    Assert.assertEquals(property4.complexType, "OffsetDate");
    Assert.assertEquals(property4.dataType, "OffsetDate");
    Assert.assertEquals(property4.name, "birthDate");
    Assert.assertEquals(property4.defaultValue, "null");
    Assert.assertEquals(property4.baseType, "OffsetDate");
    Assert.assertFalse(property4.hasMore);
    Assert.assertFalse(property4.required);
    Assert.assertFalse(property4.isContainer);
}
 
Example #11
Source File: DataGenerator.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
public boolean isDate(Schema<?> schema) {
    return schema instanceof DateSchema;
}
 
Example #12
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 #13
Source File: SerializableParamExtractionTest.java    From swagger-inflector with Apache License 2.0 4 votes vote down vote up
@Test
public void getDateParameterClassTest() throws Exception {
    JavaType jt = utils.getTypeFromParameter(new QueryParameter().schema(new DateSchema()), null);
    assertEquals(jt.getRawClass(), LocalDate.class);
}
 
Example #14
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();
        }
    }
 
Example #15
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeserializeDateString() {
    String yaml = "openapi: 3.0.0\n" +
            "servers: []\n" +
            "info:\n" +
            "  version: 0.0.0\n" +
            "  title: My Title\n" +
            "paths:\n" +
            "  /persons:\n" +
            "    get:\n" +
            "      description: a test\n" +
            "      responses:\n" +
            "        '200':\n" +
            "          description: Successful response\n" +
            "          content:\n" +
            "            '*/*':\n" +
            "              schema:\n" +
            "                type: object\n" +
            "                properties:\n" +
            "                  date:\n" +
            "                    $ref: '#/components/schemas/DateString'\n" +
            "components:\n" +
            "  schemas:\n" +
            "    DateString:\n" +
            "      type: string\n" +
            "      format: date\n" +
            "      default: 2019-01-01\n" +
            "      enum:\n" +
            "        - 2019-01-01\n" +
            "        - Nope\n" +
            "        - 2018-02-02\n" +
            "        - 2017-03-03\n" +
            "        - null\n" +
            "";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);

    final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();

    Schema dateModel = resolved.getComponents().getSchemas().get("DateString");
    assertTrue(dateModel instanceof DateSchema);
    List<Date> dateValues = dateModel.getEnum();
    assertEquals(dateValues.size(), 4);
    assertEquals(
      dateValues.get(0),
      new Calendar.Builder().setDate( 2019, 0, 1).build().getTime());
    assertEquals(
      dateValues.get(1),
      new Calendar.Builder().setDate( 2018, 1, 2).build().getTime());
    assertEquals(
      dateValues.get(2),
      new Calendar.Builder().setDate( 2017, 2, 3).build().getTime());
    assertEquals(
      dateValues.get(3),
      null);

    assertEquals(
      dateModel.getDefault(),
      new Calendar.Builder().setDate( 2019, 0, 1).build().getTime());

    assertEquals(
      result.getMessages(),
      Arrays.asList( "attribute components.schemas.DateString.enum=`Nope` is not of type `date`"));
}