Search in sources :

Example 66 with RefProperty

use of io.swagger.models.properties.RefProperty in project swagger-parser by swagger-api.

the class SwaggerCompatConverter method modelFromExtendedTypedObject.

public Model modelFromExtendedTypedObject(ExtendedTypedObject obj) {
    String type = obj.getType() == null ? null : obj.getType().toString();
    String format = obj.getFormat() == null ? null : obj.getFormat().toString();
    Model output = null;
    if (obj.getRef() != null) {
        output = new RefModel().asDefault(obj.getRef());
    } else {
        if ("array".equals(type)) {
            ArrayModel am = new ArrayModel();
            Items items = obj.getItems();
            type = items.getType() == null ? null : items.getType().toString();
            format = items.getFormat() == null ? null : items.getFormat().toString();
            Property innerType = PropertyBuilder.build(type, format, null);
            if (innerType != null) {
                am.setItems(innerType);
            }
            if (items.getRef() != null) {
                am.setItems(new RefProperty(items.getRef()));
            } else {
                am.setItems(new RefProperty(type));
            }
            output = am;
        } else {
            Property input = PropertyBuilder.build(type, format, null);
            if (input != null) {
                output = PropertyBuilder.toModel(input);
            } else if (!"void".equals(type)) {
                // use ref model
                output = new RefModel().asDefault(type);
            }
        }
    }
    return output;
}
Also used : RefModel(io.swagger.models.RefModel) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ArrayModel(io.swagger.models.ArrayModel) Items(io.swagger.models.apideclaration.Items) ArrayModel(io.swagger.models.ArrayModel) 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) RefProperty(io.swagger.models.properties.RefProperty)

Example 67 with RefProperty

use of io.swagger.models.properties.RefProperty 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;
}
Also used : Path(io.swagger.models.Path) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) Operation(io.swagger.models.Operation) WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) Info(io.swagger.models.Info) WSDLInfo(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLInfo) APIUtil.handleException(org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) MalformedURLException(java.net.MalformedURLException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) ModelImpl(io.swagger.models.ModelImpl) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty)

Example 68 with RefProperty

use of io.swagger.models.properties.RefProperty in project carbon-apimgt by wso2.

the class WSDL11SOAPOperationExtractor method readExtensionModel.

private void readExtensionModel(ModelImpl model, Node node) throws APIManagementException {
    Node baseNode = node.getAttributes().getNamedItem(BASE_ATTR);
    if (baseNode == null) {
        return;
    }
    String baseName = baseNode.getNodeValue();
    String refName;
    String nsName = null;
    if (baseName.contains(":")) {
        refName = baseNode.getNodeValue().split(":")[1];
        nsName = baseNode.getNodeValue().split(":")[0];
    } else {
        refName = baseName;
    }
    model.addProperty(BASE_CONTENT_KEYWORD, new RefProperty(refName));
    if (nsName == null) {
        return;
    }
    if (isElementExist(refName, node.getOwnerDocument())) {
        log.debug(refName + ": is already defined inline.");
        return;
    }
    String ns = node.lookupNamespaceURI(nsName);
    if (ns == null) {
        log.debug("Couldn't find namespace for the " + refName + ". Hence skipping generating model.");
        return;
    }
    Document nsDoc = getBasedXSDofWSDL(ns);
    if (nsDoc == null) {
        nsDoc = loadXSDsfromNamespaces(ns);
    }
    if (nsDoc == null) {
        log.warn("Couldn't find xsd document for namespace " + ns);
    }
    Node refNode = findFirstElementByName(refName, nsDoc);
    if (refNode == null) {
        log.warn("Couldn't find element " + refName + "from namespace " + ns);
    }
    ModelImpl newModel = new ModelImpl();
    // only the refNode is handled. If children needs to handle, it's this method should calls over the children
    addModelDefinition(refNode, newModel, SOAPToRESTConstants.EMPTY_STRING, false, null);
    parameterModelMap.put(newModel.getName(), newModel);
}
Also used : Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) ModelImpl(io.swagger.models.ModelImpl) RefProperty(io.swagger.models.properties.RefProperty)

Example 69 with RefProperty

use of io.swagger.models.properties.RefProperty in project carbon-apimgt by wso2.

the class WSDL11SOAPOperationExtractor method getSoapInputParameterModel.

/**
 * Gets swagger input parameter model for a given soap operation
 *
 * @param bindingOperation soap operation
 * @return list of swagger models for the parameters
 * @throws APIMgtWSDLException
 */
