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
Related
I'm setting up a new server and my goal is to implement multipart/form-data in OpenApi3.0's yaml file. I encounter "should NOT have additional properties (consume)" error in Node.js and wanna know how to fix this error or how to implement multipart/form-data in OpenApi3.0's yaml file?
This is my OpenApi3.0's yaml file to implement this goal, it will report the error I mentioned above.
openapi: 3.0.1
info:
title: myapp
description: My cool app
version: 1.0.0
servers:
- url: /api/v1/user
tags:
- name: User
description: User Operations
paths:
/onboarding/signature:
post:
tags:
- User
description: Onboarding Upload Signature API - with parameters user's email and image file
requestBody:
description: Request Body {email, image}
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/onboardingSignature'
required: true
responses:
200:
description: OK
201:
description: Created
400:
description: Bad Request
500:
description: Internal Server Error
components:
schemas:
onboardingSignature:
description: Onboarding Signature File
type: object
properties:
email:
type: string
image:
format: binary
I expect the implementation of uploading file in Swagger use multipart/form-data format.
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.
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?
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"
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