Skip to content
Open
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 @@ -57,7 +57,7 @@ public void defineModel(String name, Schema model, AnnotatedType type, String pr
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(String.format("defineModel %s %s", name, model));
}
modelByName.put(name, model);
putModelByName(name, model);

if (StringUtils.isNotBlank(prevName) && !prevName.equals(name)) {
modelByName.remove(prevName);
Expand All @@ -68,6 +68,27 @@ public void defineModel(String name, Schema model, AnnotatedType type, String pr
}
}

/**
* Registers {@code model} under {@code name}, but never lets a schema without composition keywords
* overwrite an already-registered composed schema of the same name. This preserves the {@code allOf}
* reference a polymorphic subtype gains from its parent's {@code resolveSubtypes} when the same subtype
* is later resolved standalone (e.g. while resolving a discriminator mapping), which otherwise clobbers
* it back to a plain object schema.
*/
private void putModelByName(String name, Schema model) {
Schema existing = modelByName.get(name);
if (existing != null && hasComposition(existing) && !hasComposition(model)) {
return;
}
modelByName.put(name, model);
}

private static boolean hasComposition(Schema schema) {
return (schema.getAllOf() != null && !schema.getAllOf().isEmpty())
|| (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty())
|| (schema.getOneOf() != null && !schema.getOneOf().isEmpty());
}

@Override
public Map<String, Schema> getDefinedModels() {
return Collections.unmodifiableMap(modelByName);
Expand Down Expand Up @@ -101,7 +122,7 @@ public Schema resolve(AnnotatedType type) {

Schema resolvedImpl = resolved;
if (resolvedImpl.getName() != null) {
modelByName.put(resolvedImpl.getName(), resolved);
putModelByName(resolvedImpl.getName(), resolved);
}
} else {
processedTypes.remove(type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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.oas.annotations.media.DiscriminatorMapping;
import io.swagger.v3.oas.annotations.media.Schema;
import org.testng.annotations.Test;

import java.util.Map;

import static io.swagger.v3.core.resolving.SwaggerTestBase.mapper;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class Ticket5028Test {

@Test
public void testSubtypeKeepsCompositionOpenApi30() {
final ModelResolver modelResolver = new ModelResolver(mapper());
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);

context.resolve(new AnnotatedType(AttributeType.class));
final Map<String, io.swagger.v3.oas.models.media.Schema> models = context.getDefinedModels();

final io.swagger.v3.oas.models.media.Schema subtype = models.get("DateAttributeTypeImpl");
assertNotNull(subtype, "subtype schema should be defined");
assertNotNull(subtype.getAllOf(),
"subtype must keep its allOf reference to the parent, but was: " + subtype);
assertEquals(subtype.getAllOf().size(), 1);
assertEquals(((io.swagger.v3.oas.models.media.Schema) subtype.getAllOf().get(0)).get$ref(),
"#/components/schemas/AttributeType");
}

@Test
public void testSubtypeKeepsCompositionOpenApi31() {
final ModelResolver modelResolver = new ModelResolver(mapper());
modelResolver.setOpenapi31(true);
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);

context.resolve(new AnnotatedType(AttributeType.class));
final Map<String, io.swagger.v3.oas.models.media.Schema> models = context.getDefinedModels();

final io.swagger.v3.oas.models.media.Schema subtype = models.get("DateAttributeTypeImpl");
assertNotNull(subtype, "subtype schema should be defined");
assertNotNull(subtype.getAllOf(),
"subtype must keep its allOf reference to the parent, but was: " + subtype);
assertTrue(subtype.getAllOf().stream()
.anyMatch(s -> ("#/components/schemas/AttributeType")
.equals(((io.swagger.v3.oas.models.media.Schema) s).get$ref())),
"allOf must reference the parent AttributeType, but was: " + subtype.getAllOf());
}

interface ResourceType {
}

@Schema(
subTypes = {DateAttributeType.class},
discriminatorProperty = "resourceType",
discriminatorMapping = {
@DiscriminatorMapping(value = "DateAttributeType", schema = DateAttributeType.class)
})
interface AttributeType extends ResourceType {
@Schema(description = "The public id of the attribute type.", example = "ApprovalDate_C")
String getPublicId();
}

@Schema(implementation = DateAttributeTypeImpl.class)
interface DateAttributeType extends AttributeType {
}

static class AttributeTypeImpl implements AttributeType {
@Override
public String getPublicId() {
return "";
}
}

static class DateAttributeTypeImpl extends AttributeTypeImpl implements DateAttributeType {
}
}