Skip to content

Commit 87d62ed

Browse files
committed
fix: reattach enumAsRef for ArraySchema in OAS 3.1
1 parent b98b476 commit 87d62ed

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
540540
.propertyName(annotatedType.getPropertyName())
541541
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
542542
.components(annotatedType.getComponents())
543+
.resolveEnumAsRef(annotatedType.isResolveEnumAsRef())
543544
.parent(annotatedType.getParent()));
544545

545546
if (items == null) {

modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/EnumTest.java

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testEnumGenerics() {
5252
}
5353

5454
@Test
55-
public void testEnumPropertyWithSchemaAnnotation31() {
55+
public void testEnumPropertyWithSchemaAnnotation() {
5656
final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(true);
5757
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
5858

@@ -65,20 +65,6 @@ public void testEnumPropertyWithSchemaAnnotation31() {
6565
assertEquals(enumPropertySchema.getDescription(), "Property description");
6666
}
6767

68-
@Test
69-
public void testEnumPropertyWithSchemaAnnotation30() {
70-
final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(false);
71-
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
72-
73-
final Schema model = context.resolve(new AnnotatedType().type(ClassWithEnumAsRefProperty.class));
74-
assertBasicModelStructure(model, "ClassWithEnumAsRefProperty");
75-
assertPropertyExists(model, "enumWithSchemaProperty");
76-
77-
final Schema enumPropertySchema = (Schema) model.getProperties().get("enumWithSchemaProperty");
78-
assertEnumAsRefProperty(enumPropertySchema, "#/components/schemas/EnumWithSchemaProperty");
79-
80-
}
81-
8268
@Test
8369
public void testEnumPropertyWithGlobalSwitchOnlyOpenApi31() {
8470
ModelResolver.enumsAsRef = true;
@@ -100,6 +86,36 @@ public void testEnumPropertyWithGlobalSwitchOnlyOpenApi31() {
10086
ModelResolver.enumsAsRef = false;
10187
}
10288

89+
@Test
90+
public void testArrayOfEnumWithSchemaAnnotationOpenApi31() {
91+
final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(true);
92+
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
93+
94+
final Schema model = context.resolve(new AnnotatedType().type(ClassWithEnumArray.class));
95+
assertBasicModelStructure(model, "ClassWithEnumArray");
96+
assertPropertyExists(model, "enumArray");
97+
98+
final Schema arrayPropertySchema = (Schema) model.getProperties().get("enumArray");
99+
assertArrayWithEnumRef(arrayPropertySchema);
100+
101+
assertEnumComponentExists(context, ClassWithEnumArray.ArrayEnum.values(), "Enum description");
102+
}
103+
104+
@Test
105+
public void testArrayOfEnumWithSchemaAnnotationOpenApi30() {
106+
final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(false);
107+
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
108+
109+
final Schema model = context.resolve(new AnnotatedType().type(ClassWithEnumArray.class));
110+
assertBasicModelStructure(model, "ClassWithEnumArray");
111+
assertPropertyExists(model, "enumArray");
112+
113+
final Schema arrayPropertySchema = (Schema) model.getProperties().get("enumArray");
114+
assertArrayWithEnumRef(arrayPropertySchema);
115+
116+
assertEnumComponentExists(context, ClassWithEnumArray.ArrayEnum.values(), "Enum description");
117+
}
118+
103119
@Test
104120
public void testControlTestNoRefOpenApi31() {
105121
ModelResolver.enumsAsRef = false;
@@ -199,6 +215,18 @@ private void assertEnumComponentExists(ModelConverterContextImpl context, Enum<?
199215
assertEquals(enumComponent.getDescription(), expectedDescription);
200216
}
201217

218+
private void assertEnumComponentExistsWithDefault(ModelConverterContextImpl context, Enum<?>[] enumValues, String expectedDescription, String expectedDefault) {
219+
assertEnumComponentExists(context, enumValues, expectedDescription);
220+
221+
Map<String, Schema> components = context.getDefinedModels();
222+
Set<String> expected = Arrays.stream(enumValues)
223+
.map(Enum::name)
224+
.collect(Collectors.toCollection(LinkedHashSet::new));
225+
226+
Schema enumComponent = findEnumComponent(components, expected);
227+
assertEquals(enumComponent.getDefault(), expectedDefault);
228+
}
229+
202230
private Schema findEnumComponent(Map<String, Schema> components, Set<String> expectedValues) {
203231
return components.values().stream()
204232
.filter(Objects::nonNull)
@@ -222,6 +250,12 @@ private void assertInlineEnumProperty(Schema propertySchema) {
222250
assertNull(propertySchema.get$ref());
223251
}
224252

253+
private void assertArrayWithEnumRef(Schema arrayPropertySchema) {
254+
assertNotNull(arrayPropertySchema.getItems());
255+
assertNotNull(arrayPropertySchema.getItems().get$ref());
256+
}
257+
258+
225259
public static class ClassWithEnumAsRefProperty {
226260

227261
@io.swagger.v3.oas.annotations.media.Schema(enumAsRef = true, description = "Property description", maximum = "1923234")
@@ -238,6 +272,23 @@ public enum EnumWithSchemaProperty {
238272
}
239273
}
240274

275+
public static class ClassWithEnumArray {
276+
277+
@io.swagger.v3.oas.annotations.media.ArraySchema(schema = @io.swagger.v3.oas.annotations.media.Schema(enumAsRef = true, description = "Property Description"))
278+
public final ArrayEnum[] enumArray;
279+
280+
public ClassWithEnumArray(ArrayEnum[] enumArray) {
281+
this.enumArray = enumArray;
282+
}
283+
284+
@io.swagger.v3.oas.annotations.media.Schema(description = "Enum description")
285+
public enum ArrayEnum {
286+
FIRST,
287+
SECOND,
288+
THIRD
289+
}
290+
}
291+
241292
public static class ClassWithPlainEnum {
242293

243294
@io.swagger.v3.oas.annotations.media.Schema(description = "Plain enum property")

0 commit comments

Comments
 (0)