|
| 1 | +package io.swagger.v3.core.resolving; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import io.swagger.v3.core.util.AnnotationsUtils; |
| 5 | +import io.swagger.v3.core.util.Json; |
| 6 | +import io.swagger.v3.core.jackson.ModelResolver; |
| 7 | +import io.swagger.v3.oas.models.media.Schema; |
| 8 | +import org.testng.annotations.BeforeMethod; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import java.lang.reflect.Method; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.LinkedHashMap; |
| 15 | +import java.util.LinkedHashSet; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static org.testng.Assert.assertEquals; |
| 20 | +import static org.testng.Assert.assertFalse; |
| 21 | +import static org.testng.Assert.assertNotNull; |
| 22 | +import static org.testng.Assert.assertNull; |
| 23 | + |
| 24 | +/** |
| 25 | + * Defensive invariant tests related to |
| 26 | + * <a href="https://github.com/swagger-api/swagger-core/issues/5126">#5126</a>. |
| 27 | + * |
| 28 | + * <p>{@link ModelResolver#handleUnwrapped} must not forward null-named property schemas into the |
| 29 | + * outer model's properties list. The later {@code modelProps.put(prop.getName(), prop)} step uses |
| 30 | + * those names as map keys, and a {@code null} key makes Jackson fail when serializing the schema. |
| 31 | + * |
| 32 | + * <p>These tests exercise the private method directly because the reported Spring HATEOAS path has |
| 33 | + * not been reproduced as a swagger-core-only resolver test. The clone-based case demonstrates the |
| 34 | + * concrete intermediate state this guard is intended to tolerate: {@code AnnotationsUtils.clone} |
| 35 | + * keeps the properties-map keys while losing nested {@code Schema.name} values because |
| 36 | + * {@code Schema.getName()} is {@code @JsonIgnore}. |
| 37 | + */ |
| 38 | +public class Ticket5126Test extends SwaggerTestBase { |
| 39 | + |
| 40 | + private ModelResolver modelResolver; |
| 41 | + private Method handleUnwrapped; |
| 42 | + |
| 43 | + @BeforeMethod |
| 44 | + public void setup() throws Exception { |
| 45 | + modelResolver = new ModelResolver(new ObjectMapper()); |
| 46 | + handleUnwrapped = ModelResolver.class.getDeclaredMethod( |
| 47 | + "handleUnwrapped", List.class, Schema.class, String.class, String.class, List.class); |
| 48 | + handleUnwrapped.setAccessible(true); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void noPrefixOrSuffix_restoresNameFromClonedInnerPropertiesMapKey() throws Exception { |
| 53 | + Schema<Object> inner = cloneLosingNestedPropertyNames(namedInnerSchema()); |
| 54 | + |
| 55 | + assertEquals(inner.getProperties().keySet(), new LinkedHashSet<>(Arrays.asList("name", "count"))); |
| 56 | + assertNull(((Schema<?>) inner.getProperties().get("name")).getName()); |
| 57 | + assertNull(((Schema<?>) inner.getProperties().get("count")).getName()); |
| 58 | + |
| 59 | + List<Schema> props = invokeHandleUnwrapped(inner, "", ""); |
| 60 | + |
| 61 | + assertPropertyNames(props, "name", "count"); |
| 62 | + Schema<Object> outer = schemaWithModelProps(props); |
| 63 | + assertFalse(outer.getProperties().containsKey(null)); |
| 64 | + Json.mapper().writeValueAsString(outer); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void noPrefixOrSuffix_restoresNameFromInnerPropertiesMapKeyForNullNameState() throws Exception { |
| 69 | + Schema<Object> inner = innerSchemaWithNullPropertyNames(); |
| 70 | + |
| 71 | + List<Schema> props = invokeHandleUnwrapped(inner, "", ""); |
| 72 | + |
| 73 | + assertPropertyNames(props, "name", "count"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void noPrefixOrSuffix_preservesExistingPropertyName() throws Exception { |
| 78 | + Schema<?> nameSchema = new Schema<>().type("string"); |
| 79 | + nameSchema.setName("schemaName"); |
| 80 | + Schema<Object> inner = new Schema<>(); |
| 81 | + Map<String, Schema> innerProps = new LinkedHashMap<>(); |
| 82 | + innerProps.put("mapName", nameSchema); |
| 83 | + inner.setProperties(innerProps); |
| 84 | + |
| 85 | + List<Schema> props = invokeHandleUnwrapped(inner, "", ""); |
| 86 | + |
| 87 | + assertPropertyNames(props, "schemaName"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void withPrefixAndSuffix_combinesMapKeyWhenClonedInnerNameIsNull() throws Exception { |
| 92 | + Schema<Object> inner = cloneLosingNestedPropertyNames(namedInnerSchema()); |
| 93 | + |
| 94 | + List<Schema> props = invokeHandleUnwrapped(inner, "p_", "_s"); |
| 95 | + |
| 96 | + assertPropertyNames(props, "p_name_s", "p_count_s"); |
| 97 | + Schema<Object> outer = schemaWithModelProps(props); |
| 98 | + assertFalse(outer.getProperties().containsKey(null)); |
| 99 | + Json.mapper().writeValueAsString(outer); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void withPrefixAndSuffix_combinesMapKeyWhenInnerNameIsNull() throws Exception { |
| 104 | + Schema<Object> inner = innerSchemaWithNullPropertyNames(); |
| 105 | + |
| 106 | + List<Schema> props = invokeHandleUnwrapped(inner, "p_", "_s"); |
| 107 | + |
| 108 | + assertPropertyNames(props, "p_name_s", "p_count_s"); |
| 109 | + } |
| 110 | + |
| 111 | + private Schema<Object> namedInnerSchema() { |
| 112 | + Schema<?> nameSchema = new Schema<>().type("string"); |
| 113 | + nameSchema.setName("name"); |
| 114 | + Schema<?> countSchema = new Schema<>().type("integer"); |
| 115 | + countSchema.setName("count"); |
| 116 | + |
| 117 | + Schema<Object> inner = new Schema<>(); |
| 118 | + inner.setName("Inner"); |
| 119 | + Map<String, Schema> innerProps = new LinkedHashMap<>(); |
| 120 | + innerProps.put("name", nameSchema); |
| 121 | + innerProps.put("count", countSchema); |
| 122 | + inner.setProperties(innerProps); |
| 123 | + return inner; |
| 124 | + } |
| 125 | + |
| 126 | + private Schema<Object> innerSchemaWithNullPropertyNames() { |
| 127 | + Schema<?> nameSchema = new Schema<>().type("string"); |
| 128 | + Schema<?> countSchema = new Schema<>().type("integer"); |
| 129 | + Schema<Object> inner = new Schema<>(); |
| 130 | + Map<String, Schema> innerProps = new LinkedHashMap<>(); |
| 131 | + innerProps.put("name", nameSchema); |
| 132 | + innerProps.put("count", countSchema); |
| 133 | + inner.setProperties(innerProps); |
| 134 | + return inner; |
| 135 | + } |
| 136 | + |
| 137 | + private Schema<Object> cloneLosingNestedPropertyNames(Schema<Object> inner) { |
| 138 | + return AnnotationsUtils.clone(inner, false); |
| 139 | + } |
| 140 | + |
| 141 | + private List<Schema> invokeHandleUnwrapped(Schema<Object> inner, String prefix, String suffix) throws Exception { |
| 142 | + List<Schema> props = new ArrayList<>(); |
| 143 | + List<String> requiredProps = new ArrayList<>(); |
| 144 | + handleUnwrapped.invoke(modelResolver, props, inner, prefix, suffix, requiredProps); |
| 145 | + return props; |
| 146 | + } |
| 147 | + |
| 148 | + private Schema<Object> schemaWithModelProps(List<Schema> props) { |
| 149 | + Schema<Object> outer = new Schema<>(); |
| 150 | + Map<String, Schema> modelProps = new LinkedHashMap<>(); |
| 151 | + for (Schema prop : props) { |
| 152 | + modelProps.put(prop.getName(), prop); |
| 153 | + } |
| 154 | + outer.setProperties(modelProps); |
| 155 | + return outer; |
| 156 | + } |
| 157 | + |
| 158 | + private void assertPropertyNames(List<Schema> props, String... expectedNames) { |
| 159 | + assertEquals(props.size(), expectedNames.length); |
| 160 | + LinkedHashSet<String> names = new LinkedHashSet<>(); |
| 161 | + for (Schema p : props) { |
| 162 | + assertNotNull(p.getName(), |
| 163 | + "Each unwrapped property must carry a non-null name to avoid null keys in the outer's properties map"); |
| 164 | + names.add(p.getName()); |
| 165 | + } |
| 166 | + assertEquals(names, new LinkedHashSet<>(Arrays.asList(expectedNames))); |
| 167 | + } |
| 168 | +} |
0 commit comments