From 963692c0e0c07b3aa1429eb7060b20c98a036faf Mon Sep 17 00:00:00 2001 From: Carsten Wickner Date: Mon, 11 Nov 2024 22:04:46 +0100 Subject: [PATCH] chore: reduce code complexity --- .../generator/impl/SchemaCleanUpUtils.java | 27 ++++++---- .../impl/SchemaGenerationContextImpl.java | 54 ++++++++++--------- 2 files changed, 45 insertions(+), 36 deletions(-) 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 9eca500d..4d95ba88 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 @@ -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; @@ -644,17 +645,7 @@ private void reduceRedundantAttributesIfPossible(ObjectNode memberSchema, Map reverseTagMap) { - if (schemaNode.has(typeTagName)) { - // explicit type indication is already present - return; - } - List 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 impliedTypes = this.collectImpliedTypes(schemaNode, typeTagName, reverseTagMap); if (impliedTypes.isEmpty()) { return; } @@ -672,6 +663,20 @@ private void addTypeInfoWhereMissing(ObjectNode schemaNode, String typeTagName, } } + private List collectImpliedTypes(ObjectNode schemaNode, String typeTagName, Map 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). * diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaGenerationContextImpl.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaGenerationContextImpl.java index fe17ca75..331ccd2a 100644 --- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaGenerationContextImpl.java +++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/SchemaGenerationContextImpl.java @@ -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 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; } @@ -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 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;