Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): handle Schema annotation defaultValue field #876

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public R fromSchema(Schema schema, Map<String, Schema> definitions) {
private Optional<T> buildExample(String name, Schema schema, Map<String, Schema> definitions, Set<Schema> visited) {
log.debug("Building example for schema {}", schema);

Optional<T> exampleValue = getExampleValueFromSchemaAnnotation(schema);
Optional<T> exampleValue = getExampleFromSchemaAnnotation(schema);
if (exampleValue.isPresent()) {
return exampleValue;
}
Expand All @@ -88,9 +88,12 @@ private Optional<T> buildExample(String name, Schema schema, Map<String, Schema>
return example;
}

private Optional<T> getExampleValueFromSchemaAnnotation(Schema schema) {
Object exampleValue = schema.getExample();
private Optional<T> getExampleFromSchemaAnnotation(Schema schema) {
return getExampleValueFromSchemaAnnotation(schema, schema.getExample())
.or(() -> getExampleValueFromSchemaAnnotation(schema, schema.getDefault()));
}

private Optional<T> getExampleValueFromSchemaAnnotation(Schema schema, Object exampleValue) {
// schema is a map of properties from a nested object, whose example cannot be inferred
if (exampleValue == null) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ void type_boolean(TestInfo testInfo) throws JsonProcessingException {
assertThat(actualString).isEqualTo("true");
}

@Test
void type_boolean_default_set(TestInfo testInfo) throws JsonProcessingException {
BooleanSchema schema = new BooleanSchema();
schema.setName(testInfo.getDisplayName());
schema.setDefault(Boolean.FALSE);

JsonNode actual = jsonSchemaWalker.fromSchema(schema, emptyMap());
String actualString = jsonMapper.writeValueAsString(actual);

assertThat(actualString).isEqualTo("false");
}

@Test
void type_boolean_example_set(TestInfo testInfo) throws JsonProcessingException {
BooleanSchema schema = new BooleanSchema();
Expand All @@ -124,6 +136,18 @@ void type_integer(TestInfo testInfo) throws JsonProcessingException {
assertThat(actualString).isEqualTo("0");
}

@Test
void type_integer_default_set(TestInfo testInfo) throws JsonProcessingException {
IntegerSchema schema = new IntegerSchema();
schema.setName(testInfo.getDisplayName());
schema.setDefault(Integer.parseInt("123"));

JsonNode actual = jsonSchemaWalker.fromSchema(schema, emptyMap());
String actualString = jsonMapper.writeValueAsString(actual);

assertThat(actualString).isEqualTo("123");
}

@Test
void type_integer_example_set(TestInfo testInfo) throws JsonProcessingException {
IntegerSchema schema = new IntegerSchema();
Expand Down Expand Up @@ -172,6 +196,18 @@ void type_number_format_double(TestInfo testInfo) throws JsonProcessingException
assertThat(actualString).isEqualTo("1.1");
}

@Test
void type_number_default_set(TestInfo testInfo) throws JsonProcessingException {
Schema<BigDecimal> schema = new NumberSchema();
schema.setName(testInfo.getDisplayName());
schema.setDefault(new BigDecimal("123.45"));

JsonNode actual = jsonSchemaWalker.fromSchema(schema, emptyMap());
String actualString = jsonMapper.writeValueAsString(actual);

assertThat(actualString).isEqualTo("123.45");
}

@Test
void type_number_example_set(TestInfo testInfo) throws JsonProcessingException {
Schema<BigDecimal> schema = new NumberSchema();
Expand All @@ -195,6 +231,18 @@ void type_string(TestInfo testInfo) throws JsonProcessingException {
assertThat(actualString).isEqualTo("\"string\"");
}

@Test
void type_string_default_set(TestInfo testInfo) throws JsonProcessingException {
StringSchema schema = new StringSchema();
schema.setName(testInfo.getDisplayName());
schema.setDefault("custom-example-value");

JsonNode actual = jsonSchemaWalker.fromSchema(schema, emptyMap());
String actualString = jsonMapper.writeValueAsString(actual);

assertThat(actualString).isEqualTo("\"custom-example-value\"");
}

@Test
void type_string_example_set(TestInfo testInfo) throws JsonProcessingException {
StringSchema schema = new StringSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ void type_boolean() {
assertThat(actual).isEqualTo("<type_boolean>true</type_boolean>");
}

@Test
void type_boolean_default_set() {
BooleanSchema schema = new BooleanSchema();
schema.setDefault(Boolean.FALSE);
schema.setName("type_boolean_default_set");

String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim();

assertThat(actual).isEqualTo("<type_boolean_default_set>false</type_boolean_default_set>");
}

@Test
void type_boolean_example_set() {
BooleanSchema schema = new BooleanSchema();
Expand All @@ -128,6 +139,17 @@ void type_integer() {
assertThat(actual).isEqualTo("<type_integer>0</type_integer>");
}

@Test
void type_integer_default_set() {
IntegerSchema schema = new IntegerSchema();
schema.setDefault(Integer.parseInt("123"));
schema.setName("type_integer_default_set");

String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim();

assertThat(actual).isEqualTo("<type_integer_default_set>123</type_integer_default_set>");
}

@Test
void type_integer_example_set() {
IntegerSchema schema = new IntegerSchema();
Expand Down Expand Up @@ -172,6 +194,17 @@ void type_number_format_double() {
assertThat(actual).isEqualTo("<type_number_format_double>1.1</type_number_format_double>");
}

@Test
void type_number_default_set() {
Schema<BigDecimal> schema = new NumberSchema();
schema.setDefault(new BigDecimal("123.45"));
schema.setName("type_number_default_set");

String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim();

assertThat(actual).isEqualTo("<type_number_default_set>123.45</type_number_default_set>");
}

@Test
void type_number_example_set() {
Schema<BigDecimal> schema = new NumberSchema();
Expand All @@ -193,6 +226,17 @@ void type_string() {
assertThat(actual).isEqualTo("<type_string>string</type_string>");
}

@Test
void type_string_default_set() {
StringSchema schema = new StringSchema();
schema.setDefault("custom-example-value");
schema.setName("type_string_default_set");

String actual = xmlSchemaWalker.fromSchema(schema, emptyMap()).trim();

assertThat(actual).isEqualTo("<type_string_default_set>custom-example-value</type_string_default_set>");
}

@Test
void type_string_example_set() {
StringSchema schema = new StringSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ void type_boolean(TestInfo testInfo) {
""");
}

@Test
void type_boolean_default_set(TestInfo testInfo) {
BooleanSchema schema = new BooleanSchema();
schema.setName(testInfo.getDisplayName());
schema.setDefault(Boolean.FALSE);

String actualString = jsonSchemaWalker.fromSchema(schema, emptyMap());

assertThat(actualString).isEqualTo("""
false
""");
}

@Test
void type_boolean_example_set(TestInfo testInfo) {
BooleanSchema schema = new BooleanSchema();
Expand All @@ -130,6 +143,19 @@ void type_integer(TestInfo testInfo) {
""");
}

@Test
void type_integer_default_set(TestInfo testInfo) {
IntegerSchema schema = new IntegerSchema();
schema.setName(testInfo.getDisplayName());
schema.setDefault(Integer.parseInt("123"));

String actualString = jsonSchemaWalker.fromSchema(schema, emptyMap());

assertThat(actualString).isEqualTo("""
123
""");
}

@Test
void type_integer_example_set(TestInfo testInfo) {
IntegerSchema schema = new IntegerSchema();
Expand Down Expand Up @@ -182,6 +208,19 @@ void type_number_format_double(TestInfo testInfo) {
""");
}

@Test
void type_number_default_set(TestInfo testInfo) {
Schema<BigDecimal> schema = new NumberSchema();
schema.setName(testInfo.getDisplayName());
schema.setDefault(new BigDecimal("123.45"));

String actualString = jsonSchemaWalker.fromSchema(schema, emptyMap());

assertThat(actualString).isEqualTo("""
123.45
""");
}

@Test
void type_number_example_set(TestInfo testInfo) {
Schema<BigDecimal> schema = new NumberSchema();
Expand All @@ -207,6 +246,19 @@ void type_string(TestInfo testInfo) {
""");
}

@Test
void type_string_default_set(TestInfo testInfo) {
StringSchema schema = new StringSchema();
schema.setName(testInfo.getDisplayName());
schema.setDefault("custom-example-value");

String actualString = jsonSchemaWalker.fromSchema(schema, emptyMap());

assertThat(actualString).isEqualTo("""
"custom-example-value"
""");
}

@Test
void type_string_example_set(TestInfo testInfo) {
StringSchema schema = new StringSchema();
Expand Down