Search in sources :

Example 71 with ArraySchema

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

the class InlineModelResolver method flattenBody.

private void flattenBody(String pathname, RequestBody body) {
    if (body == null) {
        return;
    }
    Map<String, MediaType> content = body.getContent();
    if (content == null) {
        return;
    }
    for (String key : content.keySet()) {
        if (content.get(key) != null) {
            MediaType mediaType = content.get(key);
            if (mediaType.getSchema() != null) {
                Schema model = mediaType.getSchema();
                String genericName = pathBody(pathname);
                if (model.getProperties() != null && model.getProperties().size() > 0) {
                    flattenProperties(model.getProperties(), pathname);
                    String modelName = resolveModelName(model.getTitle(), genericName);
                    mediaType.setSchema(new Schema().$ref(modelName));
                    addGenerated(modelName, model);
                    openAPI.getComponents().addSchemas(modelName, model);
                } else if (model instanceof ComposedSchema) {
                    flattenComposedSchema(model, pathname);
                    if (model.get$ref() == null) {
                        String modelName = resolveModelName(model.getTitle(), genericName);
                        mediaType.setSchema(this.makeRefProperty(modelName, model));
                        addGenerated(modelName, model);
                        openAPI.getComponents().addSchemas(modelName, model);
                    }
                } else if (model instanceof ArraySchema) {
                    ArraySchema am = (ArraySchema) model;
                    Schema inner = am.getItems();
                    if (isObjectSchema(inner)) {
                        if (inner.getProperties() != null && inner.getProperties().size() > 0) {
                            flattenProperties(inner.getProperties(), pathname);
                            String modelName = resolveModelName(inner.getTitle(), genericName);
                            String existing = matchGenerated(inner);
                            if (existing != null) {
                                am.setItems(new Schema().$ref(existing));
                            } else {
                                am.setItems(new Schema().$ref(modelName));
                                addGenerated(modelName, inner);
                                openAPI.getComponents().addSchemas(modelName, inner);
                            }
                        } else if (inner instanceof ComposedSchema && this.flattenComposedSchemas) {
                            flattenComposedSchema(inner, key);
                            if (inner.get$ref() == null) {
                                String modelName = resolveModelName(inner.getTitle(), "inline_body_items_" + key + "_" + pathname);
                                am.setItems(this.makeRefProperty(modelName, inner));
                                addGenerated(modelName, inner);
                                openAPI.getComponents().addSchemas(modelName, inner);
                            }
                        }
                    }
                }
            }
        }
    }
}
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)

Example 72 with ArraySchema

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

the class FileReferenceTest method testAllOfFlatAndNested.

@Test
public void testAllOfFlatAndNested() {
    for (String path : Arrays.asList("./src/test/resources/allOf-properties-ext-ref/models/swagger.json", "./src/test/resources/allOf-properties-ext-ref/swagger.json")) {
        OpenAPI swagger = new OpenAPIV3Parser().read(path);
        assertEquals(3, swagger.getComponents().getSchemas().size());
        ComposedSchema composedModel = (ComposedSchema) swagger.getComponents().getSchemas().get("record");
        assertEquals(composedModel.getAllOf().get(0).get$ref(), "#/components/schemas/pet");
        Map<String, Schema> props = composedModel.getAllOf().get(1).getProperties();
        assertEquals(props.get("mother").get$ref(), "#/components/schemas/pet");
        assertEquals(((ArraySchema) props.get("siblings")).getItems().get$ref(), "#/components/schemas/pet");
    }
}
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) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 73 with ArraySchema

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

the class SwaggerConverter method convert.

