Search in sources :

Example 66 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class ExternalRefProcessor method processSchema.

private void processSchema(Schema property, String file) {
    if (property != null) {
        if (StringUtils.isNotBlank(property.get$ref())) {
            processRefSchema(property, file);
        }
        if (property.getProperties() != null) {
            processProperties(property.getProperties(), file);
        }
        if (property instanceof ArraySchema) {
            processSchema(((ArraySchema) property).getItems(), file);
        }
        if (property.getAdditionalProperties() instanceof Schema) {
            processSchema(((Schema) property.getAdditionalProperties()), file);
        }
        if (property instanceof ComposedSchema) {
            ComposedSchema composed = (ComposedSchema) property;
            processProperties(composed.getAllOf(), file);
            processProperties(composed.getAnyOf(), file);
            processProperties(composed.getOneOf(), file);
        }
    }
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema)

Example 67 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class SchemaProcessorTest method testProcessArrayProperty_ItemsIsRefProperty.

@Test
public void testProcessArrayProperty_ItemsIsRefProperty() throws Exception {
    expectCreationOfExternalRefProcessor();
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final Schema refProperty = new Schema().$ref(ref);
    ArraySchema arrayProperty = new ArraySchema();
    arrayProperty.setItems(refProperty);
    expectCallToExternalRefProcessor(ref, RefFormat.URL, "bar");
    new SchemaProcessor(cache, openAPI).processSchema(arrayProperty);
    new FullVerifications() {

        {
        }
    };
    assertEquals((arrayProperty.getItems()).get$ref(), "#/components/schemas/bar");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Test(org.testng.annotations.Test)

Example 68 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class InlineModelResolver method flattenResponses.

private void flattenResponses(String pathname, Map<String, ApiResponse> responses) {
    if (responses == null) {
        return;
    }
    for (String key : responses.keySet()) {
        ApiResponse response = responses.get(key);
        if (response.getContent() != null) {
            Map<String, MediaType> content = response.getContent();
            for (String name : content.keySet()) {
                if (content.get(name) != null) {
                    MediaType media = content.get(name);
                    if (media.getSchema() != null) {
                        Schema mediaSchema = media.getSchema();
                        if (isObjectSchema(mediaSchema)) {
                            if (mediaSchema.getProperties() != null && mediaSchema.getProperties().size() > 0 || mediaSchema instanceof ComposedSchema) {
                                String modelName = resolveModelName(mediaSchema.getTitle(), "inline_response_" + key);
                                String existing = matchGenerated(mediaSchema);
                                if (existing != null) {
                                    media.setSchema(this.makeRefProperty(existing, mediaSchema));
                                } else {
                                    media.setSchema(this.makeRefProperty(modelName, mediaSchema));
                                    addGenerated(modelName, mediaSchema);
                                    openAPI.getComponents().addSchemas(modelName, mediaSchema);
                                }
                            } else if (mediaSchema.getAdditionalProperties() != null && !(mediaSchema.getAdditionalProperties() instanceof Boolean)) {
                                Schema innerProperty = (Schema) mediaSchema.getAdditionalProperties();
                                if (isObjectSchema(innerProperty)) {
                                    key = "inline_response_map" + key;
                                    flattenMapSchema(innerProperty, key, pathname, mediaSchema);
                                } else if (innerProperty instanceof ArraySchema) {
                                    ArraySchema arraySchema = (ArraySchema) innerProperty;
                                    Schema inner = arraySchema.getItems();
                                    if (isObjectSchema(inner)) {
                                        key = "inline_response_map_items" + key;
                                        flattenMapSchema(inner, key, pathname, mediaSchema);
                                    }
                                }
                            }
                        } else if (mediaSchema instanceof ArraySchema) {
                            ArraySchema ap = (ArraySchema) mediaSchema;
                            Schema inner = ap.getItems();
                            if (isObjectSchema(inner)) {
                                flattenArraySchema(inner, key, pathname, ap);
                            }
                        } else if (mediaSchema.getAdditionalProperties() != null && !(mediaSchema.getAdditionalProperties() instanceof Boolean)) {
                            Schema innerProperty = (Schema) mediaSchema.getAdditionalProperties();
                            if (isObjectSchema(innerProperty)) {
                                key = "inline_response_map" + key;
                                flattenMapSchema(innerProperty, key, pathname, mediaSchema);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Example 69 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class InlineModelResolver method modelFromProperty.

@SuppressWarnings("static-method")
public Schema modelFromProperty(ArraySchema object, @SuppressWarnings("unused") String path) {
    String description = object.getDescription();
    String example = null;
    Object obj = object.getExample();
    if (obj != null) {
        example = obj.toString();
    }
    Schema inner = object.getItems();
    if (inner instanceof ObjectSchema) {
        ArraySchema model = new ArraySchema();
        model.setDescription(description);
        model.setExample(example);
        model.setItems(object.getItems());
        return model;
    }
    return null;
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema)

Example 70 with ArraySchema

use of io.swagger.v3.oas.annotations.media.ArraySchema in project swagger-parser by swagger-api.

the class InlineModelResolver method flattenProperties.

public void flattenProperties(Map<String, Schema> properties, String path) {
    if (properties == null) {
        return;
    }
    Map<String, Schema> propsToUpdate = new HashMap<>();
    Map<String, Schema> modelsToAdd = new HashMap<>();
    for (String key : properties.keySet()) {
        Schema property = properties.get(key);
        if (isObjectSchema(property) && property.getProperties() != null && property.getProperties().size() > 0) {
            String modelName = resolveModelName(property.getTitle(), path + "_" + key);
            Schema model = createModelFromProperty(property, modelName);
            String existing = matchGenerated(model);
            if (existing != null) {
                propsToUpdate.put(key, new Schema().$ref(existing));
            } else {
                propsToUpdate.put(key, new Schema().$ref(RefType.SCHEMAS.getInternalPrefix() + modelName));
                modelsToAdd.put(modelName, model);
                addGenerated(modelName, model);
                openAPI.getComponents().addSchemas(modelName, model);
            }
        } else if (property instanceof ArraySchema) {
            ArraySchema ap = (ArraySchema) property;
            Schema inner = ap.getItems();
            if (isObjectSchema(inner)) {
                if (inner.getProperties() != null && inner.getProperties().size() > 0) {
                    flattenProperties(inner.getProperties(), path);
                    String modelName = resolveModelName(inner.getTitle(), path + "_" + key);
                    Schema innerModel = createModelFromProperty(inner, modelName);
                    String existing = matchGenerated(innerModel);
                    if (existing != null) {
                        ap.setItems(new Schema().$ref(existing));
                    } else {
                        ap.setItems(new Schema().$ref(modelName));
                        addGenerated(modelName, innerModel);
                        openAPI.getComponents().addSchemas(modelName, innerModel);
                    }
                } else if (inner instanceof ComposedSchema && this.flattenComposedSchemas) {
                    flattenComposedSchema(inner, key);
                    String modelName = resolveModelName(inner.getTitle(), path + "_" + key);
                    Schema innerModel = createModelFromProperty(inner, modelName);
                    String existing = matchGenerated(innerModel);
                    if (existing != null) {
                        ap.setItems(new Schema().$ref(existing));
                    } else {
                        ap.setItems(new Schema().$ref(modelName));
                        addGenerated(modelName, innerModel);
                        openAPI.getComponents().addSchemas(modelName, innerModel);
                    }
                }
            }
        } else if (property.getAdditionalProperties() != null && !(property.getAdditionalProperties() instanceof Boolean)) {
            Schema inner = (Schema) property.getAdditionalProperties();
            if (isObjectSchema(inner)) {
                if (inner.getProperties() != null && inner.getProperties().size() > 0) {
                    flattenProperties(inner.getProperties(), path);
                    String modelName = resolveModelName(inner.getTitle(), path + "_" + key);
                    Schema innerModel = createModelFromProperty(inner, modelName);
                    String existing = matchGenerated(innerModel);
                    if (existing != null) {
                        property.setAdditionalProperties(new Schema().$ref(existing));
                    } else {
                        property.setAdditionalProperties(new Schema().$ref(modelName));
                        addGenerated(modelName, innerModel);
                        openAPI.getComponents().addSchemas(modelName, innerModel);
                    }
                }
            }
        }
    }
    if (propsToUpdate.size() > 0) {
        for (String key : propsToUpdate.keySet()) {
            properties.put(key, propsToUpdate.get(key));
        }
    }
    for (String key : modelsToAdd.keySet()) {
        openAPI.getComponents().addSchemas(key, modelsToAdd.get(key));
        this.addedModels.put(key, modelsToAdd.get(key));
    }
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) HashMap(java.util.HashMap) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema)

Aggregations

ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)107 Schema (io.swagger.v3.oas.models.media.Schema)85 Test (org.testng.annotations.Test)76 StringSchema (io.swagger.v3.oas.models.media.StringSchema)63 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)53 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)53 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)51 OpenAPI (io.swagger.v3.oas.models.OpenAPI)49 MapSchema (io.swagger.v3.oas.models.media.MapSchema)28 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)22 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)21 PathItem (io.swagger.v3.oas.models.PathItem)16 Parameter (io.swagger.v3.oas.models.parameters.Parameter)16 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)16 MediaType (io.swagger.v3.oas.models.media.MediaType)15 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)15 Operation (io.swagger.v3.oas.models.Operation)14 Content (io.swagger.v3.oas.models.media.Content)12 DateSchema (io.swagger.v3.oas.models.media.DateSchema)11 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)11