use of io.swagger.v3.oas.models.media.Discriminator in project swagger-parser by swagger-api.
the class OpenAPIDeserializer method getDiscriminator.
public Discriminator getDiscriminator(ObjectNode node, String location, ParseResult result) {
Discriminator discriminator = new Discriminator();
String value = getString("propertyName", node, true, location, result);
if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
discriminator.setPropertyName(value);
}
ObjectNode mappingNode = getObject("mapping", node, false, location, result);
if (mappingNode != null) {
Map<String, String> mapping = new LinkedHashMap<>();
Set<String> keys = getKeys(mappingNode);
for (String key : keys) {
mapping.put(key, mappingNode.get(key).asText());
}
discriminator.setMapping(mapping);
}
return discriminator;
}
use of io.swagger.v3.oas.models.media.Discriminator in project swagger-parser by swagger-api.
the class SchemaProcessor method changeDiscriminatorMapping.
private void changeDiscriminatorMapping(ComposedSchema composedSchema, String oldRef, String newRef) {
Discriminator discriminator = composedSchema.getDiscriminator();
if (!oldRef.equals(newRef) && discriminator != null) {
String oldName = RefUtils.computeDefinitionName(oldRef);
String newName = RefUtils.computeDefinitionName(newRef);
String mappingName = null;
if (discriminator.getMapping() != null) {
for (String name : discriminator.getMapping().keySet()) {
if (oldRef.equals(discriminator.getMapping().get(name))) {
mappingName = name;
break;
}
}
if (mappingName != null) {
discriminator.getMapping().put(mappingName, newRef);
}
}
if (mappingName == null && !oldName.equals(newName)) {
if (discriminator.getMapping() == null) {
discriminator.setMapping(new HashMap());
}
discriminator.getMapping().put(oldName, newRef);
}
}
}
use of io.swagger.v3.oas.models.media.Discriminator in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testDiscriminatorObject.
@Test
void testDiscriminatorObject(@Injectable List<AuthorizationValue> auths) {
String yaml = "openapi: '3.0.1'\n" + "components:\n" + " schemas:\n" + " Pet:\n" + " type: object\n" + " required:\n" + " - pet_type\n" + " properties:\n" + " pet_type:\n" + " type: string\n" + " discriminator:\n" + " propertyName: pet_type\n" + " mapping:\n" + " cachorro: Dog\n" + " Cat:\n" + " allOf:\n" + " - $ref: '#/components/schemas/Pet'\n" + " - type: object\n" + " # all other properties specific to a `Cat`\n" + " properties:\n" + " name:\n" + " type: string\n" + " Dog:\n" + " allOf:\n" + " - $ref: '#/components/schemas/Pet'\n" + " - type: object\n" + " # all other properties specific to a `Dog`\n" + " properties:\n" + " bark:\n" + " type: string\n" + " Lizard:\n" + " allOf:\n" + " - $ref: '#/components/schemas/Pet'\n" + " - type: object\n" + " # all other properties specific to a `Lizard`\n" + " properties:\n" + " lovesRocks:\n" + " type: boolean";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readContents(yaml, auths, options);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<>(messageList);
assertEquals(result.getOpenAPI().getComponents().getSchemas().get("Pet").getDiscriminator().getPropertyName(), "pet_type");
assertEquals(result.getOpenAPI().getComponents().getSchemas().get("Pet").getDiscriminator().getMapping().get("cachorro"), "Dog");
assertTrue(messages.contains("attribute paths is missing"));
assertTrue(messages.contains("attribute info is missing"));
}
use of io.swagger.v3.oas.models.media.Discriminator in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testDeserializeWithEnumDiscriminator.
@Test
public void testDeserializeWithEnumDiscriminator() {
String yaml = "openapi: 3.0.0\n" + "components:\n" + " schemas:\n" + " Animal:\n" + " type: object\n" + " discriminator:\n" + " propertyName: petType\n" + " description: |\n" + " A basic `Animal` object which can extend to other animal types.\n" + " required:\n" + " - commonName\n" + " - petType\n" + " properties:\n" + " commonName:\n" + " description: the household name of the animal\n" + " type: string\n" + " petType:\n" + " description: |\n" + " The discriminator for the animal type. It _must_\n" + " match one of the concrete schemas by name (i.e. `Cat`)\n" + " for proper deserialization\n" + " enum:\n" + " - cat\n" + " - dog";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
Map<String, Schema> properties = result.getOpenAPI().getComponents().getSchemas().get("Animal").getProperties();
assertTrue(properties.containsKey("commonName"));
assertTrue(properties.containsKey("petType"));
assertEquals(properties.get("petType").getType(), "string");
}
use of io.swagger.v3.oas.models.media.Discriminator in project swagger-parser by swagger-api.
the class SwaggerConverter method convert.
public Schema convert(io.swagger.models.Model v2Model) {
if (v2Model == null) {
return null;
}
Schema result;
if (v2Model instanceof ArrayModel) {
ArraySchema arraySchema = Json.mapper().convertValue(v2Model, ArraySchema.class);
arraySchema.setItems(convert(((ArrayModel) v2Model).getItems()));
result = arraySchema;
} else if (v2Model instanceof ComposedModel) {
ComposedModel composedModel = (ComposedModel) v2Model;
ComposedSchema composed = new ComposedSchema();
composed.setDescription(composedModel.getDescription());
if (composedModel.getExample() != null) {
composed.setExample(composedModel.getExample());
}
if (composedModel.getExternalDocs() != null) {
composed.setExternalDocs(convert(composedModel.getExternalDocs()));
}
composed.setTitle(composedModel.getTitle());
composed.setExtensions(convert(composedModel.getVendorExtensions()));
composed.setAllOf(composedModel.getAllOf().stream().map(this::convert).collect(Collectors.toList()));
addProperties(v2Model, composed);
result = composed;
} else {
String v2discriminator = null;
if (v2Model instanceof ModelImpl) {
ModelImpl model = (ModelImpl) v2Model;
v2discriminator = model.getDiscriminator();
model.setDiscriminator(null);
}
result = Json.mapper().convertValue(v2Model, Schema.class);
addProperties(v2Model, result);
if (v2Model instanceof ModelImpl) {
ModelImpl model = (ModelImpl) v2Model;
if (model.getAdditionalProperties() != null) {
result.setAdditionalProperties(convert(model.getAdditionalProperties()));
}
} else if (v2Model instanceof RefModel) {
RefModel ref = (RefModel) v2Model;
if (ref.get$ref().indexOf("#/definitions") == 0) {
String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
result.set$ref(updatedRef);
}
}
if (v2discriminator != null) {
Discriminator discriminator = new Discriminator();
discriminator.setPropertyName(v2discriminator);
result.setDiscriminator(discriminator);
}
}
if (v2Model.getVendorExtensions() != null) {
Object nullableExtension = v2Model.getVendorExtensions().get("x-nullable");
if (nullableExtension != null) {
result.setNullable((Boolean) nullableExtension);
}
}
return result;
}
Aggregations