Search in sources :

Example 1 with Discriminator

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Discriminator(io.swagger.v3.oas.models.media.Discriminator)

Example 2 with 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);
        }
    }
}
Also used : HashMap(java.util.HashMap) Discriminator(io.swagger.v3.oas.models.media.Discriminator)

Example 3 with Discriminator

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"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with Discriminator

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");
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 5 with Discriminator

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;
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ModelImpl(io.swagger.models.ModelImpl) ArrayModel(io.swagger.models.ArrayModel) Discriminator(io.swagger.v3.oas.models.media.Discriminator)

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)19 Test (org.testng.annotations.Test)19 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)15 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)15 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)15 MapSchema (io.swagger.v3.oas.models.media.MapSchema)14 StringSchema (io.swagger.v3.oas.models.media.StringSchema)14 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)13 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)13 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)12 OpenAPI (io.swagger.v3.oas.models.OpenAPI)11 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)6 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)6 Discriminator (io.swagger.v3.oas.models.media.Discriminator)5 DateSchema (io.swagger.v3.oas.models.media.DateSchema)4 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)4 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)3 HashSet (java.util.HashSet)3 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2