Skip to content

Create a failing test presenting open api 3.1 - enum property schema enumAsRef error #4931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down