Skip to content
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

fix: refs #4290 - fixes anyof for non objects #4291

Merged
merged 1 commit into from
Nov 2, 2022
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 @@ -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;

}