public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
    Parameter v3Parameter = new Parameter();
    if (StringUtils.isNotBlank(v2Parameter.getDescription())) {
        v3Parameter.setDescription(v2Parameter.getDescription());
    }
    if (v2Parameter instanceof SerializableParameter) {
        v3Parameter.setAllowEmptyValue(((SerializableParameter) v2Parameter).getAllowEmptyValue());
    }
    v3Parameter.setIn(v2Parameter.getIn());
    v3Parameter.setName(v2Parameter.getName());
    Object exampleExtension = v2Parameter.getVendorExtensions().get("x-example");
    if (exampleExtension != null) {
        v3Parameter.setExample(exampleExtension);
    }
    Schema schema = null;
    if (v2Parameter instanceof RefParameter) {
        RefParameter ref = (RefParameter) v2Parameter;
        if (ref.get$ref().indexOf("#/parameters") == 0) {
            String updatedRef = "#/components/";
            if (components.getRequestBodies() != null && components.getRequestBodies().get(ref.getSimpleRef()) != null) {
                updatedRef += "requestBodies";
            } else if (components.getSchemas() != null && components.getSchemas().get("formData_" + ref.getSimpleRef()) != null) {
                updatedRef += "schemas";
            } else {
                updatedRef += "parameters";
            }
            updatedRef += ref.get$ref().substring("#/parameters".length());
            ref.set$ref(updatedRef);
        }
        v3Parameter.set$ref(ref.get$ref());
    } else if (v2Parameter instanceof SerializableParameter) {
        SerializableParameter sp = (SerializableParameter) v2Parameter;
        if ("array".equals(sp.getType())) {
            ArraySchema a = new ArraySchema();
            // TODO: convert arrays to proper template format
            String cf = sp.getCollectionFormat();
            if (StringUtils.isEmpty(cf)) {
                cf = "csv";
            }
            switch(cf) {
                case "ssv":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.SPACEDELIMITED);
                    }
                    break;
                case "pipes":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle((Parameter.StyleEnum.PIPEDELIMITED));
                    }
                    break;
                case "tsv":
                    break;
                case "multi":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.FORM);
                        v3Parameter.setExplode(true);
                    }
                    break;
                case "csv":
                default:
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.FORM);
                        v3Parameter.setExplode(false);
                    } else if ("header".equals(v2Parameter.getIn()) || "path".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.SIMPLE);
                        v3Parameter.setExplode(false);
                    }
            }
            Property items = sp.getItems();
            Schema itemsSchema = convert(items);
            a.setItems(itemsSchema);
            if (sp.getMaxItems() != null) {
                a.setMaxItems(sp.getMaxItems());
            }
            if (sp.getMinItems() != null) {
                a.setMinItems(sp.getMinItems());
            }
            if (sp.isUniqueItems() != null) {
                a.setUniqueItems(sp.isUniqueItems());
            }
            schema = a;
        } else {
            schema = SchemaTypeUtil.createSchema(sp.getType(), sp.getFormat());
            schema.setType(sp.getType());
            schema.setFormat(sp.getFormat());
            schema.setMaximum(sp.getMaximum());
            schema.setExclusiveMaximum(sp.isExclusiveMaximum());
            schema.setMinimum(sp.getMinimum());
            schema.setExclusiveMinimum(sp.isExclusiveMinimum());
            schema.setMinLength(sp.getMinLength());
            schema.setMaxLength(sp.getMaxLength());
            if (sp.getMultipleOf() != null) {
                schema.setMultipleOf(new BigDecimal(sp.getMultipleOf().toString()));
            }
            schema.setPattern(sp.getPattern());
        }
        if (sp.getEnum() != null) {
            for (String e : sp.getEnum()) {
                switch(sp.getType() == null ? SchemaTypeUtil.OBJECT_TYPE : sp.getType()) {
                    case SchemaTypeUtil.INTEGER_TYPE:
                        schema.addEnumItemObject(Integer.parseInt(e));
                        break;
                    case SchemaTypeUtil.NUMBER_TYPE:
                        schema.addEnumItemObject(new BigDecimal(e));
                        break;
                    case SchemaTypeUtil.BOOLEAN_TYPE:
                        schema.addEnumItemObject(Boolean.valueOf(e));
                        break;
                    default:
                        schema.addEnumItemObject(e);
                        break;
                }
            }
        }
        if (sp.getVendorExtensions() != null) {
            Object nullableExtension = sp.getVendorExtensions().get("x-nullable");
            if (nullableExtension != null) {
                schema.setNullable((Boolean) nullableExtension);
            }
        }
        schema.setExtensions(convert(sp.getVendorExtensions()));
        if (sp instanceof AbstractSerializableParameter) {
            AbstractSerializableParameter ap = (AbstractSerializableParameter) sp;
            schema.setDefault(ap.getDefault());
        }
    }
    if (v2Parameter.getRequired()) {
        v3Parameter.setRequired(v2Parameter.getRequired());
    }
    v3Parameter.setSchema(schema);
    v3Parameter.setExtensions(convert(v2Parameter.getVendorExtensions()));
    return v3Parameter;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) RefParameter(io.swagger.models.parameters.RefParameter) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) 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) Parameter(io.swagger.v3.oas.models.parameters.Parameter) SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) RefParameter(io.swagger.models.parameters.RefParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) BigDecimal(java.math.BigDecimal)

