use of io.swagger.inflector.examples.models.Example in project carbon-apimgt by wso2.
the class OAS2Parser method getSchemaExample.
/**
* This method generates Sample/Mock payloads of Schema Examples for operations in the swagger definition
* @param model model
* @param definitions definitions
* @return Example Json
*/
private String getSchemaExample(Model model, Map<String, Model> definitions, HashSet<String> strings) {
Example example = ExampleBuilder.fromModel("Model", model, definitions, new HashSet<String>());
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
return Json.pretty(example);
}
use of io.swagger.inflector.examples.models.Example in project carbon-apimgt by wso2.
the class SequenceGenerator method generateSequencesFromSwagger.
/**
* Generates in/out sequences from the swagger given
*
* @param swaggerStr swagger string
* @throws APIManagementException
*/
public static List<SOAPToRestSequence> generateSequencesFromSwagger(String swaggerStr) throws APIManagementException {
List<SOAPToRestSequence> sequences = new ArrayList<SOAPToRestSequence>();
Swagger swagger = new SwaggerParser().parse(swaggerStr);
Map<String, Model> definitions = swagger.getDefinitions();
// Configure serializers
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
Yaml.mapper().registerModule(simpleModule);
Map<String, Path> paths = swagger.getPaths();
for (String pathName : paths.keySet()) {
Path path = paths.get(pathName);
Map<HttpMethod, Operation> operationMap = path.getOperationMap();
for (HttpMethod httpMethod : operationMap.keySet()) {
boolean isResourceFromWSDL = false;
Map<String, String> parameterJsonPathMapping = new HashMap<>();
Map<String, String> queryParameters = new HashMap<>();
Operation operation = operationMap.get(httpMethod);
String operationId = operation.getOperationId();
// get vendor extensions
Map<String, Object> vendorExtensions = operation.getVendorExtensions();
Object vendorExtensionObj = vendorExtensions.get("x-wso2-soap");
String soapAction = SOAPToRESTConstants.EMPTY_STRING;
String namespace = SOAPToRESTConstants.EMPTY_STRING;
String soapVersion = SOAPToRESTConstants.EMPTY_STRING;
if (vendorExtensionObj != null) {
soapAction = (String) ((LinkedHashMap) vendorExtensionObj).get("soap-action");
namespace = (String) ((LinkedHashMap) vendorExtensionObj).get("namespace");
soapVersion = (String) ((LinkedHashMap) vendorExtensionObj).get(SOAPToRESTConstants.Swagger.SOAP_VERSION);
soapMessageType = (String) ((LinkedHashMap) vendorExtensionObj).get(SOAPToRESTConstants.Swagger.SOAP_MESSAGE_TYPE);
soapStyle = (String) ((LinkedHashMap) vendorExtensionObj).get(SOAPToRESTConstants.Swagger.SOAP_STYLE);
isResourceFromWSDL = true;
}
String soapNamespace = SOAPToRESTConstants.SOAP12_NAMSPACE;
if (StringUtils.isNotBlank(soapVersion) && SOAPToRESTConstants.SOAP_VERSION_11.equals(soapVersion)) {
soapNamespace = SOAPToRESTConstants.SOAP11_NAMESPACE;
}
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>());
replaceNullWithStringExample(example);
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", e);
}
}
}
}
if (parameter instanceof QueryParameter) {
String type = ((QueryParameter) parameter).getType();
queryParameters.put(name, type);
}
}
// populates body parameter json paths and query parameters to generate api sequence parameters
populateParametersFromOperation(operation, definitions, parameterJsonPathMapping, queryParameters);
Map<String, String> payloadSequence = createPayloadFacXMLForOperation(parameterJsonPathMapping, queryParameters, namespace, SOAPToRESTConstants.EMPTY_STRING, operationId, definitions);
try {
String[] propAndArgElements = getPropertyAndArgElementsForSequence(parameterJsonPathMapping, queryParameters);
if (log.isDebugEnabled()) {
log.debug("properties string for the generated sequence: " + propAndArgElements[0]);
log.debug("arguments string for the generated sequence: " + propAndArgElements[1]);
}
org.json.simple.JSONArray arraySequenceElements = new org.json.simple.JSONArray();
// gets array elements for the sequence to be used
getArraySequenceElements(arraySequenceElements, parameterJsonPathMapping);
Map<String, String> sequenceMap = new HashMap<>();
sequenceMap.put("args", propAndArgElements[0]);
sequenceMap.put("properties", propAndArgElements[1]);
sequenceMap.put("sequence", payloadSequence.get(operationId));
RESTToSOAPMsgTemplate template = new RESTToSOAPMsgTemplate();
String inSequence = template.getMappingInSequence(sequenceMap, operationId, soapAction, namespace, soapNamespace, arraySequenceElements);
String outSequence = template.getMappingOutSequence();
if (isResourceFromWSDL) {
SOAPToRestSequence inSeq = new SOAPToRestSequence(httpMethod.toString().toLowerCase(), pathName, inSequence, Direction.IN);
sequences.add(inSeq);
SOAPToRestSequence outSeq = new SOAPToRestSequence(httpMethod.toString().toLowerCase(), pathName, outSequence, Direction.OUT);
sequences.add(outSeq);
}
} catch (APIManagementException e) {
handleException("Error when generating sequence property and arg elements for soap operation: " + operationId, e);
}
}
}
return sequences;
}
use of io.swagger.inflector.examples.models.Example 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);
}
}
}
Aggregations