Search in sources :

Example 81 with BodyParameter

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

the class RelativeReferenceTest method testIssue213.

@Test
public void testIssue213() throws Exception {
    new Expectations() {

        {
            RemoteUrl.urlToString("http://foo.bar.com/swagger.json", null);
            times = 1;
            result = spec;
            RemoteUrl.urlToString("http://foo.bar.com/path/samplePath.yaml", null);
            times = 1;
            result = samplePath;
        }
    };
    Swagger swagger = new SwaggerParser().read("http://foo.bar.com/swagger.json");
    assertNotNull(swagger.getPath("/samplePath").getGet());
    assertNotNull(swagger.getPath("/samplePath").getGet().getParameters().get(0));
    Parameter param = swagger.getPath("/samplePath").getGet().getParameters().get(0);
    assertTrue(param instanceof BodyParameter);
    BodyParameter bp = (BodyParameter) param;
    assertNotNull(bp.getSchema());
}
Also used : Expectations(mockit.Expectations) Swagger(io.swagger.models.Swagger) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.testng.annotations.Test)

Example 82 with BodyParameter

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

the class SwaggerParserTest method testIssue360.

@Test
public void testIssue360() {
    SwaggerParser parser = new SwaggerParser();
    final Swagger swagger = parser.read("src/test/resources/issue_360.yaml");
    assertNotNull(swagger);
    Parameter parameter = swagger.getPath("/pets").getPost().getParameters().get(0);
    assertNotNull(parameter);
    assertTrue(parameter instanceof BodyParameter);
    BodyParameter bp = (BodyParameter) parameter;
    assertNotNull(bp.getSchema());
    Model model = bp.getSchema();
    assertTrue(model instanceof ModelImpl);
    ModelImpl impl = (ModelImpl) model;
    assertNotNull(impl.getProperties().get("foo"));
    Map<String, Object> extensions = bp.getVendorExtensions();
    assertNotNull(extensions);
    assertNotNull(extensions.get("x-examples"));
    Object o = extensions.get("x-examples");
    assertTrue(o instanceof Map);
    Map<String, Object> on = (Map<String, Object>) o;
    Object jn = on.get("application/json");
    assertTrue(jn instanceof Map);
    Map<String, Object> objectNode = (Map<String, Object>) jn;
    assertEquals(objectNode.get("foo"), "bar");
    Parameter stringBodyParameter = swagger.getPath("/otherPets").getPost().getParameters().get(0);
    assertTrue(stringBodyParameter instanceof BodyParameter);
    BodyParameter sbp = (BodyParameter) stringBodyParameter;
    assertTrue(sbp.getRequired());
    assertEquals(sbp.getName(), "simple");
    Model sbpModel = sbp.getSchema();
    assertTrue(sbpModel instanceof ModelImpl);
    ModelImpl sbpModelImpl = (ModelImpl) sbpModel;
    assertEquals(sbpModelImpl.getType(), "string");
    assertEquals(sbpModelImpl.getFormat(), "uuid");
    Parameter refBodyParameter = swagger.getPath("/evenMorePets").getPost().getParameters().get(0);
    assertTrue(refBodyParameter instanceof BodyParameter);
    BodyParameter ref = (BodyParameter) refBodyParameter;
    assertTrue(ref.getRequired());
    assertEquals(ref.getName(), "simple");
    Model refModel = ref.getSchema();
    assertTrue(refModel instanceof RefModel);
    RefModel refModelImpl = (RefModel) refModel;
    assertEquals(refModelImpl.getSimpleRef(), "Pet");
}
Also used : RefModel(io.swagger.models.RefModel) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) SerializableParameter(io.swagger.models.parameters.SerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FormParameter(io.swagger.models.parameters.FormParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 83 with BodyParameter

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

the class SwaggerDeserializerTest method testIssue343Parameter.

@Test
public void testIssue343Parameter() {
    String yaml = "swagger: '2.0'\n" + "info:\n" + "  description: 'bleh'\n" + "  version: '2.0.0'\n" + "  title: 'Test'\n" + "paths:\n" + "  /foo:\n" + "    post:\n" + "      parameters:\n" + "        - in: query\n" + "          name: skip\n" + "          type: integer\n" + "          format: int32\n" + "          multipleOf: 3\n" + "        - in: body\n" + "          name: body\n" + "          required: true\n" + "          schema:\n" + "            type: object\n" + "            additionalProperties:\n" + "              type: string\n" + "      responses:\n" + "        200:\n" + "          description: 'OK'\n" + "definitions:\n" + "  Fun:\n" + "    properties:\n" + "      id:\n" + "        type: integer\n" + "        format: int32\n" + "        multipleOf: 5\n" + "      mySet:\n" + "        type: array\n" + "        uniqueItems: true\n" + "        items:\n" + "          type: string";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    Swagger swagger = result.getSwagger();
    QueryParameter qp = (QueryParameter) swagger.getPath("/foo").getPost().getParameters().get(0);
    assertEquals(qp.getMultipleOf(), 3.0);
    BodyParameter bp = (BodyParameter) swagger.getPath("/foo").getPost().getParameters().get(1);
    ModelImpl schema = (ModelImpl) bp.getSchema();
    assertTrue(schema.getAdditionalProperties() != null);
    IntegerProperty id = (IntegerProperty) swagger.getDefinitions().get("Fun").getProperties().get("id");
    assertEquals(id.getMultipleOf(), new BigDecimal("5"));
    ArrayProperty ap = (ArrayProperty) swagger.getDefinitions().get("Fun").getProperties().get("mySet");
    assertTrue(ap.getUniqueItems());
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 84 with BodyParameter

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

use of io.swagger.models.parameters.BodyParameter 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)

Aggregations

BodyParameter (io.swagger.models.parameters.BodyParameter)96 Test (org.testng.annotations.Test)42 Parameter (io.swagger.models.parameters.Parameter)34 Model (io.swagger.models.Model)30 Operation (io.swagger.models.Operation)28 QueryParameter (io.swagger.models.parameters.QueryParameter)26 ModelImpl (io.swagger.models.ModelImpl)23 Swagger (io.swagger.models.Swagger)23 PathParameter (io.swagger.models.parameters.PathParameter)23 RefModel (io.swagger.models.RefModel)22 ArrayModel (io.swagger.models.ArrayModel)21 Property (io.swagger.models.properties.Property)21 Path (io.swagger.models.Path)20 RefProperty (io.swagger.models.properties.RefProperty)19 StringProperty (io.swagger.models.properties.StringProperty)18 ArrayProperty (io.swagger.models.properties.ArrayProperty)13 HeaderParameter (io.swagger.models.parameters.HeaderParameter)12 Response (io.swagger.models.Response)10 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 ArrayList (java.util.ArrayList)9