Skip to content

Commit

Permalink
Add DateTime field
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhalos committed Feb 3, 2022
1 parent fbbb6d6 commit ddc70aa
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/asyncapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ channels:
- 352
type: "integer"
format: "int32"
exampleLocalDateTime:
title: "exampleLocalDateTime"
examples:
- "2022-01-31T23:20:50.52Z"
- "1985-04-12T15:59:55-08:00"
type: "string"
format: "date-time"
innerExample:
title: "innerExample"
properties:
Expand Down Expand Up @@ -85,6 +92,13 @@ channels:
- 352.01
type: "number"
format: "double"
exampleInstant:
title: "exampleInstant"
examples:
- "2022-01-31T23:20:50.52Z"
- "1985-04-12T15:59:55-08:00"
type: "string"
format: "date-time"
floatingAmount:
title: "floatingAmount"
examples:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lu.greenhalos.j2asyncapi.core.fields;

import java.lang.reflect.Field;

import java.time.Instant;
import java.time.LocalDateTime;

import java.util.List;


/**
* @author Ben Antony - [email protected]
*/
class DateTimeFieldType implements FieldType {

@Override
public List<Class<?>> getAllowedClasses() {

return List.of(LocalDateTime.class, Instant.class);
}


@Override
public List<Object> getExamples(Field field) {

return List.of("2022-01-31T23:20:50.52Z", "1985-04-12T15:59:55-08:00");
}


@Override
public String getType(Field field) {

return "string";
}


@Override
public String getFormat(Field field) {

return "date-time";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class FieldUtil {

private static final List<FieldType> FIELD_TYPES = List.of(new NumberFieldType(), new StringFieldType(),
new BooleanFieldType(), new DecimalNumberFieldType(), new ListFieldType(), new EnumFieldType(),
new DateFieldType());
new DateFieldType(), new DateTimeFieldType());

public static Schema process(Field field) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lu.greenhalos.j2asyncapi.core;

import com.asyncapi.v2.model.schema.Schema;

import org.junit.jupiter.api.Test;

import java.time.Instant;

import java.util.List;


/**
* @author Ben Antony - [email protected]
*/
class InstantTest {

@Test
void testField() {

var fieldSchema = new Schema();
fieldSchema.setTitle("field");
fieldSchema.setType("string");
fieldSchema.setFormat("date-time");
fieldSchema.setExamples(List.of("2022-01-31T23:20:50.52Z", "1985-04-12T15:59:55-08:00"));

FieldTestUtil.assertSchemaOnClass(Example.class, fieldSchema);
}

private static class Example {

private Instant field;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lu.greenhalos.j2asyncapi.core;

import com.asyncapi.v2.model.schema.Schema;

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import java.util.List;


/**
* @author Ben Antony - [email protected]
*/
class LocalDateTimeTest {

@Test
void testField() {

var fieldSchema = new Schema();
fieldSchema.setTitle("field");
fieldSchema.setType("string");
fieldSchema.setFormat("date-time");
fieldSchema.setExamples(List.of("2022-01-31T23:20:50.52Z", "1985-04-12T15:59:55-08:00"));

FieldTestUtil.assertSchemaOnClass(Example.class, fieldSchema);
}

private static class Example {

private LocalDateTime field;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import java.math.BigDecimal;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -112,6 +114,8 @@ private static class Example {
public List<String> listCurrency;
public ExampleEnum exampleEnum;
public LocalDate exampleLocalDate;
public LocalDateTime exampleLocalDateTime;
public Instant exampleInstant;

@AsyncApi.Field(type = Integer.class, examples = { "456565", "4654" }, format = "flapping")
public String fieldAnnotation;
Expand Down

0 comments on commit ddc70aa

Please sign in to comment.