Skip to content

Commit

Permalink
fix: refs #4290 - fixes anyof for non objects
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Nov 2, 2022
1 parent 85829d5 commit c1950f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
allOfFiltered.forEach(c -> {
Schema allOfRef = context.resolve(new AnnotatedType().type(c).jsonViewAnnotation(annotatedType.getJsonViewAnnotation()));
Schema refSchema = new Schema().$ref(allOfRef.getName());
if (StringUtils.isBlank(allOfRef.getName())) {
refSchema = allOfRef;
}
// allOf could have already being added during subtype resolving
if (composedSchema.getAllOf() == null || !composedSchema.getAllOf().contains(refSchema)) {
composedSchema.addAllOfItem(refSchema);
Expand All @@ -822,7 +825,11 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
.collect(Collectors.toList());
anyOfFiltered.forEach(c -> {
Schema anyOfRef = context.resolve(new AnnotatedType().type(c).jsonViewAnnotation(annotatedType.getJsonViewAnnotation()));
composedSchema.addAnyOfItem(new Schema().$ref(anyOfRef.getName()));
if (StringUtils.isNotBlank(anyOfRef.getName())) {
composedSchema.addAnyOfItem(new Schema().$ref(anyOfRef.getName()));
} else {
composedSchema.addAnyOfItem(anyOfRef);
}
// remove shared properties defined in the parent
if (isSubtype(beanDesc.getClassInfo(), c)) {
removeParentProperties(composedSchema, anyOfRef);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.swagger.v3.core.resolving;

import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverterContextImpl;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.resolving.resources.Issue4290;
import io.swagger.v3.oas.models.media.Schema;
import org.testng.annotations.Test;

public class Ticket4290Test extends SwaggerTestBase {
@Test
public void testAnyOf() throws Exception {
final ModelResolver modelResolver = new ModelResolver(mapper());

final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);

final Schema model = context
.resolve(new AnnotatedType(Issue4290.class));

SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "Issue4290:\n" +
" type: object\n" +
" properties:\n" +
" value:\n" +
" type: object\n" +
" description: \"A string, a number or a boolean\"\n" +
" anyOf:\n" +
" - type: string\n" +
" - type: number\n" +
" - type: boolean");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.swagger.v3.core.resolving.resources;

import io.swagger.v3.oas.annotations.media.Schema;

public class Issue4290 {
@Schema(description = "A string, a number or a boolean", anyOf = { String.class, Number.class, Boolean.class })
public Object value;

}

0 comments on commit c1950f5

Please sign in to comment.