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;
}
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;
}
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;
}
Aggregations