Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwaggerUI passing parameters

I have a swagger.json defined like this:

"/API/GetTieredInventory": {
        "post": {
            "summary": "Get Tiered inventory from ID",
            "description": "Gets Tiered inventory for passed ID/IC combination",
            "produces": [
                "application/json"
            ],
            "parameters": [
                {
                    "name": "id",
                    "in": "path",
                    "description": "ID to retrieve Tiered inventory for",
                    "required": true,
                    "type": "string"
                },
                {
                    "name": "ic",
                    "in": "path",
                    "description": "IC to retrieve Tiered inventory for",
                    "required": true,
                    "type": "string"
                }
            ],
            "responses": {
                "200": {
                    "description": "successful operation"
                },
                "500": {
                    "description": "Internal error"
                }
            }
        }
    }
},

Now, my API takes the parameters like this:

private function GetTieredInventory() {
    if($this->get_request_method() != "POST"){
        $this->response('Error code 405, Method not allowed.',405);
    }
    if(is_array($this->_request['ic'])) {
        $v = array();       
        foreach($this->_request['ic'] as $i) {
            $v[] = "'".$i."'";
        }
        $ic=html_entity_decode(implode(',', $v ));
    } else {
        $ic = "'".$this->_request['ic']."'";
    }
    $id=pg_escape_string($this->_request['id']);

    <SNIP DB CODE>

    try 
    {       
        $results = pg_query($sql);
        if(pg_num_rows($results) == 0) {
            $rows = [];
        }
        else
        {
            $data = pg_fetch_all($results);
            foreach($data as $item)
            {                    
                $rows[$item["ic"]][] = $item;
            }
        }
        pg_free_result($results);
    }
    catch (Exception $e) 
    {
        $err = array("message"=>$e->getMessage(), "code"=> $e->getCode(), "error"=>$e->__toString().",\n".print_r($_REQUEST, true));
        $this->response($this->toJSON($err),500);
    }
    echo json_encode($rows);
}

It doesn't matter what the in value is, I still get a
Notice: Undefined index: ic and Notice: Undefined index: id error returned.

I have tried all three of these for both parameters:

"in": "path",
"in": "query",
"in": "body",

This is how the api is called in my service:

$fields = array(
    'id' => $this->compId,
    'ic'    => $ic
); 
$fields_string = http_build_query($fields);

$url = "/API/GetTieredInventory";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

changed the parameters definition like this because ic can be an array of values:

"parameters": [
    {
        "name": "id",
        "in": "body",
        "description": "ID to retrieve Tiered inventory for",
        "required": true,
        "type": "string"
    },
    {
        "name": "ic",
        "in": "body",
        "schema": {
            "type": "array",
            "items": {
                "type": "string",
                "style": "form"
            }
        },
        "description": "Interchange to retrieve Tiered inventory for",
        "required": true
    }
],

But still same errors...:-(

like image 939
MB34 Avatar asked Jun 04 '18 13:06

MB34


People also ask

How do you pass multiple parameters in Swagger?

If you are trying to send a body with multiple parameters, add an object model in the definitions section and refer it in your body parameter, see below (works with editor.swagger.io):

How do you pass the optional parameter in Swagger?

By default, Swagger treats all request parameters as optional. You can add required: true to mark a parameter as required. Note that path parameters must have required: true , because they are always required. description: Numeric ID of the user to get.

How do you give parameters in URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".


1 Answers

Changing the parameters to this made it work. I am using OAS 2.0

"parameters": [
    {
        "name": "id",
        "in": "formData",
        "description": "ID to retrieve Tiered inventory for",
        "required": true,
        "type": "string"
    },
    {
        "name": "ic",
        "in": "formData",
        "description": "IC to retrieve Tiered inventory for",
        "required": true,
        "type": "string"
    }
],
like image 183
MB34 Avatar answered Sep 18 '22 13:09

MB34