TimestampMapper implements JsonSchemaMapper { @Override public Schema.Builder updateSchema(Shape shape, Schema.Builder builder, JsonSchemaConfig config) { String format = extractTimestampFormat(shape, config); if (format == null) { return builder; } switch (format) { case TimestampFormatTrait.DATE_TIME: return builder.type("string").format(format); case TimestampFormatTrait.HTTP_DATE: return builder.type("string"); case TimestampFormatTrait.EPOCH_SECONDS: return builder.format(null).type("number"); default: return format.startsWith("epoch-") ? builder.type("number") : builder.type("string"); } }  @Override byte getOrder(); @Override Schema.Builder updateSchema(Shape shape, Schema.Builder builder, JsonSchemaConfig config);  }
@Test public void convertsDateTimeToStringAndDateTimeFormat() { TimestampShape shape = TimestampShape.builder() .id("smithy.example#Timestamp") .addTrait(new TimestampFormatTrait(TimestampFormatTrait.DATE_TIME)) .build(); Schema schema = new TimestampMapper().updateSchema(shape, Schema.builder(), new JsonSchemaConfig()).build(); assertThat(schema.getType().get(), equalTo("string")); assertThat(schema.getFormat().get(), equalTo("date-time")); }
@Test public void convertsHttpDateToString() { TimestampShape shape = TimestampShape.builder() .id("smithy.example#Timestamp") .addTrait(new TimestampFormatTrait(TimestampFormatTrait.HTTP_DATE)) .build(); Schema schema = new TimestampMapper().updateSchema(shape, Schema.builder(), new JsonSchemaConfig()).build(); assertThat(schema.getType().get(), equalTo("string")); assertFalse(schema.getFormat().isPresent()); }
@Test public void convertsEpochSecondsToNumber() { TimestampShape shape = TimestampShape.builder() .id("smithy.example#Timestamp") .addTrait(new TimestampFormatTrait(TimestampFormatTrait.EPOCH_SECONDS)) .build(); Schema schema = new TimestampMapper().updateSchema(shape, Schema.builder(), new JsonSchemaConfig()).build(); assertThat(schema.getType().get(), equalTo("number")); assertFalse(schema.getFormat().isPresent()); }
@Test public void convertsEpochUnknownToNumber() { TimestampShape shape = TimestampShape.builder() .id("smithy.example#Timestamp") .addTrait(new TimestampFormatTrait("epoch-millis")) .build(); Schema schema = new TimestampMapper().updateSchema(shape, Schema.builder(), new JsonSchemaConfig()).build(); assertThat(schema.getType().get(), equalTo("number")); assertFalse(schema.getFormat().isPresent()); }
@Test public void supportsDefaultTimestampFormat() { TimestampShape shape = TimestampShape.builder().id("smithy.example#Timestamp").build(); JsonSchemaConfig config = new JsonSchemaConfig(); config.setDefaultTimestampFormat(TimestampFormatTrait.Format.DATE_TIME); Schema schema = new TimestampMapper().updateSchema(shape, Schema.builder(), config).build(); assertThat(schema.getType().get(), equalTo("string")); assertThat(schema.getFormat().get(), equalTo("date-time")); }
@Test public void assumesDateTimeStringWhenNoFormatOrDefaultPresent() { TimestampShape shape = TimestampShape.builder() .id("smithy.example#Timestamp") .build(); Schema schema = new TimestampMapper().updateSchema(shape, Schema.builder(), new JsonSchemaConfig()).build(); assertThat(schema.getType().get(), equalTo("string")); assertThat(schema.getFormat().get(), equalTo("date-time")); }