private List<ModelImpl> getSoapInputParameterModel(BindingOperation bindingOperation) throws APIMgtWSDLException {
    List<ModelImpl> inputParameterModelList = new ArrayList<>();
    Operation operation = bindingOperation.getOperation();
    if (operation != null) {
        Input input = operation.getInput();
        if (input != null) {
            Message message = input.getMessage();
            if (message != null) {
                Map map = message.getParts();
                for (Object obj : map.entrySet()) {
                    Map.Entry entry = (Map.Entry) obj;
                    Part part = (Part) entry.getValue();
                    if (part != null) {
                        if (part.getElementName() != null) {
                            inputParameterModelList.add(parameterModelMap.get(part.getElementName().getLocalPart()));
                        } else {
                            if (part.getTypeName() != null && parameterModelMap.containsKey(part.getTypeName().getLocalPart())) {
                                inputParameterModelList.add(parameterModelMap.get(part.getTypeName().getLocalPart()));
                            } else if (part.getTypeName() != null && parameterModelMap.containsKey(message.getQName().getLocalPart())) {
                                ModelImpl model = parameterModelMap.get(message.getQName().getLocalPart());
                                model.addProperty(part.getName(), getPropertyFromDataType(part.getTypeName().getLocalPart()));
                                parameterModelMap.put(model.getName(), model);
                                inputParameterModelList.add(model);
                            } else {
                                ModelImpl model;
                                if (parameterModelMap.get(message.getQName().getLocalPart()) != null) {
                                    model = parameterModelMap.get(message.getQName().getLocalPart());
                                } else {
                                    model = new ModelImpl();
                                    model.setType(ObjectProperty.TYPE);
                                    model.setName(message.getQName().getLocalPart());
                                }
                                if (getPropertyFromDataType(part.getTypeName().getLocalPart()) instanceof RefProperty) {
                                    RefProperty property = (RefProperty) getPropertyFromDataType(part.getTypeName().getLocalPart());
                                    property.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + part.getTypeName().getLocalPart());
                                    model.addProperty(part.getName(), property);
                                } else {
                                    model.addProperty(part.getName(), getPropertyFromDataType(part.getTypeName().getLocalPart()));
                                }
                                parameterModelMap.put(model.getName(), model);
                                inputParameterModelList.add(model);
                            }
                        }
                    }
                }
            }
        }
    }
    return inputParameterModelList;
}
Also used : Input(javax.wsdl.Input) Message(javax.wsdl.Message) Part(javax.wsdl.Part) ArrayList(java.util.ArrayList) Operation(javax.wsdl.Operation) SOAP12Operation(javax.wsdl.extensions.soap12.SOAP12Operation) WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) BindingOperation(javax.wsdl.BindingOperation) SOAPOperation(javax.wsdl.extensions.soap.SOAPOperation) WSDLOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLOperation) ModelImpl(io.swagger.models.ModelImpl) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) RefProperty(io.swagger.models.properties.RefProperty)

Example 70 with RefProperty

use of io.swagger.models.properties.RefProperty in project cxf by apache.

the class JaxRs2Extension method enforcePrimitive.

private Property enforcePrimitive(final Property in, final int level) {
    if (in instanceof RefProperty) {
        return new StringProperty();
    }
    if (in instanceof ArrayProperty) {
        if (level == 0) {
            final ArrayProperty array = (ArrayProperty) in;
            array.setItems(enforcePrimitive(array.getItems(), level + 1));
        } else {
            return new StringProperty();
        }
    }
    return in;
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) StringProperty(io.swagger.models.properties.StringProperty) RefProperty(io.swagger.models.properties.RefProperty)

Aggregations

RefProperty (io.swagger.models.properties.RefProperty)74 Property (io.swagger.models.properties.Property)50 ArrayProperty (io.swagger.models.properties.ArrayProperty)46 StringProperty (io.swagger.models.properties.StringProperty)35 Test (org.testng.annotations.Test)35 Model (io.swagger.models.Model)23 MapProperty (io.swagger.models.properties.MapProperty)20 ModelImpl (io.swagger.models.ModelImpl)18 Response (io.swagger.models.Response)18 IntegerProperty (io.swagger.models.properties.IntegerProperty)18 Operation (io.swagger.models.Operation)17 RefModel (io.swagger.models.RefModel)17 LongProperty (io.swagger.models.properties.LongProperty)14 Path (io.swagger.models.Path)12 Swagger (io.swagger.models.Swagger)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 ArrayModel (io.swagger.models.ArrayModel)11 BodyParameter (io.swagger.models.parameters.BodyParameter)11 Map (java.util.Map)11