Skip to content
Merged
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 @@ -751,10 +751,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context

Annotation[] ctxAnnotation31 = null;
Schema.SchemaResolution resolvedSchemaResolution = AnnotationsUtils.resolveSchemaResolution(this.schemaResolution, ctxSchema);
if (
Schema.SchemaResolution.ALL_OF.equals(resolvedSchemaResolution) ||
Schema.SchemaResolution.ALL_OF_REF.equals(resolvedSchemaResolution) ||
openapi31) {
if (AnnotationsUtils.areSiblingsAllowed(resolvedSchemaResolution, openapi31)) {
List<Annotation> ctxAnnotations31List = new ArrayList<>();
if (annotations != null) {
for (Annotation a : annotations) {
Expand All @@ -763,9 +760,6 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
!(a instanceof io.swagger.v3.oas.annotations.media.ArraySchema)) {
ctxAnnotations31List.add(a);
}
if ((ctxSchema != null) && (!ctxSchema.implementation().equals(Void.class) || StringUtils.isNotEmpty(ctxSchema.type()))) {
ctxAnnotations31List.add(a);
}
}
ctxAnnotation31 = ctxAnnotations31List.toArray(new Annotation[ctxAnnotations31List.size()]);
}
Expand Down Expand Up @@ -823,6 +817,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
return openapi31 ? new JsonSchema() : new Schema();
}
});

boolean areSiblingsAllowed = AnnotationsUtils.areSiblingsAllowed(resolvedSchemaResolution, openapi31);
aType = AnnotationsUtils.addTypeWhenSiblingsAllowed(aType, ctxSchema, areSiblingsAllowed);
property = context.resolve(aType);
property = clone(property);
Schema ctxProperty = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2916,4 +2916,19 @@ public static boolean computeEnumAsRef(io.swagger.v3.oas.annotations.media.Schem
}
return false;
}

public static boolean areSiblingsAllowed(Schema.SchemaResolution resolvedSchemaResolution, boolean openapi31) {
return Schema.SchemaResolution.ALL_OF.equals(resolvedSchemaResolution) || Schema.SchemaResolution.ALL_OF_REF.equals(resolvedSchemaResolution) || openapi31;
}

public static AnnotatedType addTypeWhenSiblingsAllowed(AnnotatedType aType, io.swagger.v3.oas.annotations.media.Schema ctxSchema, boolean areSiblingsAllowed) {
if (areSiblingsAllowed && ctxSchema != null) {
if (!Void.class.equals(ctxSchema.implementation())) {
aType.setType(ctxSchema.implementation());
} else if (StringUtils.isNotBlank(ctxSchema.type())) {
aType.setType(ctxSchema.type().getClass());
}
}
return aType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.swagger.v3.core.resolving;

import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.oas.models.media.Schema;
import org.testng.annotations.Test;

import java.util.Map;

public class Ticket4800Test extends SwaggerTestBase{

@Test(description = "Custom schema implementation in property and enum as ref type value")
public void testCustomSchemaImplementation() {

String expectedYaml = "ModelWithCustomSchemaImplementationInProperty:\n" +
" type: object\n" +
" properties:\n" +
" enumExampleFieldWithImplementationProp:\n" +
" $ref: \"#/components/schemas/MyEnum\"\n" +
" default: \"yes\"\n" +
" description: Prop description\n" +
" secondExampleFieldWithTypeProp:\n" +
" type: string\n" +
"MyEnum:\n" +
" type: string\n" +
" enum:\n" +
" - \"yes\"\n" +
" - \"no\"";

Map<String, Schema> stringSchemaMap = ModelConverters.getInstance(true).readAll(Ticket4800Test.ModelWithCustomSchemaImplementationInProperty.class);
SerializationMatchers.assertEqualsToYaml31(stringSchemaMap, expectedYaml);
}

static class ModelWithCustomSchemaImplementationInProperty {

@io.swagger.v3.oas.annotations.media.Schema(implementation = MyEnum.class, description = "Prop description", defaultValue = "yes", enumAsRef = true)
private MyEnum enumExampleFieldWithImplementationProp;

@io.swagger.v3.oas.annotations.media.Schema(type = "string", enumAsRef = true)
private MyEnum2 secondExampleFieldWithTypeProp;

public MyEnum getEnumExampleFieldWithImplementationProp() {
return enumExampleFieldWithImplementationProp;
}

public void setEnumExampleFieldWithImplementationProp(MyEnum enumExampleFieldWithImplementationProp) {
this.enumExampleFieldWithImplementationProp = enumExampleFieldWithImplementationProp;
}

public MyEnum2 getSecondExampleFieldWithTypeProp() {
return secondExampleFieldWithTypeProp;
}

public void setSecondExampleFieldWithTypeProp(MyEnum2 secondExampleFieldWithTypeProp) {
this.secondExampleFieldWithTypeProp = secondExampleFieldWithTypeProp;
}

enum MyEnum {
yes, no
}

//Should not be included in the model definitions
enum MyEnum2 {
si, no
}
}
}
Loading