Showing posts with label swagger-ui. Show all posts
Showing posts with label swagger-ui. Show all posts

Friday, June 15, 2018

SwaggerUI passing parameters

Leave a Comment

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...:-(

1 Answers

Answers 1

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"     } ], 
Read More

Thursday, January 18, 2018

Change default port for swagger-ui dist bundle

Leave a Comment

How would you go about changing the default port of swagger-ui dist version?

By default it listens to requests on port 8080. I want it to listen to some other port. The use case is that we want to have a couple of dists running on our host but listening on different ports.

Is this possible or do you actually need to do some more complicated setup?

We run it via node js default package:

{   "name": "dist",   "version": "1.0.0",   "description": "",   "main": "swagger-ui-bundle.js",   "scripts": {     "start": "http-server"   },   "keywords": [],   "author": "",   "license": "ISC" } 

2 Answers

Answers 1

From here and here, you may read:

You should be able to use override the port in the basePath

Answers 2

The simples solution that I know Is just to use docker and map port by -p 80:8080:

https://hub.docker.com/r/swaggerapi/swagger-ui/

Read More

Thursday, December 28, 2017

Swagger does not show real error message

Leave a Comment

We are on .NET WebAPI framework and we are using swagger on top of web api for annotations and out of the box UI experience. So far, it just has been working great. However, when I return an error from WebAPI(Http 400) as following code:

return BadRequest("Some error");

However, when I do that, it seems like Swagger is not able to parse the error properly. It basically, show response 0. Which is wrong for sure because when I go to Chrome DevTools, I see that my request had a response of HTTP 400 as I expected.

How can I configure Swagger so that it is able to show the errors correctly? Or should I be throwing my errors a little bit differently to be able to see it in swagger?

1 Answers

Answers 1

You can annotate your actions like this

[ProducesResponseType(typeof(ResponseObjectTypeHere), 200)] [ProducesResponseType(typeof(ErrorResponseObjectTypeHere), 400)] 

You can also add something like this in the xml documentation if you enable swagger to read and use xml documentation

/// <response code="200">Description</response> /// <response code="400">Description of error</response> 

I used those in the .net core web api project with swagger, should be the same for regular .net web api project as well.

Read More

Sunday, September 24, 2017

Getting empty paths key in swagger when deployed. Local is working

Leave a Comment

I am using swagger-jsdoc. Everything seems fine and I am getting the json till I am using localhost, when I use live url, it's giving me nothing in the paths keys ie.

{     "info": {         "title": "App API",         "version":"0.0.0",         "description":"Server API documentation"     },     "host":"something.com",     "basePath":"/",     "schemes":["https"],     "swagger":"2.0",     "paths":{},     "definitions":{},     "responses":{},     "parameters":{},     "securityDefinitions":{},     "tags":[] } 

This is what I am getting on live. The same code is working on localhost/swagger.json, but not with https://something.com/swagger.json

var swaggerJSDoc = require('swagger-jsdoc');  var swaggerOptions = {     swaggerDefinition: config.swaggerDefinition || {         info: { // API informations (required)             title: 'Hello World', // Title (required)             version: '1.0.0', // Version (required)             description: 'A sample API', // Description (optional)         },         host: 'localhost:3000', // Host (optional)         basePath: '/', // Base path (optional)     },     apis: ['./routes/*.js'], // Path to the API docs };  var swaggerSpec = swaggerJSDoc(swaggerOptions);  router.get('/swagger.json', function(req, res) {   res.setHeader('Content-Type', 'application/json');   res.send(swaggerSpec); }); 

1 Answers

Answers 1

The problem might be a problem with your host option in config.swaggerDefinition. Is it set to the correct domain?

Read More

Thursday, April 14, 2016

how to add arrays and examples in my Swagger UI using Swagger express?

Leave a Comment

To sum it up, I basically am trying to make a swagger Ui based on this "tutorial" : https://github.com/fliptoo/swagger-express

Unfortunately, it doesn't describe at all how to add examples for the body and how to add arrays as well.

The code for the documentation I have right now is :

/** * @swagger * path: /animal/dogs * operations:  *   -  httpMethod: POST *      summary: Add several dogs to the animal shelter *      notes: Returns results of the post *      responseClass: Shelter *      nickname : shelterPostWithDogs *      consumes:  *        - application/json *      parameters: *        - name: body *          schema: true *          description: Dogs object that needs to be added *          paramType: body *          required: true *          dataType: Dog */ 

And I have at the bottom :

/** * @swagger * models: *  Dog: *      id: Dog *      properties: *          dog_id: *              required: true *              type: int *  Shelter: *      id: Shelter *      properties: *          shelter_id: *              type: int *          location: *              type: string                              */ 

Unfortunately, with this, I have this in my Model :

Dog {     dog_id (int) } 

I would like instead to have this:

Dog {     [         { dog_id(int)}     ] } 

What do I have to change to get this result?

Bonus : How do I add examples to my model? I would like something similar to this one : http://petstore.swagger.io/#!/pet/addPet Unfortunately, I don't know how to do it in swagger express

1 Answers

Answers 1

I am also very new to swagger-express but one thing I will suggest, you have to use type as "array" in models definition:

Dog:    id: Dog       properties:           dog_id:               required: true               type: **array** 

Hope this will help you.

Thanks!

Read More