Swagger generator doesn't recognize controller - node.js

I have this yaml file:
---
swagger: "2.0"
info:
version: 0.1.0
title: "My API"
host: localhost:3000
basePath: /api
schemes:
- http
paths:
/weather:
get:
x-swagger-router-controller: "weatherController"
description: "Returns current weather in the specified city to the caller"
operationId: getWeather
parameters:
- name: city
in: query
description: "The city you want weather for in the form city,state,country"
required: true
type: "string"
When I run swagger-codegen-cli, generates /api/Default.js and /api/DefaultService.js , but no /api/weatherController.js.
I've tried also with this:
---
swagger: "2.0"
info:
version: 0.1.0
title: "My API"
host: localhost:3000
basePath: /api
schemes:
- http
paths:
/weather:
x-swagger-router-controller: "weatherController"
get:
description: "Returns current weather in the specified city to the caller"
operationId: getWeather
parameters:
- name: city
in: query
description: "The city you want weather for in the form city,state,country"
required: true
type: "string"
And I run the generator with this command:
java -jar swagger-codegen-cli.jar generate -l nodejs-server -o output -i api.yaml
How can I do this?

I think that's a bug, it would be great if you could file that on the swagger-codegen project. But as a work around, consider adding a tag:
/weather:
x-swagger-router-controller: "weatherController"
get:
tags:
- weatherController
description: "Returns current weather in the specified city to the caller"
operationId: getWeather
parameters:
- name: city
in: query
description: "The city you want weather for in the form city,state,country"
required: true
type: "string"

Related

Generate swagger server that will server local file

I'm trying to write a yaml that will generate a nodejs stub-server serving a file. Swagger codegen generates server that replies nothing.
swagger: "2.0"
info:
description: ""
version: "1"
title: "Swagger example"
host: "localhost:3200"
basePath: "/v2"
schemes:
- "http"
paths:
/getImage:
get:
tags:
- "image"
summary: "Returns image"
description: ""
operationId: "getImage"
produces:
- "application/octet-stream"
responses:
"200":
description: "successful operation"
schema:
type: "file"
example:
sampleFile:
summary: "A sample file"
externalValue: "http://github.com/topheroes/topheroes.github.io/raw/master/img/dragon.png"
"400":
description: "Invalid value"
Is there a way to generate a stub that will serve a local or a random binary (image) file?
I tried type: string and format: binary, I tried url in example. Nothing seems to work.
Please advice

"Unknown Error" while creating API spec in Swagger

Hi I'm creating a Sample API Spec using swagger as below :
I keep getting "Unknown Error on "Items:" line
swagger: "2.0"
info:
version: "0.0.1"
title: Todo API
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/:
get:
description: "this endpoint returns list of todo events"
operationId: "GetAllTodos"
parameters: []
responses:
200:
description: "an array of all the present todos"
schema:
type: "array"
items:
$ref: "#definitions/Todo"
x-swagger-router-controller: "GetAllTodos"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
Todo:
type: "object"
properties:
todo_id:
type: "integer"
description: "id of the todo event"
todo:
type: "string"
description: "The todo item"
datecreated:
type: "string"
format: "date-time"
description: "timestamp when the todo event was created"
author:
type: "string"
description: "owner of the todo event"
duedate:
type: "string"
format: "date-time"
description: "time by when the todo event should be completed"
completed:
type: "boolean"
description: "status of todo completion"
Change
$ref: "#definitions/Todo"
to
$ref: "#/definitions/Todo"
Note the / after #.
Also, you are using an old version of Swagger Editor (2.x), which is no longer maintained. Consider using the latest version at http://editor.swagger.io.

Cannot resolve the configured swagger-router handler: movie_get

I'm using Swagger with NodeJS, and when I test an example I have this error.
Here is my YAML:
swagger: "2.0"
info:
version: "0.0.1"
title: Hello World App
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/movie:
# our controller name
x-swagger-router-controller: movie
get:
description: get the movies list
# define the type of response for Success "200" and Error
responses:
"200":
description: Success
schema:
$ref: "#/definitions/GetMoviesListResponse"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
GetMoviesListResponse:
required:
- movies
properties:
# The array of movies
movies:
type: array
items:
type: object
properties:
id:
type: string
title:
type: string
year:
type: number
ErrorResponse:
required:
- message
properties:
message:
type: string
Given your YAML, and assuming you're using swagger-tools with the options given in their tutorials, x-swagger-router-controller: movie will route any requests to GET /movie to a function called get exported from the module /controllers/movie.js in your project. Have you set that up?
Also check your swagger options to check that the correct controllers path is included, e.g. controllers: `${__dirname}`/controllers.

Swagger error: Relative paths to the individual endpoints

Using node v7.10.0 and npm 4.2.0.
I have created a nodejs 'Hello World' project and I was trying to reorganize the swagger.yaml taking the paths out of the main yaml file into a sepparate paths.yaml file:
This is my api/swagger/swagger.yaml:
swagger: "2.0"
info:
version: "0.0.1"
title: Hello World App
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
$ref: './paths.yaml'
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
HelloWorldResponse:
required:
- message
properties:
message:
type: string
ErrorResponse:
required:
- message
properties:
message:
type: string
and this is the api/swagger/paths.yaml that I'm referencing to:
/hello:
# binds a127 app logic to a route
x-swagger-router-controller: hello_world
get:
description: Returns 'Hello' to the caller
# used as the method name of the controller
operationId: hello
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/HelloWorldResponse"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
I get the following Swagger error:
Relative paths to the individual endpoints. They must be relative to the 'basePath'.
Reference could not be resolved: ./paths.yaml
Both files 'swagger.yaml' and 'paths.yaml' are placed in the same directory 'api/swagger/'.
I've tried to reference paths.yaml in different ways:
paths:
$ref: 'paths.yaml'
or
paths:
$ref: './api/swagger/paths.yaml'
But I get the same error. Is it possible to reference the paths in a sepparate yaml file?

Swagger validation failed in Yaml file

I have a swagger yaml specification like this :
swagger: "2.0"
info:
version: "0.0.1"
title: Chat API
# during dev, should point to your local machine
host: localhost:5000
# basePath prefixes all resource paths
basePath: /api/v2
#
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
paths:
/room:
post:
summary: Get room
operationId: getRoom
tags:
- room
parameters:
-
name: token
in: header
description: "token to be passed as a header"
default: "ZjE4YjMxNmY3OGEzNDMyN2JiYjJmYTQwMDBjODg4OWM="
required: true
-
name: room_id
in: body
description: "get room"
required: true
schema:
$ref: "#/definitions/Room"
definitions:
Room:
required:
- room_id
properties:
room_id:
type: string
This yaml file is compiled well without the header part. If I include the header in the paramerts . The nodejs app keep throwing : "Swagger validation errors"
-
name: token
in: header
description: "token to be passed as a header"
default: "ZjE4YjMxNmY3OGEzNDMyN2JiYjJmYTQwMDBjODg4OWM="
required: true
I don't know what was wrong in this part. I want to add the header to this spec file.
You simply need to add the type attribute. Swagger doesn't know if this is a string, an integer, etc. (although one could say the default explains it).
- name: token
in: header
description: "token to be passed as a header"
default: "ZjE4YjMxNmY3OGEzNDMyN2JiYjJmYTQwMDBjODg4OWM="
required: true
type: string

Resources