diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java index 98bcb4ddeb..a55805deb6 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java @@ -415,8 +415,11 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context // Store off the ref and add the enum as a top-level model context.defineModel(name, model, annotatedType, null); // Return the model as a ref only property - model = openapi31 ? new JsonSchema() : new Schema(); - model.$ref(Components.COMPONENTS_SCHEMAS_REF + name); + if (openapi31) { + model = new JsonSchema().$ref(Components.COMPONENTS_SCHEMAS_REF + name); + } else { + model = new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + name); + } } if (!isComposedSchema) { if (schemaRefFromAnnotation != null && model != null) { @@ -875,7 +878,26 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context } final BeanDescription propBeanDesc = _mapper.getSerializationConfig().introspect(propType); if (property != null && !propType.isContainerType()) { - if (isObjectSchema(property)) { + // Check if this is an enum property with enumAsRef=true + if (propType.isEnumType() && ctxSchema != null && ctxSchema.enumAsRef()) { + // create a reference for the enum property + String pName = _typeName(propType, propBeanDesc); + if (StringUtils.isNotBlank(property.getName())) { + pName = property.getName(); + } + + // Define the enum model if it's not already defined + if (!context.getDefinedModels().containsKey(pName)) { + context.defineModel(pName, property, new AnnotatedType().type(propType.getRawClass()), null); + } + + // Create a reference to the enum model + if (openapi31) { + property = new JsonSchema().$ref(constructRef(pName)); + } else { + property = new Schema().$ref(constructRef(pName)); + } + } else if (isObjectSchema(property)) { // create a reference for the property String pName = _typeName(propType, propBeanDesc); if (StringUtils.isNotBlank(property.getName())) { diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/EnumTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/EnumTest.java index 72dd08ee12..3aa1b0fde0 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/EnumTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/EnumTest.java @@ -56,6 +56,34 @@ public void testEnumGenerics() { assertNotNull(model.getProperties().get("type")); } + @Test + public void testEnumPropertyWithSchemaAnnotation() { + final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(true); + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); + + final Schema model = context.resolve(new AnnotatedType().type(ClassWithEnumAsRefProperty.class)); + assertNotNull(model); + assertEquals(model.getName(), "ClassWithEnumAsRefProperty"); + assertTrue(model.getProperties().containsKey("enumWithSchemaProperty")); + final Schema enumPropertySchema = (Schema) model.getProperties().get("enumWithSchemaProperty"); + assertNotNull(enumPropertySchema.get$ref()); + } + + public static class ClassWithEnumAsRefProperty { + + @io.swagger.v3.oas.annotations.media.Schema(enumAsRef = true) + public final EnumWithSchemaProperty enumWithSchemaProperty; + + public ClassWithEnumAsRefProperty(EnumWithSchemaProperty enumWithSchemaProperty) { + this.enumWithSchemaProperty = enumWithSchemaProperty; + } + + public enum EnumWithSchemaProperty { + VALUE1, + VALUE2 + } + } + public enum Currency { USA, CANADA }