Skip to content

Commit

Permalink
chore: reduce code complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenWickner committed Nov 11, 2024
1 parent e637b3b commit 963692c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.victools.jsonschema.generator.SchemaKeyword;
import com.github.victools.jsonschema.generator.SchemaVersion;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -644,17 +645,7 @@ private void reduceRedundantAttributesIfPossible(ObjectNode memberSchema, Map<St
*/
private void addTypeInfoWhereMissing(ObjectNode schemaNode, String typeTagName, boolean considerNullType,
Map<String, SchemaKeyword> reverseTagMap) {
if (schemaNode.has(typeTagName)) {
// explicit type indication is already present
return;
}
List<String> impliedTypes = reverseTagMap.entrySet().stream()
.filter(entry -> schemaNode.has(entry.getKey()))
.flatMap(entry -> entry.getValue().getImpliedTypes().stream())
.distinct()
.sorted()
.map(SchemaKeyword.SchemaType::getSchemaKeywordValue)
.collect(Collectors.toList());
List<String> impliedTypes = this.collectImpliedTypes(schemaNode, typeTagName, reverseTagMap);
if (impliedTypes.isEmpty()) {
return;
}
Expand All @@ -672,6 +663,20 @@ private void addTypeInfoWhereMissing(ObjectNode schemaNode, String typeTagName,
}
}

private List<String> collectImpliedTypes(ObjectNode schemaNode, String typeTagName, Map<String, SchemaKeyword> reverseTagMap) {
if (schemaNode.has(typeTagName)) {
// explicit type indication is already present
return Collections.emptyList();
}
return reverseTagMap.entrySet().stream()
.filter(entry -> schemaNode.has(entry.getKey()))
.flatMap(entry -> entry.getValue().getImpliedTypes().stream())
.distinct()
.sorted()
.map(SchemaKeyword.SchemaType::getSchemaKeywordValue)
.collect(Collectors.toList());
}

/**
* Replace characters in the given definition key that are deemed incompatible within a URI (as expected by JSON Schema).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,31 +690,7 @@ static ObjectNode makeNullable(ObjectNode node, SchemaGeneratorConfig config) {
// given node is a simple schema, we can adjust its "type" attribute
SchemaGenerationContextImpl.extendTypeDeclarationToIncludeNull(node, config);
} else {
// cannot be sure what is specified in those other schema parts, instead simply create an anyOf wrapper
ObjectNode nullSchema = config.createObjectNode()
.put(config.getKeyword(SchemaKeyword.TAG_TYPE), config.getKeyword(SchemaKeyword.TAG_TYPE_NULL));
String anyOfTagName = config.getKeyword(SchemaKeyword.TAG_ANYOF);
// reduce likelihood of nested duplicate null schema
JsonNode existingAnyOf = node.get(anyOfTagName);
if (existingAnyOf instanceof ArrayNode) {
Iterator<JsonNode> anyOfIterator = existingAnyOf.iterator();
while (anyOfIterator.hasNext()) {
if (nullSchema.equals(anyOfIterator.next())) {
// the existing anyOf array contains a duplicate null schema, remove it
anyOfIterator.remove();
// unlikely that there are multiple
break;
}
}
}
ArrayNode newAnyOf = config.createArrayNode()
// one option in the anyOf should be null
.add(nullSchema)
// the other option is the given (assumed to be) not-nullable node
.add(config.createObjectNode().setAll(node));
// replace all existing (and already copied properties with the anyOf wrapper
node.removeAll();
node.set(anyOfTagName, newAnyOf);
SchemaGenerationContextImpl.addAnyOfNullSchema(node, config);
}
return node;
}
Expand Down Expand Up @@ -756,6 +732,34 @@ private static void extendTypeDeclarationToIncludeNull(ObjectNode node, SchemaGe
// if no "type" is specified, null is allowed already
}

private static void addAnyOfNullSchema(ObjectNode node, SchemaGeneratorConfig config) {
// cannot be sure what is specified in those other schema parts, instead simply create an anyOf wrapper
ObjectNode nullSchema = config.createObjectNode()
.put(config.getKeyword(SchemaKeyword.TAG_TYPE), config.getKeyword(SchemaKeyword.TAG_TYPE_NULL));
String anyOfTagName = config.getKeyword(SchemaKeyword.TAG_ANYOF);
// reduce likelihood of nested duplicate null schema
JsonNode existingAnyOf = node.get(anyOfTagName);
if (existingAnyOf instanceof ArrayNode) {
Iterator<JsonNode> anyOfIterator = existingAnyOf.iterator();
while (anyOfIterator.hasNext()) {
if (nullSchema.equals(anyOfIterator.next())) {
// the existing anyOf array contains a duplicate null schema, remove it
anyOfIterator.remove();
// unlikely that there are multiple
break;
}
}
}
ArrayNode newAnyOf = config.createArrayNode()
// one option in the anyOf should be null
.add(nullSchema)
// the other option is the given (assumed to be) not-nullable node
.add(config.createObjectNode().setAll(node));
// replace all existing (and already copied properties with the anyOf wrapper
node.removeAll();
node.set(anyOfTagName, newAnyOf);
}

private static class GenericTypeDetails {
private final TypeScope scope;
private final boolean nullable;
Expand Down

0 comments on commit 963692c

Please sign in to comment.