I am writing a schema for two get routes where the result is an object like
{
"id":"49077acb6ac8",
"info":
{
"name":"test"
}
}
Actually I got this :
/*
* #swagger
* definitions:
* getVCenter:
* id:
* type: string
* format: uuid
* info:
* type: object
* properties:
* name:
* type: string
* fullName:
* type: string
* vendor:
* type: string
* /v1/vcenters/:
* get:
* tags:
* - vcenters
* summary: Get availables vCenters.
* description: Get a list of availables vCenters.
* produces:
* - application/json
* responses:
* 200:
* description: an array of vCenters
* schema:
* $ref : '#definitions/getVCenter'
*/
But it doesn't work anymore.
Can someone explain to me what I did wrong please?
There are syntax errors in your annotation.
The indentation of info properties should be as follows:
* info:
* type: object
* properties: # <-- "properties" must be on the same level as "type: object"
* name:
* type: string
* fullName:
* type: string
* vendor:
* type: string
In $refs, there must be a / after # - replace #definitions with #/definitions:
$ref : '#/definitions/getVCenter'
Also, if the response is supposed to be "an array of vCenters" and not a single vCenter, then the response schema should be:
* responses:
* 200:
* description: an array of vCenters
* schema:
* type: array
* items:
* $ref : '#/definitions/getVCenter'
Related
Reading the docs for the Nodejs Scheduler I did not found how to pass a name for the cron job I want to create.
Does anyone know if this package supports this?
I tried:
> const job = {
> httpTarget: {
> uri: `my_url`,
> httpMethod: 'GET',
> },
> schedule: '0 0,8-17 * * 0-6',
> timeZone: 'my_timezone',
> body: Buffer.from(JSON.stringify({JOB_ID: 'custom_name', name: 'custom_name'})),
> name: 'custom_name'
> };
From looking at the API spec for the Cloud Scheduler Job resource and at the Nodejs Quickstart I think you need to move the body attribute within the httpTarget as the Job resource does not have a body attribute, it should be associated with the http request.
Based on your code you would end wanting something like this:
const job = {
httpTarget: {
uri: 'my_url',
httpMethod: 'GET',
body: Buffer.from(JSON.stringify({JOB_ID: 'custom_name', name: 'custom_name'})),
},
schedule: '0 0,8-17 * * 0-6',
timeZone: 'my_timezone',
name: 'custom_name'
};
I figured out what was wrong in order to set the name correctly we need to set the name with the project_id and the location_id
httpTarget: {
uri: 'my_url',
httpMethod: 'GET',
},
schedule: '0 0,8-17 * * 0-6',
timeZone: 'my_timezone',
name: 'projects/YOUR_PROJECT_ID/locations/YOUR_LOCATION_ID/jobs/YOUR_CUSTOM_JOB_NAME'
};
I'm trying to build an API using OpenApi 3.0.2 for active documentation. I've managed to build a validated spec-file, and if I take out all of the OpenApi "stuff" from the API itself, all of the routes work properly, and I have no errors (unless I've managed to change something vital while attempting to solve this problem).
Additionally, my controllers do in fact have the generic controller names that this error suggests I use, so my initial thought was that it's having trouble finding my controllers. However, when I used Swagger 2.0 (before rebuilding it with 3.0), I didn't have this problem.
All of my controllers are structured similarly, and when I changed the order of the controllers (path '/users' first), I retrieved the same error ('users' swapped out for 'garments' in the error log).
That said, I feel that there must be something I'm doing wrong in my "bringing together" of the functioning API and the valid spec-file.
I've been looking for a solution to this problem for a while now, but I have found nothing. If this question has been asked and answered before, I apologize; please redirect me. This is my first StackOverflow question, so please be gentle. If I have missed any information important to the question, please let me know.
Error:
outfittr | 2019-01-21T13:51:37.150Z info: Valid specification file
outfittr | 2019-01-21T13:51:37.162Z info: Specification file dereferenced
outfittr | 2019-01-21T13:51:37.210Z info: No localhost or relative server found in spec file, added for testing in Swagger UI
outfittr | 2019-01-21T13:51:37.210Z debug: Register: GET - /garments
outfittr | 2019-01-21T13:51:37.211Z debug: GET - /garments
outfittr | 2019-01-21T13:51:37.212Z debug: Spec-file does not have router property -> try generic controller name: garmentsController
outfittr | 2019-01-21T13:51:37.212Z debug: Controller with generic controller name wasn't found either -> try Default one
outfittr | 2019-01-21T13:51:37.212Z error: There is no controller for GET - /garments
outfittr exited with code 0
openapi.yaml:
openapi: 3.0.2
info:
version: "1.0.0"
title: Outfittr API
paths:
/swagger:
x-swagger-pipe: swagger_raw
####################################### Garments ##############################################
/garments:
x-router-controller: garmentsController
get:
description: Returns an array of garments.
operationId: indexGarments
responses:
"200":
$ref: '#/components/schemas/Garment'
default:
$ref: "#/components/schemas/ErrorResponse"
post:
summary: Creates a new garment
operationId: newGarment
description: Adds garment to the system
responses:
'200':
$ref: '#/components/schemas/Garment'
default:
$ref: "#/components/schemas/ErrorResponse"
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Garment'
description: User that was created.
/garments/{_id}:
x-router-controller: garmentsController
get:
description: Returns one garment
operationId: viewGarment
parameters:
- in: path
name: _id
schema:
type: string
required: true
description: Numeric ID of the user to get
responses:
"200":
$ref: '#/components/schemas/Garment'
default:
$ref: "#/components/schemas/ErrorResponse"
######################################## Users ################################################
/users:
x-router-controller: usersController
get:
description: Returns an array of users.
operationId: indexUsers
responses:
"200":
$ref: '#/components/schemas/User'
default:
$ref: "#/components/schemas/ErrorResponse"
post:
summary: Creates a new user
operationId: newUser
description: Adds user to the system
responses:
'200':
$ref: '#/components/schemas/User'
default:
$ref: "#/components/schemas/ErrorResponse"
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/User'
description: User that was created.
/users/{_id}:
x-router-controller: usersController
get:
description: Returns one user
operationId: viewUser
parameters:
- in: path
name: _id
schema:
type: string
required: true
description: Numeric ID of the user to get
responses:
"200":
$ref: '#/components/schemas/User'
default:
$ref: "#/components/schemas/ErrorResponse"
####################################### Wardrobe ##############################################
/wardrobe:
x-router-controller: wardrobeController
get:
description: Returns an array of garments in the user's wardrobe.
operationId: indexWardrobeItems
responses:
"200":
$ref: '#/components/schemas/WardrobeItem'
default:
$ref: "#/components/schemas/ErrorResponse"
post:
summary: Creates a new wardrobe item
operationId: newWardrobeItem
description: Adds garment to the user's wardrobe in the system
responses:
'200':
$ref: '#/components/schemas/WardrobeItem'
default:
$ref: "#/components/schemas/ErrorResponse"
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/WardrobeItem'
description: User that was created.
/wardrobeItem/{_id}:
x-router-controller: wardrobeController
get:
description: Returns one wardrobe item
operationId: viewWardrobeItem
parameters:
- in: path
name: _id
schema:
type: string
required: true
description: Numeric ID of the user to get
responses:
"200":
$ref: '#/components/schemas/WardrobeItem'
default:
$ref: "#/components/schemas/ErrorResponse"
###################################### Components #############################################
servers:
- url: outfittr.net
- url: localhost:3000
components:
schemas:
User:
type: object
required:
- _id
- email
- username
- password
properties:
_id:
type: string
description: unique ID given by Mongo.
firstName:
type: string
description: First name of the user.
lastName:
type: string
description: Last name of the user.
email:
type: string
description: User's email address.
username:
type: string
description: User's username (for login)
password:
type: string
description: User's password (for login).
create_date:
type: string
description: date that the user joined.
__v:
type: integer
description: I have no idea.
Garment:
type: object
required:
- _id
- type
- imageLink
properties:
_id:
type: string
description: unique ID given by Mongo.
type:
type: string
description: type of garment
imageLink:
type: string
description: primary color of garment
__v:
type: integer
description: I have no idea.
WardrobeItem:
type: object
required:
- _id
- owner_id
- garment_id
properties:
_id:
type: string
description: unique ID given by Mongo.
unavailable:
type: boolean
description: Is the wardrobe item dirty, loaned out, or otherwise unavailable?
owner_id:
type: string
description: foreign key linking this wardrobe item to its owner.
garment_id:
type: string
description: foreign key linking this wadrobe item to the garment it is.
torn:
type: boolean
description: Is the wardrobe item torn?
reserveDate:
type: string
description: Optional - a date for which this wardrobe item must be worn
reserveTilDate:
type: string
description: Optional - a date after which the wardrobe item cannot be worn until the reserveDate.
__v:
type: integer
description: I have no idea.
ErrorResponse:
required:
- message
properties:
message:
type: string
Any help is vastly appreciated.
I'll answer this even if it's late because I have had this same problem and looked for a long time the solution. If someone else happens to have any reference on how to fix it.
When you set the controllers parameter in oas-tools:
OasTools.configure({
controllers: `${__dirname}/controllers`,
...
});
The default mapping that oas-tools makes to load the files is endpoint+Controller.js, in your case oas-tools would look for the export function indexGarments in .../controllers/garmentsController.js
Another example would be for the case /garments/{_ id}, the name of the controller file should be garments_idController.js
Using x-router-controller
If you want to use the parameter x-router-controller you have to put it inside the method:
...
/garments:
get:
x-router-controller: garmentsController
operationId: indexGarments
description: Returns an array of garments.
responses:
But be careful because it also modifies the value of the parameter (I do not know if this is a bug or because the name of the files has that requirement), for example, if you configure x-router-controller with the value garments.resource the file to be searched will be .../controllers/garmentsresource.js
In case you set the x-router-controller parameter and the file is not found, this error will be thrown:
info: Valid specification file
info: Specification file dereferenced
debug: Register: GET - /health
debug: GET - /health
debug: OAS-doc has x-router-controller property
error: undefined
For OAS3 you may try this:
paths:
/users:
get:
tags:
- User
x-openapi-router-controller: usersController
description: Returns an array of users.
operationId: indexUsers
responses:
"200":
$ref: '#/components/schemas/User'
i have developed a REST Service using Node Js and Express.
I have integrate Swagger to define api doc.
About login service this is the swagger definition that i used:
/**
* #swagger
* /api/v1.0/login:
* post:
* tags:
* - Login
* description: Login into system
* produces:
* - application/json
* parameters:
* - username: User
* description: The username of user
* in: body
* required: true
* - password: password
* description: Password of user
* in: body
* required: true
*
* responses:
* 200:
* description: Successfully login
*/
But my service gives me this response json:
{
"status": "ok",
"data": {
"auth": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjViYzg3ZDFkOWNhNmRkNDM5MDI1YjA1MCIsImlhdCI6MTU0MTA5MzMxMSwiZXhwIjoxNTQxMTc5NzExfQ.3BIl0dIQg-cEU9fyM7BocKLHEugH8cws5_E-dmRVHZM",
"faId": "HSo7q2o0",
"roles": "Owner"
}
}
How i can describe this response into swagger response description?
Thanks
You can learn a lot about how to format Swagger definitions using the actual specification online: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#responsesDefinitionsObject
A really simplified version of what you want would look something like this:
responses:
200:
description: Successfully login
schema:
properties:
status:
type: string
data:
type: object
properties:
auth:
type: boolean
token:
type: string
faId:
type: string
roles:
type: string
You would probably want to fill in more information as far as descriptions, which properties are required, etc. You can read about what those mean in the link above.
Also, models in Swagger are defined using the JSON Schema vocabulary, which you can read more about here.
I am creating a API definition, and a I wanto to split my canonical model to different documents and use the JSON pointer '$ref' to reuse them.
I need to find a way to add version in the YAML files.
For instance:
***pj.yaml***
pJType:
verison: 1.0
type: object
properties:
cnpj:
type: integer
***afastamento.yaml***
oswagger: '2.0'
info:
version: '1.0'
title: AfastamentoService
consumes:
- application/json
produces:
- application/json
paths:
'/{nis}':
get:
parameters:
- in: path
name: nis
type: integer
required: true
responses:
'200':
description: OK
schema:
$ref: '#/definitions/pesquisarResponse'
definitions:
pesquisarResponse:
type: object
properties:
listaAfastamento:
$ref: '#/definitions/listaAfastamentoType'
...
empregadorType:
type: object
properties:
personalidadeJuridica:
type: string
pessoaJuridica:
$ref: pJ.yaml#/pessoaJuridicaType
...
You can use the extension properties (prefixed with x-) to add arbitrary data to the spec:
# pj.yaml
pJType:
x-version: 1.0
type: object
properties:
cnpj:
type: integer
I am using node-swagger. It's working fine. I want to post json body without defining the schema in details. For example below I don't want to specify object properties. Is there any way to do this?
/pets:
post:
description: Add new
parameters:
- name: id
in: body
description: data to post
required: true
type: object
responses:
'200':
description: A list of pets
schema:
type : string
It's not rendering the textarea to post json data.
Try this YAML:
---
swagger: '2.0'
info:
version: 0.0.0
title: Simple API
paths:
/:
post:
produces:
- application/json
parameters:
- in: body
name: id
required: true
schema:
"$ref": "#/definitions/inBody"
responses:
201:
description: Added
definitions:
inBody:
type: object
If you are using swagger-ui-express and swagger-jsdoc and don't wan to use definitions you can directly define in YAML on endpoint like this.
/**
* #swagger
* /pets:
* post:
* description: Add new
* produces:
* - application/json
* parameters:
* - name: id
* description: data to post
* in: body
* required: true
* schema:
* type: object
* responses:
* 201:
* description: Pet created
*/
Based on Open API 3 spec, for an empty body request, you should have a requestBody section like the following:
requestBody:
required: true
description: blabla.
content:
application/json:
schema:
type: object
nullable: true