Example 74 with ArraySchema

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

the class SwaggerConverter method convert.

private Schema convert(Property schema) {
    if (schema == null) {
        return null;
    }
    Schema result;
    if (schema instanceof RefProperty) {
        RefProperty ref = (RefProperty) schema;
        if (ref.get$ref().indexOf("#/definitions") == 0) {
            String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
            ref.set$ref(updatedRef);
        }
        result = new Schema();
        result.set$ref(ref.get$ref());
    } else if (schema instanceof ArrayProperty) {
        ArraySchema arraySchema = Json.mapper().convertValue(schema, ArraySchema.class);
        Property items = ((ArrayProperty) schema).getItems();
        Schema itemsSchema = convert(items);
        arraySchema.setItems(itemsSchema);
        if (((ArrayProperty) schema).getMaxItems() != null) {
            arraySchema.setMaxItems(((ArrayProperty) schema).getMaxItems());
        }
        if (((ArrayProperty) schema).getMinItems() != null) {
            arraySchema.setMinItems(((ArrayProperty) schema).getMinItems());
        }
        if (((ArrayProperty) schema).getUniqueItems() != null && ((ArrayProperty) schema).getUniqueItems()) {
            arraySchema.setUniqueItems(((ArrayProperty) schema).getUniqueItems());
        }
        result = arraySchema;
    } else if (schema instanceof FileProperty) {
        FileSchema fileSchema = Json.mapper().convertValue(schema, FileSchema.class);
        result = fileSchema;
    } else {
        result = Json.mapper().convertValue(schema, Schema.class);
        if (schema.getExample() != null) {
            result.setExample(schema.getExample());
        }
        if ("object".equals(schema.getType()) && (result.getProperties() != null) && (result.getProperties().size() > 0)) {
            Map<String, Schema> properties = new LinkedHashMap<>();
            ((ObjectProperty) schema).getProperties().forEach((k, v) -> properties.put(k, convert(v)));
            result.setProperties(properties);
        }
        if (schema instanceof MapProperty) {
            MapProperty map = (MapProperty) schema;
            result.setAdditionalProperties(convert(map.getAdditionalProperties()));
            result.setMinProperties(map.getMinProperties());
            result.setMaxProperties(map.getMaxProperties());
        }
        if (schema instanceof AbstractNumericProperty) {
            AbstractNumericProperty np = (AbstractNumericProperty) schema;
            result.setExclusiveMaximum(np.getExclusiveMaximum());
            result.setExclusiveMinimum(np.getExclusiveMinimum());
        }
        if (schema instanceof StringProperty) {
            StringProperty sp = (StringProperty) schema;
            result.setMinLength(sp.getMinLength());
            result.setMaxLength(sp.getMaxLength());
            result.setPattern(sp.getPattern());
        }
    }
    if (schema.getVendorExtensions() != null) {
        Object nullableExtension = schema.getVendorExtensions().get("x-nullable");
        if (nullableExtension != null) {
            result.setNullable((Boolean) nullableExtension);
        }
        result.setExtensions(convert(schema.getVendorExtensions()));
    }
    return result;
}
Also used : 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) LinkedHashMap(java.util.LinkedHashMap) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) FileSchema(io.swagger.v3.oas.models.media.FileSchema)

Example 75 with ArraySchema

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

the class V2ConverterTest method testIssue32.

@Test(description = "Convert schema, property and array examples")
public void testIssue32() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_32_JSON);
    Map<String, Schema> schemas = oas.getComponents().getSchemas();
    assertNotNull(schemas);
    Map properties = schemas.get(USER_MODEL).getProperties();
    assertNotNull(properties);
    assertEquals(((Schema) properties.get(ID)).getExample(), EXAMPLE_42_NUMBER);
    assertEquals(((Schema) properties.get(NAME)).getExample(), ARTHUR_DENT_NAME);
    assertEquals(((ArraySchema) properties.get(FRIEND_IDS)).getItems().getExample(), EXAMPLE_8_NUMBER);
    final List<Integer> numbers = new ArrayList<>();
    numbers.add(3);
    numbers.add(4);
    numbers.add(5);
    assertEquals(((ArraySchema) properties.get(FRIEND_IDS)).getExample(), numbers);
    assertEquals(Json.mapper().writeValueAsString(schemas.get(ARRAY_OF_USERS_MODEL).getExample()), ARRAY_VALUES);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ArrayList(java.util.ArrayList) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) Test(org.testng.annotations.Test)

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