Search in sources :

Example 11 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter in project swagger-parser by swagger-api.

the class SwaggerCompatConverter method convertParameter.

public Parameter convertParameter(io.swagger.models.apideclaration.Parameter param) {
    Parameter output = null;
    List<String> _enum = param.getEnumValues();
    if (ParamType.PATH.equals(param.getParamType())) {
        PathParameter p = new PathParameter();
        p.setDefaultValue(param.getDefaultValue());
        p.setEnum(_enum);
        output = p;
    } else if (ParamType.QUERY.equals(param.getParamType())) {
        QueryParameter p = new QueryParameter();
        p.setDefaultValue(param.getDefaultValue());
        p.setEnum(_enum);
        output = p;
    } else if (ParamType.HEADER.equals(param.getParamType())) {
        HeaderParameter p = new HeaderParameter();
        p.setDefaultValue(param.getDefaultValue());
        p.setEnum(_enum);
        output = p;
    } else if (ParamType.BODY.equals(param.getParamType())) {
        BodyParameter p = new BodyParameter();
        output = p;
    } else if (ParamType.FORM.equals(param.getParamType())) {
        FormParameter p = new FormParameter();
        p.setDefaultValue(param.getDefaultValue());
        p.setEnum(_enum);
        output = p;
    }
    output.setName(param.getName());
    output.setDescription(param.getDescription());
    if (param.getRequired() != null) {
        output.setRequired(param.getRequired());
    }
    Property property = null;
    String type = param.getType() == null ? null : param.getType().toString();
    String format = param.getFormat() == null ? null : param.getFormat().toString();
    if (null == type) {
        LOGGER.warn("Empty type in Param: " + param);
    }
    if (output instanceof BodyParameter) {
        BodyParameter bp = (BodyParameter) output;
        bp.setSchema(modelFromExtendedTypedObject(param));
    } else if (output instanceof SerializableParameter) {
        SerializableParameter sp = (SerializableParameter) output;
        Property p = null;
        if (param.getAllowMultiple() != null && param.getAllowMultiple() == true) {
            ArrayProperty arrayProperty = new ArrayProperty();
            Property innerType = PropertyBuilder.build(type, format, null);
            arrayProperty.setItems(innerType);
            p = arrayProperty;
        } else {
            p = propertyFromTypedObject(param);
            if (p == null) {
                LOGGER.warn(String.format("WARNING! No property detected for parameter '%s' (%s)! Falling back to string!", param.getName(), param.getParamType()));
                p = new StringProperty();
            }
        }
        if (p instanceof ArrayProperty) {
            ArrayProperty ap = (ArrayProperty) p;
            sp.setType("array");
            sp.setCollectionFormat("csv");
            sp.setItems(ap.getItems());
        } else {
            sp.setType(p.getType());
            sp.setFormat(p.getFormat());
        }
    }
    // all path parameters are required
    if (output instanceof PathParameter) {
        ((PathParameter) output).setRequired(true);
    }
    return output;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) QueryParameter(io.swagger.models.parameters.QueryParameter) ArrayProperty(io.swagger.models.properties.ArrayProperty) SerializableParameter(io.swagger.models.parameters.SerializableParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) StringProperty(io.swagger.models.properties.StringProperty) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) FormParameter(io.swagger.models.parameters.FormParameter) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) ModelProperty(io.swagger.models.apideclaration.ModelProperty) RefProperty(io.swagger.models.properties.RefProperty) UntypedProperty(io.swagger.models.properties.UntypedProperty)

Example 12 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter 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 13 with SerializableParameter

use of io.swagger.models.parameters.SerializableParameter in project swagger-parser by swagger-api.

the class SwaggerConverter method convertFormDataToRequestBody.

private RequestBody convertFormDataToRequestBody(List<io.swagger.models.parameters.Parameter> formParams, List<String> consumes) {
    RequestBody body = new RequestBody();
    Schema formSchema = new Schema();
    for (io.swagger.models.parameters.Parameter param : formParams) {
        SerializableParameter sp;
        Schema schema;
        String name;
        if (param instanceof RefParameter) {
            RefParameter refParameter = (RefParameter) param;
            String simpleRef = refParameter.getSimpleRef();
            sp = (SerializableParameter) globalV2Parameters.get(simpleRef);
            name = components.getSchemas().get("formData_" + simpleRef).getName();
            schema = new Schema().$ref("#/components/schemas/formData_" + simpleRef);
        } else {
            sp = (SerializableParameter) param;
            schema = convert(sp);
            name = schema.getName();
        }
        if (sp.getRequired()) {
            formSchema.addRequiredItem(sp.getName());
        }
        formSchema.addProperties(name, schema);
    }
    List<String> mediaTypes = new ArrayList<>(globalConsumes);
    if (consumes != null && consumes.size() > 0) {
        mediaTypes.clear();
        mediaTypes.addAll(consumes);
    }
    // Assume multipart/form-data if nothing is specified
    if (mediaTypes.size() == 0) {
        mediaTypes.add("multipart/form-data");
    }
    Content content = new Content();
    for (String type : mediaTypes) {
        content.addMediaType(type, new MediaType().schema(formSchema));
    }
    body.content(content);
    return body;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) RefParameter(io.swagger.models.parameters.RefParameter) Content(io.swagger.v3.oas.models.media.Content) 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) ArrayList(java.util.ArrayList) MediaType(io.swagger.v3.oas.models.media.MediaType) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Aggregations

SerializableParameter (io.swagger.models.parameters.SerializableParameter)13 BodyParameter (io.swagger.models.parameters.BodyParameter)7 PathParameter (io.swagger.models.parameters.PathParameter)6 QueryParameter (io.swagger.models.parameters.QueryParameter)6 Parameter (io.swagger.models.parameters.Parameter)5 FormParameter (io.swagger.models.parameters.FormParameter)4 HeaderParameter (io.swagger.models.parameters.HeaderParameter)4 Swagger (io.swagger.models.Swagger)3 AbstractSerializableParameter (io.swagger.models.parameters.AbstractSerializableParameter)3 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 Property (io.swagger.models.properties.Property)3 RefProperty (io.swagger.models.properties.RefProperty)3 StringProperty (io.swagger.models.properties.StringProperty)3 ArrayList (java.util.ArrayList)3 Test (org.testng.annotations.Test)3 Operation (io.swagger.models.Operation)2 Path (io.swagger.models.Path)2 RefModel (io.swagger.models.RefModel)2 Response (io.swagger.models.Response)2 RefParameter (io.swagger.models.parameters.RefParameter)2