Nest JS swagger API response body from third party package - nestjs

I am writing a REST API swagger in Nest JS.
My code snippet looks like this:
#Get()
#ApiOperation({
summary: `Something`,
description: `Something`,
})
#ApiOkResponse({
description: 'The resources were returned successfully',
type: VpcV1,
isArray: true
})
#ApiForbiddenResponse({ description: 'Unauthorized Request' })
list() {
return this.service.list();
}
As you can see the the this.service.list() is returning ab object of type VpcV1.
I have added it as type in the #ApiOkResponse().
But as the VpcV1 is from third party package which I do not have any control, it's not a DTO.
In such a case how can I see the API response object such that when user will see the swagger, they can be able to see the Response Schema properly?

Related

Is there a way to fix this Mailchimp API error in node?

Is there a way to fix this MailChimp API error in Node.js?
{
type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
title: 'Invalid Resource',
status: 400,
detail: "The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
instance: '95a2824e-4f30-4ce4-8c5e-322859d933e4',
errors: [
{
field: 'members.item:0.status',
message: 'Data presented is not one of the accepted values: subscribed, unsubscribed, cleaned, pending.'
}
]
}
What is the API call the Node app is trying to do? Looks like its trying to add a member to a list, so assuming you are making this request POST /lists/{list_id}/members
The required fields in the body needs to be:
{
"email_address": "",
"status": ""
}
Based on the error message, you are not passing in a status in the POST request. Possible values are the following: "subscribed", "unsubscribed", "cleaned", "pending", or "transactional".

Express/Swagger pre validation function for end points

I am working on express js with swagger.
I have created some end points with defining respective controller and function call.
tabs/users:
# binds a127 app logic to a route
x-swagger-router-controller: tabs
get:
description: Returns list of all users
# used as the method name of the controller
operationId: getUsers
parameters:
- name: name
in: query
description: name
required: false
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/TabResponse"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
Like oprtionId getUsers, I have multiple functions.
Inside controller tabs.js I have functions like
function getUser(){
//business logic
}
Now I have one use case for every call to my end points I need to validate the token which user sent with each request is valid or not.
For this, I need to add validation function call to each controller function.
But I am looking for the central solution so that every call should go through validation function.
Please suggest your thoughts on it.

Ember.js data records shows content: null in relationships inspector

I have the following code in my 'user.js' model in ember-data:
export default DS.Model.extend({
organization: DS.belongsTo('organization'),
//other stuff
});
The CRUD for the website is working as expected, and in MongoDB I can see the following for the organization field of User:
"organization" : ObjectId("571974742ce868d575b79d6a"),
BUT, and I'm not sure if this is an error in my code or me not understanding how Ember-data works, I cannot access that ID from a model hook like so:
model(){
return this.store.findRecord("user", this.get("session.currentUser.id"))
.then(user => this.store.findRecord("location", {organization: user.organization}));
}
And if I go to the Ember inspector to observe the belongsTo attribute of the User object, I see:
organization: <(subclass of Ember.ObjectProxy):ember956>
But clicking through I see content: null
What am I doing wrong? Could it be a server-side error?
Edit including JSON response from server for the above findRecord("user") call:
{
"links":{
"self":"/users/5719749a2ce868d575b79d6b"
},
"included":[
{
"type":"organizations",
"id":"571974742ce868d575b79d6a",
"links":{
"self":"/organizations/571974742ce868d575b79d6a"
},
"attributes":{
"creation-date":"2016-04-22T00:46:44.779Z"
}
}
],
"jsonapi":{
"version":"1.0"
},
"data":{
"type":"users",
"id":"5719749a2ce868d575b79d6b",
"links":{
"self":"/users/5719749a2ce868d575b79d6b"
},
"attributes":{
"email":"danthwa#gmail.com",
"first-name":"Daniel",
"last-name":"Thompson",
"registration-date":"2016-04-22T00:47:22.534Z"
},
"relationships":{
"organization":{
"type":"organizations",
"id":"571974742ce868d575b79d6a"
}
}
}
}
Confirmed. As stated by Kingpin2k,
the relationships isn't being built up correctly, I think the type and id inside of organization need to be within a data object.
This applies to Ember sites expecting a JSON API spec payload, meaning they have been configured to use JSONAPISerializer for incoming payloads.

Swagger Updates Breaks API Usage

