|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal.openapi.projection; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import io.jooby.Projection; |
| 12 | +import io.swagger.v3.oas.models.Components; |
| 13 | +import io.swagger.v3.oas.models.media.ArraySchema; |
| 14 | +import io.swagger.v3.oas.models.media.ObjectSchema; |
| 15 | +import io.swagger.v3.oas.models.media.Schema; |
| 16 | + |
| 17 | +/** |
| 18 | + * Utility to create a pruned OpenAPI Schema based on a Jooby Projection. |
| 19 | + * |
| 20 | + * @since 4.0.0 |
| 21 | + */ |
| 22 | +public class SchemaPruner { |
| 23 | + |
| 24 | + public static Schema<?> prune( |
| 25 | + Schema<?> original, Projection<?> projection, Components components) { |
| 26 | + // 1. Deep wildcard (e.g., address(*)). Return the original schema/ref untouched. |
| 27 | + if (projection.getChildren().isEmpty()) { |
| 28 | + return original; |
| 29 | + } |
| 30 | + |
| 31 | + // 2. Handle Arrays: Recursively prune the items. |
| 32 | + if (original instanceof ArraySchema) { |
| 33 | + ArraySchema arraySchema = (ArraySchema) original; |
| 34 | + Schema<?> prunedItems = prune(arraySchema.getItems(), projection, components); |
| 35 | + |
| 36 | + ArraySchema newArraySchema = new ArraySchema(); |
| 37 | + copyMetadata(original, newArraySchema); |
| 38 | + newArraySchema.setItems(prunedItems); |
| 39 | + return newArraySchema; |
| 40 | + } |
| 41 | + |
| 42 | + // --- THE CACHE CHECK (Early Exit) --- |
| 43 | + String baseName = getBaseName(original); |
| 44 | + String newComponentName = null; |
| 45 | + |
| 46 | + if (baseName != null && components != null) { |
| 47 | + newComponentName = generateProjectedName(baseName, projection); |
| 48 | + |
| 49 | + // If we already built this exact projection view, reuse it immediately! |
| 50 | + if (components.getSchemas() != null |
| 51 | + && components.getSchemas().containsKey(newComponentName)) { |
| 52 | + return new Schema<>().$ref("#/components/schemas/" + newComponentName); |
| 53 | + } |
| 54 | + } |
| 55 | + // ------------------------------------ |
| 56 | + |
| 57 | + // 3. Resolve the actual properties from the original schema |
| 58 | + Schema<?> actualSchema = resolveSchema(original, components); |
| 59 | + if (actualSchema == null || actualSchema.getProperties() == null) { |
| 60 | + return original; |
| 61 | + } |
| 62 | + |
| 63 | + // 4. Build the new pruned ObjectSchema |
| 64 | + Schema<?> prunedSchema = new ObjectSchema(); |
| 65 | + copyMetadata(actualSchema, prunedSchema); |
| 66 | + Map<String, Schema> originalProps = actualSchema.getProperties(); |
| 67 | + |
| 68 | + for (Map.Entry<String, Projection<?>> entry : projection.getChildren().entrySet()) { |
| 69 | + String propName = entry.getKey(); |
| 70 | + Projection<?> childNode = entry.getValue(); |
| 71 | + Schema<?> originalPropSchema = originalProps.get(propName); |
| 72 | + |
| 73 | + if (originalPropSchema != null) { |
| 74 | + // Recursion naturally handles leaf nodes vs. nested objects |
| 75 | + Schema<?> prunedProp = prune(originalPropSchema, childNode, components); |
| 76 | + prunedSchema.addProperty(propName, prunedProp); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // 5. Componentize! Register the new one and return a $ref. |
| 81 | + if (newComponentName != null && components != null) { |
| 82 | + // Add the fully built pruned schema to the global registry |
| 83 | + components.addSchemas(newComponentName, prunedSchema); |
| 84 | + |
| 85 | + // Return a lightweight $ref pointer to the newly registered component |
| 86 | + return new Schema<>().$ref("#/components/schemas/" + newComponentName); |
| 87 | + } |
| 88 | + |
| 89 | + // Fallback: If it was an anonymous inline object to begin with, just return the inline pruned |
| 90 | + // object. |
| 91 | + return prunedSchema; |
| 92 | + } |
| 93 | + |
| 94 | + private static Schema<?> resolveSchema(Schema<?> schema, Components components) { |
| 95 | + if (schema.get$ref() != null && components != null && components.getSchemas() != null) { |
| 96 | + String refName = schema.get$ref().substring(schema.get$ref().lastIndexOf('/') + 1); |
| 97 | + return components.getSchemas().get(refName); |
| 98 | + } |
| 99 | + return schema; |
| 100 | + } |
| 101 | + |
| 102 | + private static String getBaseName(Schema<?> schema) { |
| 103 | + if (schema.get$ref() != null) { |
| 104 | + return schema.get$ref().substring(schema.get$ref().lastIndexOf('/') + 1); |
| 105 | + } |
| 106 | + return schema.getName(); |
| 107 | + } |
| 108 | + |
| 109 | + private static String generateProjectedName(String baseName, Projection<?> projection) { |
| 110 | + String shortHash = Integer.toString(Math.abs(projection.toView().hashCode()), 36); |
| 111 | + return baseName + "_" + shortHash; |
| 112 | + } |
| 113 | + |
| 114 | + private static void copyMetadata(Schema<?> source, Schema<?> target) { |
| 115 | + target.setName(source.getName()); |
| 116 | + target.setTitle(source.getTitle()); |
| 117 | + target.setDescription(source.getDescription()); |
| 118 | + target.setFormat(source.getFormat()); |
| 119 | + target.setDefault(source.getDefault()); |
| 120 | + if (source.getExample() != null) { |
| 121 | + target.setExample(source.getExample()); |
| 122 | + } |
| 123 | + target.setEnum((List) source.getEnum()); |
| 124 | + target.setRequired(source.getRequired()); |
| 125 | + |
| 126 | + target.setMaximum(source.getMaximum()); |
| 127 | + target.setMinimum(source.getMinimum()); |
| 128 | + target.setMaxLength(source.getMaxLength()); |
| 129 | + target.setMinLength(source.getMinLength()); |
| 130 | + target.setPattern(source.getPattern()); |
| 131 | + target.setMaxItems(source.getMaxItems()); |
| 132 | + target.setMinItems(source.getMinItems()); |
| 133 | + target.setUniqueItems(source.getUniqueItems()); |
| 134 | + |
| 135 | + if (source.getExtensions() != null) { |
| 136 | + source.getExtensions().forEach(target::addExtension); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments