|
| 1 | +package io.swagger.v3.core.converting; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import io.swagger.v3.core.converter.AnnotatedType; |
| 5 | +import io.swagger.v3.core.converter.ModelConverterContextImpl; |
| 6 | +import io.swagger.v3.core.jackson.ModelResolver; |
| 7 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 8 | +import io.swagger.v3.oas.models.media.ComposedSchema; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import javax.validation.constraints.NotNull; |
| 12 | +import java.lang.reflect.Field; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import static org.testng.Assert.*; |
| 17 | +import static org.testng.AssertJUnit.assertEquals; |
| 18 | + |
| 19 | +public class PolymorphicSubtypePropertyBleedTest { |
| 20 | + |
| 21 | + @Schema( |
| 22 | + description = "base", |
| 23 | + discriminatorProperty = "kind", |
| 24 | + oneOf = { ConcretionA.class } |
| 25 | + ) |
| 26 | + public static class BaseResponse { |
| 27 | + public String sharedField; |
| 28 | + public String kind; |
| 29 | + } |
| 30 | + |
| 31 | + @Schema(description = "subtype A") |
| 32 | + public static class ConcretionA extends BaseResponse { |
| 33 | + public String onlyInA; |
| 34 | + } |
| 35 | + |
| 36 | + public static class Wrapper { |
| 37 | + @NotNull |
| 38 | + public ConcretionA concretionA; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Mimics how swagger-core resolves a bean property: |
| 43 | + * - schemaProperty=true |
| 44 | + * - propertyName set |
| 45 | + * - ctxAnnotations from the backing field (e.g. @NotNull) |
| 46 | + */ |
| 47 | + private io.swagger.v3.oas.models.media.Schema resolveAsBeanProperty( |
| 48 | + Class<?> clazz, |
| 49 | + String propertyName, |
| 50 | + Field backingField, |
| 51 | + ModelConverterContextImpl ctx, |
| 52 | + ModelResolver resolver |
| 53 | + ) { |
| 54 | + AnnotatedType at = new AnnotatedType() |
| 55 | + .type(clazz) |
| 56 | + .schemaProperty(true) |
| 57 | + .propertyName(propertyName) |
| 58 | + .ctxAnnotations(backingField.getAnnotations()); |
| 59 | + return ctx.resolve(at); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Mimics how resolveSubtypes(...) resolves a polymorphic subtype: |
| 64 | + * - NO schemaProperty(true) |
| 65 | + * - NO propertyName |
| 66 | + * - NO ctxAnnotations from some containing field |
| 67 | +
|
| 68 | + */ |
| 69 | + private io.swagger.v3.oas.models.media.Schema resolveAsPolymorphicSubtype( |
| 70 | + Class<?> clazz, |
| 71 | + ModelConverterContextImpl ctx, |
| 72 | + ModelResolver resolver |
| 73 | + ) { |
| 74 | + AnnotatedType at = new AnnotatedType() |
| 75 | + .type(clazz); |
| 76 | + return ctx.resolve(at); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Baseline for comparison: new context, clean standalone schema resolution. |
| 81 | + */ |
| 82 | + private io.swagger.v3.oas.models.media.Schema resolveAsStandaloneSchema( |
| 83 | + Class<?> clazz, |
| 84 | + ModelResolver resolver |
| 85 | + ) { |
| 86 | + ModelConverterContextImpl freshCtx = new ModelConverterContextImpl(resolver); |
| 87 | + AnnotatedType at = new AnnotatedType() |
| 88 | + .type(clazz); |
| 89 | + return freshCtx.resolve(at); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void subtypeResolution_shouldMatchStandalone_andNotBleedFromPropertyContext() throws Exception { |
| 94 | + ObjectMapper mapper = new ObjectMapper(); |
| 95 | + ModelResolver resolver = new ModelResolver(mapper); |
| 96 | + |
| 97 | + ModelConverterContextImpl sharedCtx = new ModelConverterContextImpl(resolver); |
| 98 | + |
| 99 | + // 1. Resolve ConcretionA in the bean property context |
| 100 | + Field wrapperField = Wrapper.class.getDeclaredField("concretionA"); |
| 101 | + io.swagger.v3.oas.models.media.Schema concretionAsPropertySchema = |
| 102 | + resolveAsBeanProperty( |
| 103 | + ConcretionA.class, |
| 104 | + "concretionA", |
| 105 | + wrapperField, |
| 106 | + sharedCtx, |
| 107 | + resolver |
| 108 | + ); |
| 109 | + |
| 110 | + assertNotNull(concretionAsPropertySchema, "bean property resolution returned null"); |
| 111 | + |
| 112 | + // 2. Resolve ConcretionA again, but this time as a polymorphic subtype using the SAME sharedCtx |
| 113 | + io.swagger.v3.oas.models.media.Schema concretionAsPolymorphicSchema = |
| 114 | + resolveAsPolymorphicSubtype( |
| 115 | + ConcretionA.class, |
| 116 | + sharedCtx, |
| 117 | + resolver |
| 118 | + ); |
| 119 | + assertNotNull(concretionAsPolymorphicSchema, "polymorphic subtype resolution returned null"); |
| 120 | + |
| 121 | + // 3. Resolve ConcretionA in a clean context to represent the canonical standalone subtype schema. |
| 122 | + io.swagger.v3.oas.models.media.Schema concretionStandaloneSchema = |
| 123 | + resolveAsStandaloneSchema( |
| 124 | + ConcretionA.class, |
| 125 | + resolver |
| 126 | + ); |
| 127 | + assertNotNull(concretionStandaloneSchema, "standalone subtype resolution returned null"); |
| 128 | + |
| 129 | + // nullable should be consistent between standalone and subtype |
| 130 | + assertEquals( |
| 131 | + concretionStandaloneSchema.getNullable(), |
| 132 | + concretionAsPolymorphicSchema.getNullable() |
| 133 | + ); |
| 134 | + |
| 135 | + // required list should match between standalone and subtype-in-sharedCtx |
| 136 | + List<?> requiredStandalone = concretionStandaloneSchema.getRequired(); |
| 137 | + List<?> requiredPolymorphic = concretionAsPolymorphicSchema.getRequired(); |
| 138 | + if (requiredStandalone == null) requiredStandalone = java.util.Collections.emptyList(); |
| 139 | + if (requiredPolymorphic == null) requiredPolymorphic = java.util.Collections.emptyList(); |
| 140 | + assertEquals( |
| 141 | + requiredStandalone, |
| 142 | + requiredPolymorphic |
| 143 | + ); |
| 144 | + |
| 145 | + // Name should match standalone. We don't want subtype schemas |
| 146 | + assertEquals( |
| 147 | + concretionStandaloneSchema.getName(), |
| 148 | + concretionAsPolymorphicSchema.getName() |
| 149 | + ); |
| 150 | + |
| 151 | + //Properties should still include subtype-specific fields like 'onlyInA'. |
| 152 | + Map<?, ?> propsStandalone = concretionStandaloneSchema.getProperties(); |
| 153 | + Map<?, ?> propsPolymorphic = concretionAsPolymorphicSchema.getProperties(); |
| 154 | + assertNotNull(propsPolymorphic); |
| 155 | + assertTrue(propsPolymorphic.containsKey("onlyInA")); |
| 156 | + assertEquals( |
| 157 | + propsStandalone == null |
| 158 | + ? java.util.Collections.emptySet() |
| 159 | + : propsStandalone.keySet(), |
| 160 | + propsPolymorphic.keySet() |
| 161 | + ); |
| 162 | + |
| 163 | + assertNotSame( |
| 164 | + concretionAsPropertySchema, |
| 165 | + concretionAsPolymorphicSchema |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + @Test |
| 171 | + public void polymorphicSubtype_mustContainSubtypeFragment_notJustBaseRef() { |
| 172 | + ObjectMapper mapper = new ObjectMapper(); |
| 173 | + ModelResolver resolver = new ModelResolver(mapper); |
| 174 | + ModelConverterContextImpl ctx = new ModelConverterContextImpl(resolver); |
| 175 | + |
| 176 | + AnnotatedType subtypeAT = new AnnotatedType() |
| 177 | + .type(ConcretionA.class) |
| 178 | + .schemaProperty(true); |
| 179 | + |
| 180 | + io.swagger.v3.oas.models.media.Schema resolvedSubtypeSchema = ctx.resolve(subtypeAT); |
| 181 | + |
| 182 | + assertTrue(resolvedSubtypeSchema.getProperties().containsKey("onlyInA")); |
| 183 | + } |
| 184 | +} |
0 commit comments