Skip to content
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 @@ -59,7 +59,7 @@ public void testExample(Class<? extends SchemaGenerationExampleInterface> exampl
private static String loadResource(String resourcePath) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
try (InputStream inputStream = Objects.requireNonNull(ExampleTest.class.getResourceAsStream(resourcePath));
Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8)) {
while (scanner.hasNext()) {
stringBuilder.append(scanner.nextLine()).append('\n');
}
Expand Down
14 changes: 9 additions & 5 deletions jsonschema-generator-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<role>Provided PRs #116 and #118 (part of initial Swagger2Module)</role>
<role>Provided implementation for #204 (readOnly/writeOnly in JacksonModule)</role>
<role>Provided PR #299 and feedback on final implementation (inheritance of validation constraint annotations)</role>
<role>Provided implementation for PR #503 (avoid duplicates in required array during final clean-ups)</role>
<role>Provided PR #503 (avoid duplicates in required array during final clean-ups)</role>
</roles>
</contributor>
<contributor>
Expand Down Expand Up @@ -137,6 +137,12 @@
<role>Provided PR #487 (support @JacksonAnnotationsInside annotations)</role>
</roles>
</contributor>
<contributor>
<url>https://github.com/hbzhou</url>
<roles>
<role>Provided PR #477 (drop JDK 11 support in favor of JDK 17)</role>
</roles>
</contributor>
<contributor>
<name>Filip Hrisafov</name>
<url>https://github.com/filiphr</url>
Expand All @@ -149,8 +155,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<file.encoding>UTF-8</file.encoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<!-- maven plugins -->
<maven.plugin.version.checkstyle>3.6.0</maven.plugin.version.checkstyle>
<version.checkstyle>12.3.0</version.checkstyle>
Expand Down Expand Up @@ -292,9 +297,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.plugin.version.compiler}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<showDeprecation>true</showDeprecation>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private String deriveFieldName() {
.map(prefix -> methodName.substring(prefix.length()))
.filter(name -> !name.isEmpty())
.findFirst();
if (!possibleFieldName.isPresent()) {
if (possibleFieldName.isEmpty()) {
return methodName + "()";
}
String methodNameWithoutPrefix = possibleFieldName.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public TypeContext(AnnotationConfiguration annotationConfig) {
*/
public TypeContext(AnnotationConfiguration annotationConfig, SchemaGeneratorConfig generatorConfig) {
this(annotationConfig, generatorConfig.shouldDeriveFieldsFromArgumentFreeMethods());
if (annotationConfig instanceof AnnotationConfiguration.StdConfiguration) {
if (annotationConfig instanceof AnnotationConfiguration.StdConfiguration stdConfiguration) {
generatorConfig.getAnnotationInclusionOverrides()
.forEach(((AnnotationConfiguration.StdConfiguration) annotationConfig)::setInclusion);
.forEach(stdConfiguration::setInclusion);
}
}

Expand Down Expand Up @@ -327,8 +327,8 @@ public <A extends Annotation> A getTypeParameterAnnotation(Class<A> annotationCl
* @since 4.30.0
*/
public Stream<Annotation> getTypeParameterAnnotations(AnnotatedType annotatedContainerType, Integer containerItemIndex) {
if (annotatedContainerType instanceof AnnotatedParameterizedType) {
AnnotatedType[] typeArguments = ((AnnotatedParameterizedType) annotatedContainerType).getAnnotatedActualTypeArguments();
if (annotatedContainerType instanceof AnnotatedParameterizedType parameterizedType) {
AnnotatedType[] typeArguments = parameterizedType.getAnnotatedActualTypeArguments();
int itemIndex = containerItemIndex == null ? 0 : containerItemIndex;
if (typeArguments.length > itemIndex) {
return Stream.of(typeArguments[itemIndex].getAnnotations());
Expand Down Expand Up @@ -478,10 +478,10 @@ public String getMethodPropertyArgumentTypeDescription(ResolvedType type) {
public <R> R performActionOnMember(MemberScope<?, ?> member, Function<FieldScope, R> fieldAction,
Function<MethodScope, R> methodAction) {
R result;
if (member instanceof FieldScope) {
result = fieldAction.apply((FieldScope) member);
} else if (member instanceof MethodScope) {
result = methodAction.apply((MethodScope) member);
if (member instanceof FieldScope field) {
result = fieldAction.apply(field);
} else if (member instanceof MethodScope method) {
result = methodAction.apply(method);
} else {
throw new IllegalStateException("Unsupported member scope of type: " + member.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,47 +376,47 @@ public AttributeCollector setEnum(ObjectNode node, Collection<?> enumValues, Sch

private void addRawPropertyValue(ObjectNode node, String propertyName, Object value) {
// need to specifically add simple/primitive values by type
if (value instanceof String) {
if (value instanceof String stringValue) {
// explicit inclusion as string results in wrapping quote symbols
node.put(propertyName, (String) value);
} else if (value instanceof BigDecimal) {
node.put(propertyName, (BigDecimal) value);
} else if (value instanceof BigInteger) {
node.put(propertyName, (BigInteger) value);
} else if (value instanceof Boolean) {
node.put(propertyName, (Boolean) value);
} else if (value instanceof Double) {
node.put(propertyName, (Double) value);
} else if (value instanceof Float) {
node.put(propertyName, (Float) value);
} else if (value instanceof Integer) {
node.put(propertyName, (Integer) value);
node.put(propertyName, stringValue);
} else if (value instanceof BigDecimal decimalValue) {
node.put(propertyName, decimalValue);
} else if (value instanceof BigInteger intValue) {
node.put(propertyName, intValue);
} else if (value instanceof Boolean boolValue) {
node.put(propertyName, boolValue);
} else if (value instanceof Double doubleValue) {
node.put(propertyName, doubleValue);
} else if (value instanceof Float floatValue) {
node.put(propertyName, floatValue);
} else if (value instanceof Integer intValue) {
node.put(propertyName, intValue);
} else {
// everything else is simply forwarded as-is to the JSON Schema, it's up to the configurator to ensure the value's correctness
node.putPOJO(propertyName, value);
}
}

private void addRawArrayItem(ArrayNode node, Object value) {
private void addRawArrayItem(ArrayNode node, Object item) {
// need to specifically add simple/primitive values by type
if (value instanceof String) {
if (item instanceof String stringItem) {
// explicit inclusion as string results in wrapping quote symbols
node.add((String) value);
} else if (value instanceof BigDecimal) {
node.add((BigDecimal) value);
} else if (value instanceof BigInteger) {
node.add((BigInteger) value);
} else if (value instanceof Boolean) {
node.add((Boolean) value);
} else if (value instanceof Double) {
node.add((Double) value);
} else if (value instanceof Float) {
node.add((Float) value);
} else if (value instanceof Integer) {
node.add((Integer) value);
node.add(stringItem);
} else if (item instanceof BigDecimal decimalItem) {
node.add(decimalItem);
} else if (item instanceof BigInteger intItem) {
node.add(intItem);
} else if (item instanceof Boolean boolItem) {
node.add(boolItem);
} else if (item instanceof Double doubleItem) {
node.add(doubleItem);
} else if (item instanceof Float floatItem) {
node.add(floatItem);
} else if (item instanceof Integer intItem) {
node.add(intItem);
} else {
// everything else is simply forwarded as-is to the JSON Schema, it's up to the configurator to ensure the value's correctness
node.addPOJO(value);
node.addPOJO(item);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ private Set<String> getTagNamesSupporting(SchemaKeyword.TagContent contentType)
private void finaliseSchemaParts(List<ObjectNode> schemaNodes, Consumer<ObjectNode> performCleanUpOnSingleSchemaNode) {
List<ObjectNode> nextNodesToCheck = new ArrayList<>(schemaNodes);
Consumer<JsonNode> addNodeToCheck = node -> {
if (node instanceof ObjectNode) {
nextNodesToCheck.add((ObjectNode) node);
if (node instanceof ObjectNode objectNode) {
nextNodesToCheck.add(objectNode);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,11 @@ private void generateArrayDefinition(GenericTypeDetails typeDetails, ObjectNode
}

private JsonNode populateItemMemberSchema(TypeScope targetScope) {
if (targetScope instanceof FieldScope && !((FieldScope) targetScope).isFakeContainerItemScope()) {
return this.populateFieldSchema(((FieldScope) targetScope).asFakeContainerItemScope());
if (targetScope instanceof FieldScope field && !field.isFakeContainerItemScope()) {
return this.populateFieldSchema(field.asFakeContainerItemScope());
}
if (targetScope instanceof MethodScope && !((MethodScope) targetScope).isFakeContainerItemScope()) {
return this.populateMethodSchema(((MethodScope) targetScope).asFakeContainerItemScope());
if (targetScope instanceof MethodScope method && !method.isFakeContainerItemScope()) {
return this.populateMethodSchema(method.asFakeContainerItemScope());
}
ObjectNode arrayItemDefinition = this.generatorConfig.createObjectNode();
this.traverseGenericType(targetScope.getContainerItemType(), arrayItemDefinition);
Expand Down Expand Up @@ -710,9 +710,8 @@ private static boolean canExtendTypeDeclarationToIncludeNull(ObjectNode node, Sc
private static void extendTypeDeclarationToIncludeNull(ObjectNode node, SchemaGeneratorConfig config) {
JsonNode fixedJsonSchemaType = node.get(config.getKeyword(SchemaKeyword.TAG_TYPE));
final String nullTypeName = config.getKeyword(SchemaKeyword.TAG_TYPE_NULL);
if (fixedJsonSchemaType instanceof ArrayNode) {
if (fixedJsonSchemaType instanceof ArrayNode arrayOfTypes) {
// there are already multiple "type" values
ArrayNode arrayOfTypes = (ArrayNode) fixedJsonSchemaType;
// one of the existing "type" values could be null
for (JsonNode arrayEntry : arrayOfTypes) {
if (nullTypeName.equals(arrayEntry.stringValue())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ public SchemaDefinitionNamingStrategy getDefinitionNamingStrategy() {
public <M extends MemberScope<?, ?>> CustomPropertyDefinition getCustomDefinition(M scope, SchemaGenerationContext context,
CustomPropertyDefinitionProvider<M> ignoredDefinitionProvider) {
CustomPropertyDefinition result;
if (scope instanceof FieldScope) {
result = this.getCustomDefinition(this.fieldConfigPart, (FieldScope) scope, context,
if (scope instanceof FieldScope fieldScope) {
result = this.getCustomDefinition(this.fieldConfigPart, fieldScope, context,
(CustomPropertyDefinitionProvider<FieldScope>) ignoredDefinitionProvider);
} else if (scope instanceof MethodScope) {
result = this.getCustomDefinition(this.methodConfigPart, (MethodScope) scope, context,
} else if (scope instanceof MethodScope methodScope) {
result = this.getCustomDefinition(this.methodConfigPart, methodScope, context,
(CustomPropertyDefinitionProvider<MethodScope>) ignoredDefinitionProvider);
} else {
throw new IllegalArgumentException("Unexpected member scope: " + (scope == null ? null : scope.getClass().getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,25 @@ public class SchemaGeneratorAllOfCleanUpTest {
static Stream<Arguments> parametersForTestAllOfCleanUp() {
String differentValueInMainSchema = "{ \"type\":\"object\", \"title\":\"main schema\", \"allOf\":[{ \"title\":\"different title\" }, {}] }";
String differentValueInAllOfPart = "{ \"type\":\"object\", \"allOf\":[{ \"title\":\"title X\" }, { \"title\":\"title Y\" }] }";
String equalIfTagInMainSchema = "{ \"type\":\"object\", \"if\":{ \"const\": 1 }, \"then\":{}, "
+ "\"allOf\":[{ \"if\":{ \"const\": 1 }, \"then\":{}, \"else\": { \"title\": \"otherwise...\" } }, {}] }";
String equalIfTagInAllOfPart = "{ \"type\":\"object\", \"allOf\":[{ \"if\":{ \"const\": 1 }, \"then\":{} }, "
+ "{ \"if\":{ \"const\": 1 }, \"then\":{}, \"else\": { \"title\": \"otherwise...\" } }] }";
String equalIfTagInMainSchema = """
{ "type":"object", "if":{ "const": 1 }, "then":{}, \
"allOf":[{ "if":{ "const": 1 }, "then":{}, "else": { "title": "otherwise..." } }, {}] }\
""";
String equalIfTagInAllOfPart = """
{ "type":"object", "allOf":[{ "if":{ "const": 1 }, "then":{} }, \
{ "if":{ "const": 1 }, "then":{}, "else": { "title": "otherwise..." } }] }\
""";
List<Arguments> testCases = EnumSet.allOf(SchemaVersion.class).stream()
.flatMap(schemaVersion -> Stream.of(
Arguments.of(schemaVersion, differentValueInMainSchema, differentValueInMainSchema),
Arguments.of(schemaVersion, differentValueInAllOfPart, differentValueInAllOfPart),
Arguments.of(schemaVersion, equalIfTagInMainSchema, equalIfTagInMainSchema),
Arguments.of(schemaVersion, equalIfTagInAllOfPart, equalIfTagInAllOfPart),
Arguments.of(schemaVersion,
"{ \"type\": \"object\", \"title\":\"same in all three\", "
+ "\"allOf\": [{ \"title\":\"same in all three\" }, { \"title\":\"same in all three\" }] }",
"""
{ "type": "object", "title":"same in all three", \
"allOf": [{ "title":"same in all three" }, { "title":"same in all three" }] }\
""",
"{ \"type\": \"object\", \"title\":\"same in all three\" }"),
Arguments.of(schemaVersion,
"{ \"type\": \"object\", \"allOf\": [{ \"title\":\"from allOf[0]\" }, { \"description\":\"from allOf[1]\" }] }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,13 @@ public void testCreateStandardDefinitionReferenceForField_withCustomPropertyDefi
public void testCreateStandardDefinition() {
ResolvedType type = this.contextImpl.getTypeContext().resolve(TestClass.class);
ObjectNode result = this.contextImpl.createStandardDefinition(type, null);
Assertions.assertEquals("{\"type\":\"object\",\"properties\":{"
+ "\"booleanField\":{\"allOf\":[{},{\"title\":\"Field Title\"}]},"
+ "\"isBooleanField()\":{\"allOf\":[{},{\"title\":\"Method Title\"}]}},"
+ "\"dependentRequired\":{\"booleanField\":[\"isBooleanField()\"],\"isBooleanField()\":[\"booleanField\"]}," +
"\"description\":\"Type Description\"}",
Assertions.assertEquals("""
{"type":"object","properties":{\
"booleanField":{"allOf":[{},{"title":"Field Title"}]},\
"isBooleanField()":{"allOf":[{},{"title":"Method Title"}]}},\
"dependentRequired":{"booleanField":["isBooleanField()"],"isBooleanField()":["booleanField"]},\
"description":"Type Description"}\
""",
result.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,10 @@ private void addStandardModule(GeneratorModule module, SchemaGeneratorConfigBuil
configBuilder.with(new Swagger2Module());
break;
default:
throw new MojoExecutionException("Error: Module does not have a name in "
+ "['Jackson', 'JakartaValidation', 'JavaxValidation', 'Swagger15', 'Swagger2'] or does not have a custom classname.");
throw new MojoExecutionException("""
Error: Module does not have a name in \
['Jackson', 'JakartaValidation', 'JavaxValidation', 'Swagger15', 'Swagger2'] or does not have a custom classname.\
""");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ public void testPackageName(String scenario) throws Exception {
@Test
public void testFileNamePattern() throws Exception {
File testCaseLocation = new File("src/test/resources/reference-test-cases");
File generationLocation = new File("target/generated-test-sources/SchemaFileName/schemas/"+
"com/github/victools/jsonschema/plugin/maven/testpackage");
File generationLocation = new File("""
target/generated-test-sources/SchemaFileName/schemas/\
com/github/victools/jsonschema/plugin/maven/testpackage\
""");

// Execute the pom
executePom(new File("src/test/resources/reference-test-cases/SchemaFileName-pom.xml"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ protected int getPropertyIndex(MemberScope<?, ?> property) {
List<String> sortedProperties = this.propertyOrderPerDeclaringType
.computeIfAbsent(topMostHierarchyType.getErasedType(), this::getAnnotatedPropertyOrder);
String fieldName;
if (property instanceof MethodScope) {
fieldName = Optional.<MemberScope>ofNullable(((MethodScope) property).findGetterField())
if (property instanceof MethodScope method) {
fieldName = Optional.<MemberScope<?,?>>ofNullable(method.findGetterField())
// since 4.33.1: fall-back on method's property name if no getter can be found
.orElse(property)
.getSchemaPropertyName();
Expand Down
Loading
Loading