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

The following examples show how to use io.swagger.v3.oas.models.media.DateTimeSchema. 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: 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 #2
Source File: OASUtilsTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testOffsetDateTime() {
  MetaPrimitiveType type = new MetaPrimitiveType();
  type.setName("offsetDateTime");
  Schema schema = OASUtils.transformMetaResourceField(type);
  Assert.assertTrue(schema instanceof DateTimeSchema);
}
 
Example #3
Source File: OASUtilsTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalDateTime() {
  MetaPrimitiveType type = new MetaPrimitiveType();
  type.setName("localDateTime");
  Schema schema = OASUtils.transformMetaResourceField(type);
  Assert.assertTrue(schema instanceof DateTimeSchema);
}
 
Example #4
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 #5
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableDate_When_GivenTypeIsAInstant() {
    ResolvedType resolvedType = mockReferencedTypeOf(Instant.class);
    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

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

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

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

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

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

    assertNull(o);
}
 
Example #8
Source File: StringTypeValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateTimeConversion() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setIn("query");
    parameter.setName("test");
    parameter.setSchema(new DateTimeSchema());

    converter.validate(new DateTime(), parameter);
}
 
Example #9
Source File: StringTypeValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void testInvalidDateTimeConversion() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    parameter.setSchema(new DateTimeSchema());

    converter.validate(new Integer(3), parameter);
}
 
Example #10
Source File: StringTypeValidatorTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidLocalDateConversion() throws Exception {
    QueryParameter parameter = new QueryParameter();
    parameter.setName("test");
    parameter.setSchema(new DateTimeSchema());

    converter.validate(new LocalDate(), parameter);
}
 
Example #11
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeserializeDateTimeString() {
    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" +
            "                  dateTime:\n" +
            "                    $ref: '#/components/schemas/DateTimeString'\n" +
            "components:\n" +
            "  schemas:\n" +
            "    DateTimeString:\n" +
            "      type: string\n" +
            "      format: date-time\n" +
            "      default: 2019-01-01T00:00:00Z\n" +
            "      enum:\n" +
            "        - null\n" +
            "        - Nunh uh\n" +
            "        - 2019-01-01T00:00:00Z\n" +
            "        - 2018-02-02T23:59:59.999-05:00\n" +
            "        - 2017-03-03T11:22:33+06:00\n" +
            "        - 2016-04-04T22:33:44.555Z\n" +
            "";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);

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

    Schema dateTimeModel = resolved.getComponents().getSchemas().get("DateTimeString");
    assertTrue(dateTimeModel instanceof DateTimeSchema);
    List<OffsetDateTime> dateTimeValues = dateTimeModel.getEnum();
    assertEquals(dateTimeValues.size(), 5);
    assertEquals(
      dateTimeValues.get(0),
      null);
    assertEquals(
        dateTimeValues.get(1),
        OffsetDateTime.of(2019,1,1,0,0,0,0, ZoneOffset.UTC));

    assertEquals(
        dateTimeValues.get(2),
        OffsetDateTime.of(2018,2,2,23,59,59,999000000, ZoneOffset.ofHours(-5)));

    assertEquals(
        dateTimeValues.get(3),
        OffsetDateTime.of(2017,3,3,11,22,33,0, ZoneOffset.ofHours(6)));

    assertEquals(
        dateTimeValues.get(4),
        OffsetDateTime.of(2016,4,4,22,33,44,555000000, ZoneOffset.UTC));

    assertEquals(
            dateTimeModel.getDefault(),
            OffsetDateTime.of(2019,1,1,0,0,0,0, ZoneOffset.UTC));

    assertEquals(
      result.getMessages(),
      Arrays.asList( "attribute components.schemas.DateTimeString.enum=`Nunh uh` is not of type `date-time`"));
}
 
Example #12
Source File: DartDioModelTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test(description = "convert a simple php model")
public void simpleModelTest() {
    final Schema model = new Schema()
            .description("a sample model")
            .addProperties("id", new IntegerSchema())
            .addProperties("name", new StringSchema())
            .addProperties("createdAt", new DateTimeSchema())
            .addRequiredItem("id")
            .addRequiredItem("name");
    final DefaultCodegen codegen = new DartDioClientCodegen();
    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(), 3);
    // {{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, "DateTime");
    Assert.assertEquals(property3.dataType, "DateTime");
    Assert.assertEquals(property3.name, "createdAt");
    Assert.assertEquals(property3.defaultValue, "null");
    Assert.assertEquals(property3.baseType, "DateTime");
    Assert.assertFalse(property3.hasMore);
    Assert.assertFalse(property3.required);
    Assert.assertFalse(property3.isContainer);
}
 
Example #13
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 #14
Source File: SerializableParamExtractionTest.java    From swagger-inflector with Apache License 2.0 4 votes vote down vote up
@Test
public void getDateTimeParameterClassTest() throws Exception {
    JavaType jt = utils.getTypeFromParameter(new QueryParameter().schema(new DateTimeSchema()), null);
    assertEquals(jt.getRawClass(), DateTime.class);
}
 
Example #15
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 #16
Source File: DataGenerator.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
public boolean isDateTime(Schema<?> schema) {
    return schema instanceof DateTimeSchema;
}
 
Example #17
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);
}