diff --git a/openapi-request-validator-core/src/main/java/com/atlassian/oai/validator/schema/transform/SchemaDefinitionsInjectionTransformer.java b/openapi-request-validator-core/src/main/java/com/atlassian/oai/validator/schema/transform/SchemaDefinitionsInjectionTransformer.java index e331ee10..ad483a87 100644 --- a/openapi-request-validator-core/src/main/java/com/atlassian/oai/validator/schema/transform/SchemaDefinitionsInjectionTransformer.java +++ b/openapi-request-validator-core/src/main/java/com/atlassian/oai/validator/schema/transform/SchemaDefinitionsInjectionTransformer.java @@ -3,13 +3,29 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + /** * Simple transformer than injects schema definitions into the `#/components/schemas` path so that references resolve correctly. + *

+ * Only the definitions the schema can reach are injected: the ones it refers to, and the ones those refer to in turn. + * A compiled schema is cached, so every definition injected here is retained for as long as the cache entry lives. + * Injecting all of them made the retained size grow with (number of schemas validated) x (size of the specification), + * even when the specification was loaded fully resolved and the schema referred to none of them. */ public class SchemaDefinitionsInjectionTransformer extends SchemaTransformer { private static final SchemaDefinitionsInjectionTransformer INSTANCE = new SchemaDefinitionsInjectionTransformer(); + private static final String SCHEMA_REF_PREFIX = "#/components/schemas/"; + private static final String MAPPING_FIELD = "mapping"; + public static SchemaDefinitionsInjectionTransformer getInstance() { return INSTANCE; } @@ -20,6 +36,71 @@ public void apply(final JsonNode schemaObject, final SchemaTransformationContext return; } - ((ObjectNode) schemaObject).putObject(COMPONENTS_FIELD).set(SCHEMAS_FIELD, context.getSchemaDefinitions().deepCopy()); + final JsonNode definitions = context.getSchemaDefinitions(); + final ObjectNode injected = ((ObjectNode) schemaObject).putObject(COMPONENTS_FIELD).putObject(SCHEMAS_FIELD); + if (definitions == null) { + return; + } + final Set referenced = referencedDefinitions(schemaObject, definitions); + // In the order they are declared, so the injected block does not depend on traversal order. + for (final Map.Entry definition : definitions.properties()) { + if (referenced.contains(definition.getKey())) { + injected.set(definition.getKey(), definition.getValue().deepCopy()); + } + } + } + + /** + * The names of the definitions reachable from the given schema, directly or through other definitions. + *

+ * Any text value that points at a definition counts, not only the value of a {@code $ref}, and so does a + * discriminator mapping that names a definition without the path. + */ + private static Set referencedDefinitions(final JsonNode schemaObject, final JsonNode definitions) { + final Set referenced = new HashSet<>(); + final Deque pending = new ArrayDeque<>(); + pending.push(schemaObject); + while (!pending.isEmpty()) { + final JsonNode node = pending.pop(); + if (node.isObject()) { + for (final Map.Entry field : node.properties()) { + if (MAPPING_FIELD.equals(field.getKey()) && field.getValue().isObject()) { + field.getValue().forEach(target -> reference(target.asText(), definitions, referenced, pending)); + } + pending.push(field.getValue()); + } + } else if (node.isArray()) { + node.forEach(pending::push); + } else if (node.isTextual() && node.textValue().startsWith(SCHEMA_REF_PREFIX)) { + reference(definitionName(node.textValue()), definitions, referenced, pending); + } + } + return referenced; + } + + // A reference can point inside a definition, e.g. #/components/schemas/Order/properties/customer + private static String definitionName(final String reference) { + final int end = reference.indexOf('/', SCHEMA_REF_PREFIX.length()); + return end < 0 ? reference.substring(SCHEMA_REF_PREFIX.length()) : reference.substring(SCHEMA_REF_PREFIX.length(), end); + } + + private static void reference(final String name, + final JsonNode definitions, + final Set referenced, + final Deque pending) { + final String key = definitions.has(name) ? name : decoded(name); + final JsonNode definition = definitions.get(key); + if (definition != null && referenced.add(key)) { + pending.push(definition); + } + } + + // A reference is a URI fragment holding a JSON Pointer, so it can percent-encode the name and escape '~' and '/' in it. + private static String decoded(final String name) { + try { + return URLDecoder.decode(name.replace("+", "%2B"), StandardCharsets.UTF_8).replace("~1", "/").replace("~0", "~"); + } catch (final IllegalArgumentException e) { + return name; + } } } diff --git a/openapi-request-validator-core/src/test/java/com/atlassian/oai/validator/schema/transform/SchemaDefinitionsInjectionTransformerTest.java b/openapi-request-validator-core/src/test/java/com/atlassian/oai/validator/schema/transform/SchemaDefinitionsInjectionTransformerTest.java new file mode 100644 index 00000000..63db4cd6 --- /dev/null +++ b/openapi-request-validator-core/src/test/java/com/atlassian/oai/validator/schema/transform/SchemaDefinitionsInjectionTransformerTest.java @@ -0,0 +1,117 @@ +package com.atlassian.oai.validator.schema.transform; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class SchemaDefinitionsInjectionTransformerTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static final String DEFINITIONS = "{" + + "\"Order\":{\"type\":\"object\",\"properties\":{\"customer\":{\"$ref\":\"#/components/schemas/Customer\"}}}," + + "\"Customer\":{\"type\":\"object\",\"properties\":{\"address\":{\"$ref\":\"#/components/schemas/Address\"}}}," + + "\"Address\":{\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\"}}}," + + "\"Folder\":{\"type\":\"object\",\"properties\":{\"children\":{\"type\":\"array\"," + + "\"items\":{\"$ref\":\"#/components/schemas/Folder\"}}}}," + + "\"Cat\":{\"type\":\"object\"}," + + "\"Dog\":{\"type\":\"object\"}," + + "\"Line Item\":{\"type\":\"object\"}," + + "\"Tax/Rate\":{\"type\":\"object\"}," + + "\"Unrelated\":{\"type\":\"object\"}" + + "}"; + + private final SchemaDefinitionsInjectionTransformer classUnderTest = SchemaDefinitionsInjectionTransformer.getInstance(); + + @Test + public void injectsNothing_whenSchemaRefersToNoDefinition() throws Exception { + final ObjectNode schema = schema("{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"}}}"); + + classUnderTest.apply(schema, context()); + + assertEquals(List.of(), injected(schema)); + } + + @Test + public void injectsReferencedDefinitions_andTheOnesTheyReferTo() throws Exception { + final ObjectNode schema = schema("{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/Order\"}}"); + + classUnderTest.apply(schema, context()); + + assertEquals(List.of("Order", "Customer", "Address"), injected(schema)); + } + + @Test + public void injectsSelfReferencingDefinition_once() throws Exception { + final ObjectNode schema = schema("{\"$ref\":\"#/components/schemas/Folder\"}"); + + classUnderTest.apply(schema, context()); + + assertEquals(List.of("Folder"), injected(schema)); + } + + @Test + public void injectsDefinition_whenReferenceGoesIntoIt() throws Exception { + final ObjectNode schema = schema("{\"$ref\":\"#/components/schemas/Order/properties/customer\"}"); + + classUnderTest.apply(schema, context()); + + assertEquals(List.of("Order", "Customer", "Address"), injected(schema)); + } + + @Test + public void injectsDefinition_whenReferenceEncodesItsName() throws Exception { + final ObjectNode schema = schema("{\"type\":\"object\",\"properties\":{" + + "\"item\":{\"$ref\":\"#/components/schemas/Line%20Item\"}," + + "\"rate\":{\"$ref\":\"#/components/schemas/Tax~1Rate\"}}}"); + + classUnderTest.apply(schema, context()); + + assertEquals(List.of("Line Item", "Tax/Rate"), injected(schema)); + } + + @Test + public void injectsDefinitionsNamedByDiscriminatorMapping_withAndWithoutPath() throws Exception { + final ObjectNode schema = schema("{\"oneOf\":[{\"type\":\"object\"},{\"type\":\"object\"}]," + + "\"discriminator\":{\"propertyName\":\"kind\"," + + "\"mapping\":{\"cat\":\"#/components/schemas/Cat\",\"dog\":\"Dog\"}}}"); + + classUnderTest.apply(schema, context()); + + assertEquals(List.of("Cat", "Dog"), injected(schema)); + } + + @Test + public void injectsCopies_soTheSharedDefinitionsAreNeverMutated() throws Exception { + final SchemaTransformationContext context = context(); + final ObjectNode schema = schema("{\"$ref\":\"#/components/schemas/Address\"}"); + + classUnderTest.apply(schema, context); + ((ObjectNode) schema.at("/components/schemas/Address")).put("additionalProperties", false); + + assertTrue(schema.at("/components/schemas/Address").has("additionalProperties")); + assertFalse(context.getSchemaDefinitions().get("Address").has("additionalProperties")); + } + + private static List injected(final JsonNode schema) { + final List names = new ArrayList<>(); + schema.at("/components/schemas").fieldNames().forEachRemaining(names::add); + return names; + } + + private static ObjectNode schema(final String json) throws Exception { + return (ObjectNode) MAPPER.readTree(json); + } + + private static SchemaTransformationContext context() throws Exception { + return SchemaTransformationContext.create().withDefinitions(MAPPER.readTree(DEFINITIONS)).build(); + } +}