From 179a9d53ca18350d85b9e3c45cbdab76b6b2f113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20G=C3=B3mez-S=C3=A1nchez?= <7352559+magicDGS@users.noreply.github.com> Date: Mon, 18 Dec 2023 20:50:32 +0100 Subject: [PATCH 1/5] fix: JsonPropertyOrder with child annotations (#423) --- .../module/jackson/JsonPropertySorter.java | 6 +- .../JsonPropertyOrderIntegrationTest.java | 100 ++++++++++++++++++ ...d-annotations-integration-test-result.json | 18 ++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertyOrderIntegrationTest.java create mode 100644 jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/jsonpropertyorder-method-with-child-annotations-integration-test-result.json diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java index 2da2a4b4..a38f64ae 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java @@ -73,10 +73,12 @@ protected int getPropertyIndex(MemberScope property) { HierarchicType topMostHierarchyType = property.getDeclaringTypeMembers().allTypesAndOverrides().get(0); List sortedProperties = this.propertyOrderPerDeclaringType .computeIfAbsent(topMostHierarchyType.getErasedType(), this::getAnnotatedPropertyOrder); - String fieldName; + String fieldName = null; if (property instanceof MethodScope) { fieldName = Optional.ofNullable(((MethodScope) property).findGetterField()).map(MemberScope::getSchemaPropertyName).orElse(null); - } else { + } + // also in case that the MethodScope fails, the filename might be provided by the property.getSchemaPropertyName() + if (fieldName == null) { fieldName = property.getSchemaPropertyName(); } int propertyIndex = sortedProperties.indexOf(fieldName); diff --git a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertyOrderIntegrationTest.java b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertyOrderIntegrationTest.java new file mode 100644 index 00000000..ce63c125 --- /dev/null +++ b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertyOrderIntegrationTest.java @@ -0,0 +1,100 @@ +package com.github.victools.jsonschema.module.jackson; + +import java.io.IOException; +import java.io.InputStream; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyDescription; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.victools.jsonschema.generator.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class JsonPropertyOrderIntegrationTest { + + private static ObjectMapper OBJECT_MAPPER; + + @BeforeAll + static void beforeAll() { + OBJECT_MAPPER = new ObjectMapper(); + } + + @Test + public void testJsonPropertyOrderWithChildAnnotations() throws Exception { + // given + final SchemaGenerator generator = new SchemaGenerator(testConfig()); + // when + JsonNode result = generator.generateSchema(JsonPropertyOrderIntegrationTest.TestObject.class); + // then + final String actualSchemaAsString = OBJECT_MAPPER.writerWithDefaultPrettyPrinter() + .writeValueAsString(result); + final String expectedSchemaAsString = loadTestJson("jsonpropertyorder-method-with-child-annotations-integration-test-result.json"); + Assertions.assertEquals(expectedSchemaAsString, actualSchemaAsString); + } + + private static SchemaGeneratorConfig testConfig() { + final JacksonModule module = new JacksonModule( + JacksonOption.RESPECT_JSONPROPERTY_ORDER, + JacksonOption.INCLUDE_ONLY_JSONPROPERTY_ANNOTATED_METHODS + ); + return new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON) + .with( + Option.NONSTATIC_NONVOID_NONGETTER_METHODS, + Option.FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS + ) + .with(module) + .build(); + } + + private static String loadTestJson(String resourcePath) throws IOException { + try (InputStream inputStream = JsonPropertyOrderIntegrationTest.class.getResourceAsStream(resourcePath)) { + return OBJECT_MAPPER.readTree(inputStream).toPrettyString(); + } + } + + @JsonPropertyOrder({ "1_first", "3_third" }) + public static class TestContainer { + + @JsonPropertyDescription("My string description") + @JsonProperty("1_first") + private String firstString; + @JsonPropertyDescription("My integer description") + @JsonProperty("3_third") + private Integer thirdInteger; + } + + @JsonPropertyOrder({ "1_first", "2_second" , "3_third"}) + public static class TestObject { + + private TestContainer container; + private String secondString; + + @JsonIgnore + public TestContainer getContainer() { + return container; + } + + @JsonPropertyDescription("My string description") + @JsonProperty("1_first") + public String getString() { + return container.firstString; + } + + @JsonPropertyDescription("My second string description") + @JsonProperty("2_second") + public String getString2() { + return secondString; + } + + @JsonPropertyDescription("My integer description") + @JsonProperty("3_third") + public Integer getInteger() { + return container.thirdInteger; + } + } + +} diff --git a/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/jsonpropertyorder-method-with-child-annotations-integration-test-result.json b/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/jsonpropertyorder-method-with-child-annotations-integration-test-result.json new file mode 100644 index 00000000..4b139771 --- /dev/null +++ b/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/jsonpropertyorder-method-with-child-annotations-integration-test-result.json @@ -0,0 +1,18 @@ +{ + "$schema" : "https://json-schema.org/draft/2019-09/schema", + "type" : "object", + "properties" : { + "1_first" : { + "type" : "string", + "description" : "My string description" + }, + "2_second" : { + "type" : "string", + "description" : "My second string description" + }, + "3_third" : { + "type" : "integer", + "description" : "My integer description" + } + } +} \ No newline at end of file From 08f6453e93cd553c2e165dbb8ca19f379cb0a236 Mon Sep 17 00:00:00 2001 From: Carsten Wickner Date: Mon, 18 Dec 2023 23:48:37 +0100 Subject: [PATCH 2/5] chore: clean up test --- .../generator/impl/SchemaCleanUpUtils.java | 20 +++- .../module/jackson/JsonPropertySorter.java | 11 +- .../module/jackson/IntegrationTest.java | 1 - .../JsonPropertyOrderIntegrationTest.java | 100 ---------------- .../JsonPropertySorterIntegrationTest.java | 108 ++++++++++++++++++ ...d-annotations-integration-test-result.json | 18 --- 6 files changed, 129 insertions(+), 129 deletions(-) delete mode 100644 jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertyOrderIntegrationTest.java create mode 100644 jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorterIntegrationTest.java delete mode 100644 jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/jsonpropertyorder-method-with-child-annotations-integration-test-result.json diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java index 9248049d..c877e2b9 100644 --- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java +++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaCleanUpUtils.java @@ -345,7 +345,7 @@ private Set collectTextItemsFromArrayNode(JsonNode arrayNode) { * @return supplier of the successfully merged schemas (into a new node) or {@code null} if merging the given nodes is not easily possible */ private Supplier mergeSchemas(ObjectNode mainNodeIncludingAllOf, List nodes, Map reverseKeywordMap) { - if (nodes.stream().anyMatch(part -> !(part instanceof ObjectNode) && !(part.isBoolean() && part.asBoolean()))) { + if (nodes.stream().anyMatch(part -> part.isBoolean() && !part.asBoolean())) { return null; } List parts = nodes.stream() @@ -354,9 +354,7 @@ private Supplier mergeSchemas(ObjectNode mainNodeIncludingAllOf, Lis .collect(Collectors.toList()); // collect all defined attributes from the separate parts and check whether there are incompatible differences - Map> fieldsFromAllParts = parts.stream() - .flatMap(part -> StreamSupport.stream(((Iterable>) part::fields).spliterator(), false)) - .collect(Collectors.groupingBy(Map.Entry::getKey, LinkedHashMap::new, Collectors.mapping(Map.Entry::getValue, Collectors.toList()))); + Map> fieldsFromAllParts = this.getFieldsFromAllParts(parts); if (this.shouldSkipMergingAllOf(mainNodeIncludingAllOf, parts, fieldsFromAllParts)) { return null; } @@ -384,13 +382,25 @@ private Supplier mergeSchemas(ObjectNode mainNodeIncludingAllOf, Lis }; } + /** + * Collect all defined attributes from the separate parts. + * + * @param parts entries of the {@link SchemaKeyword#TAG_ALLOF} array to consider + * @return flattened collection of all attributes in the given parts + */ + private Map> getFieldsFromAllParts(List parts) { + return parts.stream() + .flatMap(part -> StreamSupport.stream(((Iterable>) part::fields).spliterator(), false)) + .collect(Collectors.groupingBy(Map.Entry::getKey, LinkedHashMap::new, Collectors.mapping(Map.Entry::getValue, Collectors.toList()))); + } + /** * Check whether the merging of the given node and it's allOf entries should be skipped due to a {@link SchemaKeyword#TAG_REF} being present. * Drafts 6 and 7 would ignore any other attributes besides the {@link SchemaKeyword#TAG_REF}. * * @param mainNode the main node containing an {@link SchemaKeyword#TAG_ALLOF} array (maybe {@code null}) * @param parts entries of the {@link SchemaKeyword#TAG_ALLOF} array to consider - * @param fieldsFromAllParts flatten collection of all attributes in the given parts + * @param fieldsFromAllParts flattened collection of all attributes in the given parts * @return whether to block merging of the given {@link SchemaKeyword#TAG_ALLOF} candidate */ private boolean shouldSkipMergingAllOf(ObjectNode mainNode, List parts, Map> fieldsFromAllParts) { diff --git a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java index a38f64ae..3d6c79e5 100644 --- a/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java +++ b/jsonschema-module-jackson/src/main/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorter.java @@ -73,12 +73,13 @@ protected int getPropertyIndex(MemberScope property) { HierarchicType topMostHierarchyType = property.getDeclaringTypeMembers().allTypesAndOverrides().get(0); List sortedProperties = this.propertyOrderPerDeclaringType .computeIfAbsent(topMostHierarchyType.getErasedType(), this::getAnnotatedPropertyOrder); - String fieldName = null; + String fieldName; if (property instanceof MethodScope) { - fieldName = Optional.ofNullable(((MethodScope) property).findGetterField()).map(MemberScope::getSchemaPropertyName).orElse(null); - } - // also in case that the MethodScope fails, the filename might be provided by the property.getSchemaPropertyName() - if (fieldName == null) { + fieldName = Optional.ofNullable(((MethodScope) property).findGetterField()) + // since 4.33.1: fall-back on method's property name if no getter can be found + .orElse(property) + .getSchemaPropertyName(); + } else { fieldName = property.getSchemaPropertyName(); } int propertyIndex = sortedProperties.indexOf(fieldName); diff --git a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IntegrationTest.java b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IntegrationTest.java index d58d730e..c446ad7d 100644 --- a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IntegrationTest.java +++ b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/IntegrationTest.java @@ -75,7 +75,6 @@ private static String loadResource(String resourcePath) throws IOException { } } return stringBuilder.toString(); - } @JsonClassDescription("test description") diff --git a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertyOrderIntegrationTest.java b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertyOrderIntegrationTest.java deleted file mode 100644 index ce63c125..00000000 --- a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertyOrderIntegrationTest.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.github.victools.jsonschema.module.jackson; - -import java.io.IOException; -import java.io.InputStream; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyDescription; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.victools.jsonschema.generator.*; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -public class JsonPropertyOrderIntegrationTest { - - private static ObjectMapper OBJECT_MAPPER; - - @BeforeAll - static void beforeAll() { - OBJECT_MAPPER = new ObjectMapper(); - } - - @Test - public void testJsonPropertyOrderWithChildAnnotations() throws Exception { - // given - final SchemaGenerator generator = new SchemaGenerator(testConfig()); - // when - JsonNode result = generator.generateSchema(JsonPropertyOrderIntegrationTest.TestObject.class); - // then - final String actualSchemaAsString = OBJECT_MAPPER.writerWithDefaultPrettyPrinter() - .writeValueAsString(result); - final String expectedSchemaAsString = loadTestJson("jsonpropertyorder-method-with-child-annotations-integration-test-result.json"); - Assertions.assertEquals(expectedSchemaAsString, actualSchemaAsString); - } - - private static SchemaGeneratorConfig testConfig() { - final JacksonModule module = new JacksonModule( - JacksonOption.RESPECT_JSONPROPERTY_ORDER, - JacksonOption.INCLUDE_ONLY_JSONPROPERTY_ANNOTATED_METHODS - ); - return new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON) - .with( - Option.NONSTATIC_NONVOID_NONGETTER_METHODS, - Option.FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS - ) - .with(module) - .build(); - } - - private static String loadTestJson(String resourcePath) throws IOException { - try (InputStream inputStream = JsonPropertyOrderIntegrationTest.class.getResourceAsStream(resourcePath)) { - return OBJECT_MAPPER.readTree(inputStream).toPrettyString(); - } - } - - @JsonPropertyOrder({ "1_first", "3_third" }) - public static class TestContainer { - - @JsonPropertyDescription("My string description") - @JsonProperty("1_first") - private String firstString; - @JsonPropertyDescription("My integer description") - @JsonProperty("3_third") - private Integer thirdInteger; - } - - @JsonPropertyOrder({ "1_first", "2_second" , "3_third"}) - public static class TestObject { - - private TestContainer container; - private String secondString; - - @JsonIgnore - public TestContainer getContainer() { - return container; - } - - @JsonPropertyDescription("My string description") - @JsonProperty("1_first") - public String getString() { - return container.firstString; - } - - @JsonPropertyDescription("My second string description") - @JsonProperty("2_second") - public String getString2() { - return secondString; - } - - @JsonPropertyDescription("My integer description") - @JsonProperty("3_third") - public Integer getInteger() { - return container.thirdInteger; - } - } - -} diff --git a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorterIntegrationTest.java b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorterIntegrationTest.java new file mode 100644 index 00000000..7bb02616 --- /dev/null +++ b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorterIntegrationTest.java @@ -0,0 +1,108 @@ +/* + * Copyright 2023 VicTools. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.victools.jsonschema.module.jackson; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.JsonNode; +import com.github.victools.jsonschema.generator.Option; +import com.github.victools.jsonschema.generator.OptionPreset; +import com.github.victools.jsonschema.generator.SchemaGenerator; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; +import com.github.victools.jsonschema.generator.SchemaKeyword; +import com.github.victools.jsonschema.generator.SchemaVersion; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +public class JsonPropertySorterIntegrationTest { + + @ParameterizedTest + @CsvSource({ + "TestObject, one two three", + "TestContainer, one two three" + }) + public void testJsonPropertyOrderWithChildAnnotations(String targetTypeName, String expectedFieldOrder) throws Exception { + JacksonModule module = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_ORDER, + JacksonOption.INCLUDE_ONLY_JSONPROPERTY_ANNOTATED_METHODS); + SchemaGeneratorConfig config = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON) + .with(Option.NONSTATIC_NONVOID_NONGETTER_METHODS, Option.FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS) + .with(module) + .build(); + Class targetType = Stream.of(JsonPropertySorterIntegrationTest.class.getDeclaredClasses()) + .filter(testType -> testType.getSimpleName().equals(targetTypeName)) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + SchemaGenerator generator = new SchemaGenerator(config); + JsonNode result = generator.generateSchema(targetType); + + ObjectNode properties = (ObjectNode) result.get(config.getKeyword(SchemaKeyword.TAG_PROPERTIES)); + List resultPropertyNames = new ArrayList<>(); + properties.fieldNames().forEachRemaining(resultPropertyNames::add); + Assertions.assertEquals(Arrays.asList(expectedFieldOrder.split(" ")), resultPropertyNames); + } + + @JsonPropertyOrder({ "one", "two", "three" }) + public static class TestContainer { + + @JsonProperty("three") + private Integer thirdInteger; + @JsonProperty("one") + private String firstString; + + @JsonProperty("two") + public boolean getSecondValue() { + return true; + } + + } + + @JsonPropertyOrder({ "one", "two" , "three"}) + public static class TestObject { + + private TestContainer container; + private String secondString; + + @JsonIgnore + public TestContainer getContainer() { + return this.container; + } + + @JsonProperty("three") + public Integer getInteger() { + return this.container.thirdInteger; + } + + @JsonProperty("two") + public String getSecondString() { + return this.secondString; + } + + @JsonProperty("one") + public String getString() { + return this.container.firstString; + } + } + +} diff --git a/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/jsonpropertyorder-method-with-child-annotations-integration-test-result.json b/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/jsonpropertyorder-method-with-child-annotations-integration-test-result.json deleted file mode 100644 index 4b139771..00000000 --- a/jsonschema-module-jackson/src/test/resources/com/github/victools/jsonschema/module/jackson/jsonpropertyorder-method-with-child-annotations-integration-test-result.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "$schema" : "https://json-schema.org/draft/2019-09/schema", - "type" : "object", - "properties" : { - "1_first" : { - "type" : "string", - "description" : "My string description" - }, - "2_second" : { - "type" : "string", - "description" : "My second string description" - }, - "3_third" : { - "type" : "integer", - "description" : "My integer description" - } - } -} \ No newline at end of file From f2be1ad88ffc58b09856337bd1ce25b4169b969b Mon Sep 17 00:00:00 2001 From: Carsten Wickner Date: Mon, 18 Dec 2023 23:50:36 +0100 Subject: [PATCH 3/5] chore(docs): update CHANGELOG --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79db5aab..12e6fe40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -*-* +### `jsonschema-module-jackson` +#### Fixed +- Respect `@JsonPropertyOrder` also for properties derived from non-getter methods ## [4.33.0] - 2023-11-23 ### `jsonschema-generator` From 8a9e8150854d260dc014a5f17aebfb8203b73929 Mon Sep 17 00:00:00 2001 From: Carsten Wickner Date: Tue, 19 Dec 2023 00:12:13 +0100 Subject: [PATCH 4/5] chore(docs): update contributors list --- jsonschema-generator-parent/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/jsonschema-generator-parent/pom.xml b/jsonschema-generator-parent/pom.xml index 165141c5..79f11c3a 100644 --- a/jsonschema-generator-parent/pom.xml +++ b/jsonschema-generator-parent/pom.xml @@ -118,6 +118,7 @@ https://github.com/magicDGS Provided PR #300 (introducing support for standard "format" values via Option) + Provided PR #423 (fixing Jackson property order handling) From 144f1b76657f844d314b514d5d79d424dfd8b91b Mon Sep 17 00:00:00 2001 From: Carsten Wickner Date: Tue, 19 Dec 2023 00:24:20 +0100 Subject: [PATCH 5/5] chore: harmonise indentations --- .../JsonPropertySorterIntegrationTest.java | 131 +++++++++--------- 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorterIntegrationTest.java b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorterIntegrationTest.java index 7bb02616..e0cfdbfe 100644 --- a/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorterIntegrationTest.java +++ b/jsonschema-module-jackson/src/test/java/com/github/victools/jsonschema/module/jackson/JsonPropertySorterIntegrationTest.java @@ -38,71 +38,70 @@ public class JsonPropertySorterIntegrationTest { - @ParameterizedTest - @CsvSource({ - "TestObject, one two three", - "TestContainer, one two three" - }) - public void testJsonPropertyOrderWithChildAnnotations(String targetTypeName, String expectedFieldOrder) throws Exception { - JacksonModule module = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_ORDER, - JacksonOption.INCLUDE_ONLY_JSONPROPERTY_ANNOTATED_METHODS); - SchemaGeneratorConfig config = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON) - .with(Option.NONSTATIC_NONVOID_NONGETTER_METHODS, Option.FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS) - .with(module) - .build(); - Class targetType = Stream.of(JsonPropertySorterIntegrationTest.class.getDeclaredClasses()) - .filter(testType -> testType.getSimpleName().equals(targetTypeName)) - .findFirst() - .orElseThrow(IllegalArgumentException::new); - SchemaGenerator generator = new SchemaGenerator(config); - JsonNode result = generator.generateSchema(targetType); - - ObjectNode properties = (ObjectNode) result.get(config.getKeyword(SchemaKeyword.TAG_PROPERTIES)); - List resultPropertyNames = new ArrayList<>(); - properties.fieldNames().forEachRemaining(resultPropertyNames::add); - Assertions.assertEquals(Arrays.asList(expectedFieldOrder.split(" ")), resultPropertyNames); - } - - @JsonPropertyOrder({ "one", "two", "three" }) - public static class TestContainer { - - @JsonProperty("three") - private Integer thirdInteger; - @JsonProperty("one") - private String firstString; - - @JsonProperty("two") - public boolean getSecondValue() { - return true; - } - - } - - @JsonPropertyOrder({ "one", "two" , "three"}) - public static class TestObject { - - private TestContainer container; - private String secondString; - - @JsonIgnore - public TestContainer getContainer() { - return this.container; - } - - @JsonProperty("three") - public Integer getInteger() { - return this.container.thirdInteger; - } - - @JsonProperty("two") - public String getSecondString() { - return this.secondString; - } - - @JsonProperty("one") - public String getString() { - return this.container.firstString; - } - } + @ParameterizedTest + @CsvSource({ + "TestObject, one two three", + "TestContainer, one two three" + }) + public void testJsonPropertyOrderWithChildAnnotations(String targetTypeName, String expectedFieldOrder) throws Exception { + JacksonModule module = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_ORDER, + JacksonOption.INCLUDE_ONLY_JSONPROPERTY_ANNOTATED_METHODS); + SchemaGeneratorConfig config = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON) + .with(Option.NONSTATIC_NONVOID_NONGETTER_METHODS, Option.FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS) + .with(module) + .build(); + Class targetType = Stream.of(JsonPropertySorterIntegrationTest.class.getDeclaredClasses()) + .filter(testType -> testType.getSimpleName().equals(targetTypeName)) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + SchemaGenerator generator = new SchemaGenerator(config); + JsonNode result = generator.generateSchema(targetType); + + ObjectNode properties = (ObjectNode) result.get(config.getKeyword(SchemaKeyword.TAG_PROPERTIES)); + List resultPropertyNames = new ArrayList<>(); + properties.fieldNames().forEachRemaining(resultPropertyNames::add); + Assertions.assertEquals(Arrays.asList(expectedFieldOrder.split(" ")), resultPropertyNames); + } + + @JsonPropertyOrder({"one", "two", "three"}) + public static class TestContainer { + + @JsonProperty("three") + private Integer thirdInteger; + @JsonProperty("one") + private String firstString; + + @JsonProperty("two") + public boolean getSecondValue() { + return true; + } + } + + @JsonPropertyOrder({"one", "two", "three"}) + public static class TestObject { + + private TestContainer container; + private String secondString; + + @JsonIgnore + public TestContainer getContainer() { + return this.container; + } + + @JsonProperty("three") + public Integer getInteger() { + return this.container.thirdInteger; + } + + @JsonProperty("two") + public String getSecondString() { + return this.secondString; + } + + @JsonProperty("one") + public String getString() { + return this.container.firstString; + } + } }