Skip to content

Commit

Permalink
ENH: respect JsonProperty defaultValue in JsonSchemaConverter (#4889)
Browse files Browse the repository at this point in the history
* ENH: respect JsonProperty defaultValue in JsonSchemaConverter

Signed-off-by: George Chen <qchea@amazon.com>
  • Loading branch information
chenqi0805 committed Sep 3, 2024
1 parent 8eef2f6 commit 2c034a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opensearch.dataprepper.schemas;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.victools.jsonschema.generator.FieldScope;
Expand Down Expand Up @@ -29,6 +30,7 @@ public ObjectNode convertIntoJsonSchema(
loadJsonSchemaGeneratorModules(configBuilder);
final SchemaGeneratorConfigPart<FieldScope> scopeSchemaGeneratorConfigPart = configBuilder.forFields();
overrideInstanceAttributeWithDeprecated(scopeSchemaGeneratorConfigPart);
resolveDefaultValueFromJsonProperty(scopeSchemaGeneratorConfigPart);

final SchemaGeneratorConfig config = configBuilder.build();
final SchemaGenerator generator = new SchemaGenerator(config);
Expand All @@ -49,4 +51,12 @@ private void overrideInstanceAttributeWithDeprecated(
}
});
}

private void resolveDefaultValueFromJsonProperty(
final SchemaGeneratorConfigPart<FieldScope> scopeSchemaGeneratorConfigPart) {
scopeSchemaGeneratorConfigPart.withDefaultResolver(field -> {
final JsonProperty annotation = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class);
return annotation == null || annotation.defaultValue().isEmpty() ? null : annotation.defaultValue();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.github.victools.jsonschema.generator.Module;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaVersion;
Expand All @@ -14,6 +15,7 @@
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -30,6 +32,13 @@ void testConvertIntoJsonSchemaWithDefaultModules() throws JsonProcessingExceptio
final ObjectNode jsonSchemaNode = jsonSchemaConverter.convertIntoJsonSchema(
SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON, TestConfig.class);
assertThat(jsonSchemaNode, instanceOf(ObjectNode.class));
final JsonNode propertiesNode = jsonSchemaNode.at("/properties");
assertThat(propertiesNode, instanceOf(ObjectNode.class));
assertThat(propertiesNode.has("testAttributeWithDefaultValue"), is(true));
final JsonNode testAttributeWithDefaultValueNode = propertiesNode.at("/testAttributeWithDefaultValue");
assertThat(testAttributeWithDefaultValueNode, instanceOf(ObjectNode.class));
assertThat(testAttributeWithDefaultValueNode.has("default"), is(true));
assertThat(testAttributeWithDefaultValueNode.get("default"), equalTo(TextNode.valueOf("default_value")));
}

@Test
Expand All @@ -53,6 +62,9 @@ static class TestConfig {
@JsonProperty("custom_test_attribute")
private String testAttributeWithJsonPropertyAnnotation;

@JsonProperty(defaultValue = "default_value")
private String testAttributeWithDefaultValue;

public String getTestAttributeWithGetter() {
return testAttributeWithGetter;
}
Expand Down

0 comments on commit 2c034a0

Please sign in to comment.