Skip to content

Commit

Permalink
chore: harmonise indentations
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenWickner committed Dec 18, 2023
1 parent 8a9e815 commit 144f1b7
Showing 1 changed file with 65 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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;
}
}

}

0 comments on commit 144f1b7

Please sign in to comment.