use of io.swagger.models.parameters.QueryParameter 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.QueryParameter in project carbon-apimgt by wso2.
the class SOAPOperationBindingUtils method getGeneratedSwaggerFromWSDL.
/**
* Generate the swagger from the WSDL info
*
* @param wsdlInfo WSDLInfo object which has parsed WSDL data
* @return Generated the swagger from the WSDL info
* @throws APIManagementException if an error occurs when generating swagger
*/
private static String getGeneratedSwaggerFromWSDL(WSDLInfo wsdlInfo) throws APIManagementException {
Set<WSDLSOAPOperation> operations;
Map<String, ModelImpl> paramModelMap;
String swaggerStr = SOAPToRESTConstants.EMPTY_STRING;
operations = wsdlInfo.getSoapBindingOperations();
paramModelMap = wsdlInfo.getParameterModelMap();
populateSoapOperationParameters(operations);
Swagger swaggerDoc = new Swagger();
for (WSDLSOAPOperation operation : operations) {
Path path = new Path();
Operation op = new Operation();
List<ModelImpl> inputParameterModel = operation.getInputParameterModel();
List<ModelImpl> outputParameterModel = operation.getOutputParameterModel();
if (HTTPConstants.HTTP_METHOD_GET.equals(operation.getHttpVerb())) {
for (ModelImpl input : inputParameterModel) {
if (input != null && operation.getName().equalsIgnoreCase(input.getName())) {
Map<String, Property> properties = input.getProperties();
if (properties != null) {
for (String property : properties.keySet()) {
QueryParameter param = new QueryParameter();
param.setName(property);
param.setType(properties.get(property).getType());
op.addParameter(param);
}
}
inputParameterModel.remove(input);
break;
}
}
} else {
// adding body parameter
BodyParameter param = new BodyParameter();
param.setName(APIConstants.OperationParameter.PAYLOAD_PARAM_NAME);
param.setIn(APIConstants.OperationParameter.PAYLOAD_PARAM_TYPE);
param.setRequired(true);
RefModel model = new RefModel();
model.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + operation.getName() + SOAPToRESTConstants.Swagger.INPUT_POSTFIX);
param.setSchema(model);
op.addParameter(param);
}
// adding response
Response response = new Response();
RefProperty refProperty = new RefProperty();
refProperty.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + operation.getName() + SOAPToRESTConstants.Swagger.OUTPUT_POSTFIX);
response.setSchema(refProperty);
response.setDescription(SOAPToRESTConstants.EMPTY_STRING);
op.addResponse("default", response);
op.setOperationId(operation.getSoapBindingOpName());
// setting vendor extensions
Map<String, String> extensions = new HashMap<>();
extensions.put(SOAPToRESTConstants.Swagger.SOAP_ACTION, operation.getSoapAction());
extensions.put(SOAPToRESTConstants.Swagger.SOAP_OPERATION, operation.getSoapBindingOpName());
extensions.put(SOAPToRESTConstants.Swagger.NAMESPACE, operation.getTargetNamespace());
if (wsdlInfo.isHasSoap12BindingOperations()) {
extensions.put(SOAPToRESTConstants.Swagger.SOAP_VERSION, SOAPToRESTConstants.SOAP_VERSION_12);
} else if (wsdlInfo.hasSoapBindingOperations()) {
extensions.put(SOAPToRESTConstants.Swagger.SOAP_VERSION, SOAPToRESTConstants.SOAP_VERSION_11);
}
extensions.put(SOAPToRESTConstants.Swagger.SOAP_STYLE, operation.getStyle());
extensions.put(SOAPToRESTConstants.Swagger.SOAP_MESSAGE_TYPE, operation.getMessageType());
op.setVendorExtension(SOAPToRESTConstants.Swagger.WSO2_SOAP, extensions);
if (!HTTPConstants.HTTP_METHOD_GET.equals(operation.getHttpVerb())) {
ModelImpl inputModel = new ModelImpl();
inputModel.setName(operation.getName() + SOAPToRESTConstants.Swagger.INPUT_POSTFIX);
inputModel.setType(ObjectProperty.TYPE);
Map<String, Property> inputPropertyMap = new HashMap<>();
for (ModelImpl input : inputParameterModel) {
if (input != null && input.getProperties() != null) {
RefProperty inputRefProp;
if (input.getProperties().containsKey(input.getName())) {
inputRefProp = (RefProperty) input.getProperties().get(input.getName());
} else {
inputRefProp = new RefProperty();
inputRefProp.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + input.getName());
}
inputPropertyMap.put(input.getName(), inputRefProp);
}
}
inputModel.setProperties(inputPropertyMap);
swaggerDoc.addDefinition(operation.getName() + SOAPToRESTConstants.Swagger.INPUT_POSTFIX, inputModel);
}
ModelImpl outputModel = new ModelImpl();
outputModel.setName(operation.getName() + SOAPToRESTConstants.Swagger.OUTPUT_POSTFIX);
outputModel.setType(ObjectProperty.TYPE);
Map<String, Property> outputPropertyMap = new HashMap<>();
for (ModelImpl output : outputParameterModel) {
if (output != null && output.getProperties() != null) {
RefProperty outputRefProp;
if (output.getProperties().containsKey(output.getName())) {
outputRefProp = (RefProperty) output.getProperties().get(output.getName());
} else {
outputRefProp = new RefProperty();
outputRefProp.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + output.getName());
}
outputPropertyMap.put(output.getName(), outputRefProp);
}
}
outputModel.setProperties(outputPropertyMap);
swaggerDoc.addDefinition(operation.getName() + SOAPToRESTConstants.Swagger.OUTPUT_POSTFIX, outputModel);
path.set(operation.getHttpVerb().toLowerCase(), op);
swaggerDoc.path("/" + operation.getName(), path);
Info info = new Info();
info.setVersion(SOAPToRESTConstants.EMPTY_STRING);
info.setTitle(SOAPToRESTConstants.EMPTY_STRING);
swaggerDoc.info(info);
}
if (paramModelMap != null) {
for (String propertyName : paramModelMap.keySet()) {
swaggerDoc.addDefinition(propertyName, paramModelMap.get(propertyName));
}
}
try {
swaggerStr = Json.pretty(swaggerDoc);
} catch (Exception e) {
String msg = "Error occurred while deserialize swagger model.";
handleException(msg, e);
}
if (log.isDebugEnabled()) {
log.debug(swaggerStr);
}
return swaggerStr;
}
use of io.swagger.models.parameters.QueryParameter in project carbon-apimgt by wso2.
the class SequenceGenerator method populateParametersFromOperation.
private static void populateParametersFromOperation(Operation operation, Map<String, Model> definitions, Map<String, String> parameterJsonPathMapping, Map<String, String> queryParameters) {
List<Parameter> parameters = operation.getParameters();
for (Parameter parameter : parameters) {
String name = parameter.getName();
if (parameter instanceof BodyParameter) {
Model schema = ((BodyParameter) parameter).getSchema();
if (schema instanceof RefModel) {
String $ref = ((RefModel) schema).get$ref();
if (StringUtils.isNotBlank($ref)) {
String defName = $ref.substring("#/definitions/".length());
Model model = definitions.get(defName);
Example example = ExampleBuilder.fromModel(defName, model, definitions, new HashSet<String>());
String jsonExample = Json.pretty(example);
try {
org.json.JSONObject json = new org.json.JSONObject(jsonExample);
SequenceUtils.listJson(json, parameterJsonPathMapping);
} catch (JSONException e) {
log.error("Error occurred while generating json mapping for the definition: " + defName, e);
}
}
}
}
if (parameter instanceof QueryParameter) {
String type = ((QueryParameter) parameter).getType();
queryParameters.put(name, type);
}
}
}
use of io.swagger.models.parameters.QueryParameter in project java-chassis by ServiceComb.
the class TestQueryProcessorCreator method testCreateNullAsEmpty.
@SuppressWarnings("UnusedAssignment")
@Test
public void testCreateNullAsEmpty() throws Exception {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
ArchaiusUtils.setProperty("servicecomb.rest.parameter.query.emptyAsNull", "true");
ParamValueProcessorCreator creator = ParamValueProcessorCreatorManager.INSTANCE.findValue(QueryProcessorCreator.PARAMTYPE);
Parameter parameter = new QueryParameter();
parameter.setName("query");
ParamValueProcessor processor = creator.create(parameter, String.class);
Assert.assertEquals(QueryProcessor.class, processor.getClass());
Mockito.when(request.getParameter("query")).thenReturn("Hello");
String result = (String) processor.getValue(request);
Assert.assertEquals("Hello", result);
Mockito.when(request.getParameter("query")).thenReturn("");
result = (String) processor.getValue(request);
Assert.assertEquals(null, result);
Mockito.when(request.getParameter("query")).thenReturn(null);
result = (String) processor.convertValue(null, TypeFactory.defaultInstance().constructType(String.class));
result = (String) processor.getValue(request);
Assert.assertEquals(null, result);
}
use of io.swagger.models.parameters.QueryParameter in project java-chassis by ServiceComb.
the class TestQueryProcessorCreator method testCreate.
@Test
public void testCreate() {
ParamValueProcessorCreator creator = ParamValueProcessorCreatorManager.INSTANCE.findValue(QueryProcessorCreator.PARAMTYPE);
Parameter parameter = new QueryParameter();
parameter.setName("query");
ParamValueProcessor processor = creator.create(parameter, String.class);
Assert.assertEquals(QueryProcessor.class, processor.getClass());
String result = (String) processor.convertValue("Hello", TypeFactory.defaultInstance().constructType(String.class));
Assert.assertEquals("Hello", result);
result = (String) processor.convertValue("", TypeFactory.defaultInstance().constructType(String.class));
Assert.assertEquals("", result);
result = (String) processor.convertValue(null, TypeFactory.defaultInstance().constructType(String.class));
Assert.assertEquals(null, result);
}
Aggregations