My API seems to no longer parse properly using swagger-parser and swagger-tools latest versions after we did a full npm update on all our deps last night. However, I cannot track down what is causing these failures to happen since the API follows proper formatting for the 2.0 spec according to the docs.
Some information on the current setup:
swagger-parser: v3.3.0
swagger-tools: v0.9.7
node: v4.2.1
npm: v2.14.7
We are making use of swagger-tools connect middleware to create a stand-alone API app. Our setup for the api looks like this:
var app = require('connect')();
var cors = require('cors');
var swaggerTools = require('swagger-tools');
var swaggerParser = require('swagger-parser');
app.use(cors());
app.initialize = function (done) {
swaggerParser.parse('./api/swagger.yaml', function (err, api) {
if (err) throw err;
swaggerTools.initializeMiddleware(api, function (middleware) {
app.use(middleware.swaggerMetadata());
app.use(middleware.swaggerValidator());
var options = {
controllers: './controllers',
useStubs: process.env.NODE_ENV === 'development' ? true : false
};
app.use(middleware.swaggerRouter(options));
app.use(middleware.swaggerUi());
typeof done === 'function' && done();
});
});
return app;
};
Prior to the updates on the deps everything worked fine like this. However, now when being initialized our API is throwing a bunch of errors when swaggerTools.initializeMiddleware is being called.
A few chunks of our API are as follows:
./api/swagger.yaml
swagger: '2.0'
info:
version: 0.0.1
title: Insert API Title Here
schemes:
- http
basePath: /api/v1
consumes:
- application/json
produces:
- application/json
paths:
/users:
$ref: './api/paths/users.yaml'
/users/{userId}:
$ref: './api/paths/users-userId.yaml'
definitions:
User:
$ref: './api/models/user.yaml'
parameters:
userId:
in: path
name: userId
description: The user id of the user object.
required: true
type: integer
offset:
name: offset
in: query
description: The record to start the return set at.
required: false
type: integer
default: 0
minimum: 0
limit:
name: limit
in: query
description: The quota to limit the return set to.
required: false
type: integer
default: 10
orderBy:
name: orderBy
in: query
description: The field to order by.
required: false
type: string
sort:
name: sort
in: query
description: The sort order of the return set.
required: false
type: string
enum: [desc, asc]
default: asc
./api/paths/users.yaml
x-swagger-router-controller: Users
get:
tags:
- users
summary: Gets a list of all users.
description: ''
operationId: getUsers
parameters:
- $ref: '#/parameters/offset'
- $ref: '#/parameters/limit'
- $ref: '#/parameters/orderBy'
- $ref: '#/parameters/sort'
responses:
200:
description: OK
schema:
$ref: '#/definitions/UserCollection'
401:
description: Not Authorized
The errors we are seeing are things like this now:
#/paths/~1users/get/parameters/1/name: Parameter already defined: undefined
#/paths/~1users/get/parameters/2/name: Parameter already defined: undefined
#/paths/~1users/get/parameters/3/name: Parameter already defined: undefined
#/paths/~1users~1{userId}/get: API requires path parameter but it is not defined: userId
#/paths/~1users~1{userId}/put/parameters/1/name: Parameter already defined: undefined
#/paths/~1users~1{userId}/put: API requires path parameter but it is not defined: userId
#/paths/~1users~1{userId}/delete: API requires path parameter but it is not defined: userId
#/paths/~1users~1{userId}~1profile/get: API requires path parameter but it is not defined: userId
#/paths/~1users~1{userId}~1profile/post/parameters/1/name: Parameter already defined: undefined
#/paths/~1users~1{userId}~1profile/post: API requires path parameter but it is not defined: userId
I'm not sure where to go from here since I have tried everything to keep the API layout the same (broken into multiple files) vs. having to resort to putting it all into a single file. Our API is rather large and it is much easier to maintain broken into parts like this which used to work fine without issue.
Is there something I am missing, a step that needs to be done differently with the updates to swagger-parser and swagger-tools? Any help is appreciated.
It seems the jump between v2 to v3 of Swagger-Parser has changed the functionality of .parse() to no longer resolve references. Because of this it was causing portions of the API to not validate properly. Switching to .validate() instead of .parse() has fixed this issue. A handful of adjustments had to be made to the .yaml files to make it work with the new 2.0 standards but all is working again.

Missing required field: member

{ errors:
[ { domain: 'global',
reason: 'required',
message: 'Missing required field: member' } ],
code: 400,
message: 'Missing required field: member' }
I get this error when I run the following request:
var request = client.admin.members.insert({
groupKey: "some_group#example.com"
, email: "me#example.com"
});
I was authenticated successfully (I received the access token and so on) but when I execute the request above it callbacks that error.
What member field am I supposed to add?
It works fine in API Explorer using groupKey and email fields.
The documentation at https://developers.google.com/admin-sdk/directory/v1/reference/members/insert for admin.members.insert indicates that it requires a groupKey parameter, but that the body (which the node.js library handles as a separate object) should contain a members object containing the role property. See the API Explorer a the bottom of that page as well.
email is part of the form data. The form data must be passed as object in the second argument:
// create the group insert request
var request = client.admin.members.insert({
groupKey: "some_group#example.com"
}, {
email: "me#example.com"
});

Resources