|
| 1 | +package io.swagger.v3.core.converting; |
| 2 | + |
| 3 | +import io.swagger.v3.core.converter.AnnotatedType; |
| 4 | +import io.swagger.v3.core.converter.ModelConverter; |
| 5 | +import io.swagger.v3.core.converter.ModelConverterContext; |
| 6 | +import io.swagger.v3.core.util.AnnotationsUtils; |
| 7 | +import io.swagger.v3.oas.models.media.ArraySchema; |
| 8 | +import io.swagger.v3.oas.models.media.JsonSchema; |
| 9 | +import io.swagger.v3.oas.models.media.Schema; |
| 10 | +import org.testng.annotations.Test; |
| 11 | + |
| 12 | +import java.lang.annotation.Annotation; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Iterator; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +import static org.testng.Assert.assertEquals; |
| 19 | +import static org.testng.Assert.assertNotNull; |
| 20 | +import static org.testng.Assert.assertNull; |
| 21 | + |
| 22 | +/** |
| 23 | + * Regression for <a href="https://github.com/swagger-api/swagger-core/issues/5187">gh-5187</a>: |
| 24 | + * when the cycle guard added in #5004 fires, {@code AnnotationsUtils.getArraySchema} |
| 25 | + * is invoked with {@code processSchemaImplementation = false} and used to leave the |
| 26 | + * array's {@code items} unset entirely (degrading the schema to {@code type: array} |
| 27 | + * with no items, or dropping the property when downstream code stripped the empty |
| 28 | + * schema). The fallback should emit a {@code $ref} to the implementation type that |
| 29 | + * is already registered in the {@link ModelConverterContext}. |
| 30 | + */ |
| 31 | +public class Issue5187Test { |
| 32 | + |
| 33 | + @io.swagger.v3.oas.annotations.media.Schema |
| 34 | + static class Inner { |
| 35 | + public String name; |
| 36 | + } |
| 37 | + |
| 38 | + @io.swagger.v3.oas.annotations.media.Schema(name = "RenamedInner") |
| 39 | + static class RenamedInner { |
| 40 | + public String name; |
| 41 | + } |
| 42 | + |
| 43 | + @io.swagger.v3.oas.annotations.media.ArraySchema( |
| 44 | + schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Inner.class)) |
| 45 | + static class HolderUsage { |
| 46 | + } |
| 47 | + |
| 48 | + @io.swagger.v3.oas.annotations.media.ArraySchema( |
| 49 | + schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = RenamedInner.class)) |
| 50 | + static class RenamedHolderUsage { |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void cycleGuard_oas31_emitsRefToRegisteredImplementation() { |
| 55 | + ModelConverterContext context = new StubContext(); |
| 56 | + context.defineModel("Inner", new JsonSchema()); |
| 57 | + |
| 58 | + io.swagger.v3.oas.annotations.media.ArraySchema ann = |
| 59 | + HolderUsage.class.getAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema.class); |
| 60 | + |
| 61 | + Schema existing = new JsonSchema().typesItem("array"); |
| 62 | + Optional<Schema> result = AnnotationsUtils.getArraySchema( |
| 63 | + ann, null, null, /* openapi31 */ true, existing, |
| 64 | + /* processSchemaImplementation */ false, context); |
| 65 | + |
| 66 | + assertNotNull(result.orElse(null), "getArraySchema should produce a schema"); |
| 67 | + Schema items = result.get().getItems(); |
| 68 | + assertNotNull(items, "items must be populated via the cycle-guard fallback"); |
| 69 | + assertEquals(items.get$ref(), "#/components/schemas/Inner"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void cycleGuard_oas30_emitsRefToRegisteredImplementation() { |
| 74 | + ModelConverterContext context = new StubContext(); |
| 75 | + context.defineModel("Inner", new Schema()); |
| 76 | + |
| 77 | + io.swagger.v3.oas.annotations.media.ArraySchema ann = |
| 78 | + HolderUsage.class.getAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema.class); |
| 79 | + |
| 80 | + Optional<Schema> result = AnnotationsUtils.getArraySchema( |
| 81 | + ann, null, null, /* openapi31 */ false, /* existingSchema */ null, |
| 82 | + /* processSchemaImplementation */ false, context); |
| 83 | + |
| 84 | + assertNotNull(result.orElse(null), "getArraySchema should produce a schema"); |
| 85 | + Schema items = result.get().getItems(); |
| 86 | + assertNotNull(items, "items must be populated via the cycle-guard fallback"); |
| 87 | + assertEquals(items.get$ref(), "#/components/schemas/Inner"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void cycleGuard_honoursExplicitSchemaName() { |
| 92 | + ModelConverterContext context = new StubContext(); |
| 93 | + context.defineModel("RenamedInner", new Schema()); |
| 94 | + |
| 95 | + io.swagger.v3.oas.annotations.media.ArraySchema ann = |
| 96 | + RenamedHolderUsage.class.getAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema.class); |
| 97 | + |
| 98 | + Optional<Schema> result = AnnotationsUtils.getArraySchema( |
| 99 | + ann, null, null, false, null, false, context); |
| 100 | + |
| 101 | + Schema items = result.get().getItems(); |
| 102 | + assertNotNull(items); |
| 103 | + assertEquals(items.get$ref(), "#/components/schemas/RenamedInner"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void cycleGuard_doesNotOverwriteExistingItems() { |
| 108 | + ModelConverterContext context = new StubContext(); |
| 109 | + context.defineModel("Inner", new Schema()); |
| 110 | + |
| 111 | + io.swagger.v3.oas.annotations.media.ArraySchema ann = |
| 112 | + HolderUsage.class.getAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema.class); |
| 113 | + |
| 114 | + Schema preExisting = new JsonSchema().typesItem("array"); |
| 115 | + preExisting.setItems(new JsonSchema().$ref("#/components/schemas/SomethingElse")); |
| 116 | + |
| 117 | + Optional<Schema> result = AnnotationsUtils.getArraySchema( |
| 118 | + ann, null, null, true, preExisting, false, context); |
| 119 | + |
| 120 | + // The fallback only runs when items is null; pre-existing items must be preserved. |
| 121 | + assertEquals(result.get().getItems().get$ref(), "#/components/schemas/SomethingElse"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void cycleGuard_doesNothingWhenTypeNotRegistered() { |
| 126 | + ModelConverterContext context = new StubContext(); |
| 127 | + // Note: do not define Inner in the context. |
| 128 | + |
| 129 | + io.swagger.v3.oas.annotations.media.ArraySchema ann = |
| 130 | + HolderUsage.class.getAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema.class); |
| 131 | + |
| 132 | + Optional<Schema> result = AnnotationsUtils.getArraySchema( |
| 133 | + ann, null, null, false, null, false, context); |
| 134 | + |
| 135 | + assertNull(result.get().getItems(), |
| 136 | + "without a registered schema we cannot invent a $ref; leave items unset"); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Minimal {@link ModelConverterContext} that records defined models — enough to |
| 141 | + * exercise the lookup used by the cycle-guard fallback without spinning up a |
| 142 | + * full {@code ModelConverters} chain. |
| 143 | + */ |
| 144 | + private static final class StubContext implements ModelConverterContext { |
| 145 | + private final Map<String, Schema> models = new HashMap<>(); |
| 146 | + |
| 147 | + @Override |
| 148 | + public void defineModel(String name, Schema model) { |
| 149 | + models.put(name, model); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void defineModel(String name, Schema model, AnnotatedType type, String previousName) { |
| 154 | + models.put(name, model); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void defineModel(String name, Schema model, java.lang.reflect.Type type, String previousName) { |
| 159 | + models.put(name, model); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public Map<String, Schema> getDefinedModels() { |
| 164 | + return models; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public Schema resolve(AnnotatedType type) { |
| 169 | + return null; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public Iterator<ModelConverter> getConverters() { |
| 174 | + return java.util.Collections.<ModelConverter>emptyList().iterator(); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments