From 9a04b4162bdfacbc9ed1e7649196e98a2998fcf3 Mon Sep 17 00:00:00 2001 From: Qi Chen Date: Tue, 3 Sep 2024 11:38:08 -0400 Subject: [PATCH] ENH: respect JsonProperty defaultValue in JsonSchemaConverter (#4889) * ENH: respect JsonProperty defaultValue in JsonSchemaConverter Signed-off-by: George Chen --- .../dataprepper/schemas/JsonSchemaConverter.java | 10 ++++++++++ .../dataprepper/schemas/JsonSchemaConverterTest.java | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/data-prepper-plugin-schema/src/main/java/org/opensearch/dataprepper/schemas/JsonSchemaConverter.java b/data-prepper-plugin-schema/src/main/java/org/opensearch/dataprepper/schemas/JsonSchemaConverter.java index fe08825af4..0985485952 100644 --- a/data-prepper-plugin-schema/src/main/java/org/opensearch/dataprepper/schemas/JsonSchemaConverter.java +++ b/data-prepper-plugin-schema/src/main/java/org/opensearch/dataprepper/schemas/JsonSchemaConverter.java @@ -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; @@ -29,6 +30,7 @@ public ObjectNode convertIntoJsonSchema( loadJsonSchemaGeneratorModules(configBuilder); final SchemaGeneratorConfigPart scopeSchemaGeneratorConfigPart = configBuilder.forFields(); overrideInstanceAttributeWithDeprecated(scopeSchemaGeneratorConfigPart); + resolveDefaultValueFromJsonProperty(scopeSchemaGeneratorConfigPart); final SchemaGeneratorConfig config = configBuilder.build(); final SchemaGenerator generator = new SchemaGenerator(config); @@ -49,4 +51,12 @@ private void overrideInstanceAttributeWithDeprecated( } }); } + + private void resolveDefaultValueFromJsonProperty( + final SchemaGeneratorConfigPart scopeSchemaGeneratorConfigPart) { + scopeSchemaGeneratorConfigPart.withDefaultResolver(field -> { + final JsonProperty annotation = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class); + return annotation == null || annotation.defaultValue().isEmpty() ? null : annotation.defaultValue(); + }); + } } diff --git a/data-prepper-plugin-schema/src/test/java/org/opensearch/dataprepper/schemas/JsonSchemaConverterTest.java b/data-prepper-plugin-schema/src/test/java/org/opensearch/dataprepper/schemas/JsonSchemaConverterTest.java index d5d172f8c0..67cf0ac527 100644 --- a/data-prepper-plugin-schema/src/test/java/org/opensearch/dataprepper/schemas/JsonSchemaConverterTest.java +++ b/data-prepper-plugin-schema/src/test/java/org/opensearch/dataprepper/schemas/JsonSchemaConverterTest.java @@ -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; @@ -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; @@ -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 @@ -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; }