how to write swagger document for nested objects - node.js

this document is ready for Nodejs create API, how to write swagger document for nested objects.
Here below is my full swagger document code. but it doesn't show user object. help me to fix the issue
#swagger
/api/v1/employees/add:
post:
tags:
- Employees
summary: "Create a new employee"
description:
produces:
- application/json
security:
- api_key: []
- api_token : []
parameters:
- name: body
in: body
description:
required: true
schema:
type: "object"
properties:
employee_code:
type: "string"
mobilenumber:
type: "string"
firstname:
type: "string"
lastname:
type: "string"
emailid:
type: "string"
is_login_enabled:
type: "boolean"
address_line1:
type: "string"
address_line2:
type: "string"
town:
type: "string"
city:
type: "string"
state:
type: "string"
pincode:
type: "string"
designation:
type: "string"
joining_date:
type: "string"
format: "date-time"
releiving_date:
type: "string"
format: "date-time"
is_active:
type: "boolean"
account_id:
type: "number"
user:
type: "object"
properties:
username:
type: "string"
role_id:
type: "number"
reference_object:
type: "string"
example: "employees"
password:
type: "string"
password_secret:
type: "string"
responses:
200:
description: Employee created successfully
204:
description: No data found
here is the snap of swagger ui

For nested object it is simple just define properties below with an indentation so your route definition would be
#swagger
/api/v1/employees/add:
put:
tags:
- Employees
summary: "Create a new employee"
description:
produces:
- application/json
security:
- api_key: []
- api_token : []
parameters:
- name: body
in: body
description:
required: true
schema:
type: "object"
properties:
employee_code:
type: "string"
mobilenumber:
type: "string"
firstname:
type: "string"
lastname:
type: "string"
emailid:
type: "string"
is_login_enabled:
type: "boolean"
address_line1:
type: "string"
address_line2:
type: "string"
town:
type: "string"
city:
type: "string"
state:
type: "string"
pincode:
type: "string"
designation:
type: "string"
joining_date:
type: "string"
format: "date-time"
releiving_date:
type: "string"
format: "date-time"
is_active:
type: "boolean"
account_id:
type: "number"
user:
type: "object"
properties:
username:
type: "string"
role_id:
type: integer
reference_object:
type: "string"
example: "employees"
password:
type: "string"
password_secret:
type: "string"
responses:
200:
description: Employee created successfully
204:
description: No data found
It looks like this in the Swagger UI.
Make sure to have the proper indentations when placing it.

Related

Create MongoDB new collection with json inside the schema

I try to create new collection with mongodb.
I wrote the MongoDB shcema, Nodejs+express code and everything work fine until I send parameters with Json inside them.
I was created this schema (and also more code under this schema) but its not work and I got error:
const partySchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
title: { type: String, required: true },
datetime: { type: Date, required: true },
location: {
scraping_location: { type: String, required: true },
display_location: { type: String, required: false },
city: { type: String, required: false },
adress: { type: String, required: false },
area: { type: String, required: false }
},
picture: { type: String, required: true },
url: { type: String, required: true },
affiliate_url: { type: String, required: true },
music_by: {type: [], required: false },
music_type: {type: [], required: false },
min_age_for_men: {type: Number, required: true },
min_age_for_women: {type: Number, required: true },
status_check: {type: Boolean, default: false },
tags: {
concept: { type: String, required: false },
free_text: { type: [], required: false }
},
producer_id: { type: String, required: false },
});
and the code that I wrote with nodejs+express is this:
const { title, datetime, location, picture, url, affiliate_url, music_by, music_type, min_age_for_men, min_age_for_women, tags, producer_id } = req.body;
const party = new Party({
_id: new mongoose.Types.ObjectId(),
title,
datetime,
location: { scarping_location: location.location_scarping },
picture,
url,
affiliate_url,
music_by,
music_type,
min_age_for_men,
min_age_for_women,
tags,
producer_id
});
party.save().then(() => {
res.status(200).json({
message: 'Create a new user'
})
}).catch(error => {
res.status(500).json({
error
})
});
when I send reqeust to the api with this body:
{
"title": "test",
"datetime": "2002-07-15T10:00:00+0300",
"location": {
"location_scarping": "new york"
},
"picture": "test.jpg",
"url": "https://google.com",
"affiliate_url": "https://google.com",
"min_age_for_men": 18,
"min_age_for_women": 16
}
I got this error:
{
"error": {
"errors": {
"location.scraping_location": {
"name": "ValidatorError",
"message": "Path `location.scraping_location` is required.",
"properties": {
"message": "Path `location.scraping_location` is required.",
"type": "required",
"path": "location.scraping_location"
},
"kind": "required",
"path": "location.scraping_location"
}
},
"_message": "Party validation failed",
"name": "ValidationError",
"message": "Party validation failed: location.scraping_location: Path `location.scraping_location` is required."
}
}
why its happend?
Looks like this is just a typo, from your schema:
scraping_location: { type: String, required: true }
You're trying to insert: location_scarping not location_scraping
"location_scarping": "new york"
Hopefully that fixes it.

Can i generate a fully structured yaml file from a csv file using Python?

swagger: "2.0"
info:
description: "This is a sample server Petstore server. You can find out more about Swagger at http://swagger.io or on irc.freenode.net, #swagger. For this sample, you can use the api key special-key to test the authorization filters."
version: "1.0.0"
title: "Swagger Petstore"
termsOfService: "http://swagger.io/terms/"
contact:
email: "apiteam#swagger.io"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "petstore.swagger.io"
basePath: "/v2"
tags:
name: "pet"
description: "Everything about your Pets"
externalDocs:
description: "Find out more"
url: "http://swagger.io"
name: "store"
description: "Access to Petstore orders"
name: "user"
description: "Operations about user"
externalDocs:
description: "Find out more about our store"
url: "http://swagger.io"
schemes:
"https"
"http"
paths:
/pet:
post:
tags:
- "pet"
summary: "Add a new pet to the store"
description: ""
operationId: "addPet"
consumes:
- "application/json"
- "application/xml"
produces:
- "application/xml"
- "application/json"
parameters:
- in: "body"
name: "body"
description: "Pet object that needs to be added to the store"
required: true
schema:
$ref: "#/definitions/Pet"
responses:
"405":
description: "Invalid input"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
put:
tags:
- "pet"
summary: "Update an existing pet"
description: ""
operationId: "updatePet"
consumes:
- "application/json"
- "application/xml"
produces:
- "application/xml"
- "application/json"
parameters:
- in: "body"
name: "body"
description: "Pet object that needs to be added to the store"
required: true
schema:
$ref: "#/definitions/Pet"
responses:
"400":
description: "Invalid ID supplied"
"404":
description: "Pet not found"
"405":
description: "Validation exception"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
securityDefinitions:
petstore_auth:
type: "oauth2"
authorizationUrl: "http://petstore.swagger.io/oauth/dialog"
flow: "implicit"
scopes:
write:pets: "modify pets in your account"
read:pets: "read your pets"
api_key:
type: "apiKey"
name: "api_key"
in: "header"
definitions:
Order:
type: "object"
properties:
id:
type: "integer"
format: "int64"
petId:
type: "integer"
format: "int64"
quantity:
type: "integer"
format: "int32"
shipDate:
type: "string"
format: "date-time"
status:
type: "string"
description: "Order Status"
enum:
- "placed"
- "approved"
- "delivered"
complete:
type: "boolean"
default: false
xml:
name: "Order"
Category:
type: "object"
properties:
id:
type: "integer"
format: "int64"
name:
type: "string"
xml:
name: "Category"
User:
type: "object"
properties:
id:
type: "integer"
format: "int64"
username:
type: "string"
firstName:
type: "string"
lastName:
type: "string"
email:
type: "string"
password:
type: "string"
phone:
type: "string"
userStatus:
type: "integer"
format: "int32"
description: "User Status"
xml:
name: "User"
Tag:
type: "object"
properties:
id:
type: "integer"
format: "int64"
name:
type: "string"
xml:
name: "Tag"
Pet:
type: "object"
required:
"name"
"photoUrls"
properties:
id:
type: "integer"
format: "int64"
category:
$ref: "#/definitions/Category"
name:
type: "string"
example: "doggie"
photoUrls:
type: "array"
xml:
name: "photoUrl"
wrapped: true
items:
type: "string"
tags:
type: "array"
xml:
name: "tag"
wrapped: true
items:
$ref: "#/definitions/Tag"
status:
type: "string"
description: "pet status in the store"
enum:
"available"
"pending"
"sold"
xml:
name: "Pet"
ApiResponse:
type: "object"
properties:
code:
type: "integer"
format: "int32"
type:
type: "string"
message:
type: "string"
externalDocs:
description: "Find out more about Swagger"
url: "http://swagger.io"

Issue when trying to store some data in a MongoDB data model

I want to store some data in the following MongoDB model:
import mongoose from 'mongoose';
const orderSchema = mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
orderItems: [
{
name: { type: String, required: true },
qty: { type: Number, required: true },
image: { type: String, required: true },
price: { type: Number, required: true },
product: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Product',
},
},
],
shippingAdress: {
address: { type: String, required: true },
city: { type: String, required: true },
postalCode: { type: String, required: true },
state: { type: String, required: false },
country: { type: String, required: true },
},
paymentMethod: {
type: String,
required: true,
},
paymentResult: {
id: { type: String },
status: { type: String },
update_time: { type: String },
email_address: { type: String },
},
taxPrice: {
type: Number,
required: true,
default: 0.0,
},
shippingPrice: {
type: Number,
required: true,
default: 0.0,
},
totalPrice: {
type: Number,
required: true,
default: 0.0,
},
isPaid: {
type: Boolean,
required: true,
default: false,
},
paidAt: {
type: Date,
},
isDelivered: {
type: Boolean,
required: true,
default: false,
},
deliveredAt: {
type: Date,
},
},
{ timestamps: true }
);
const Order = mongoose.model('Order', orderSchema);
export default Order;
But I am not able to post the following sample data to the database
{
"orderItems": [
{
"product": "6060f2eb6bea3f2280c08a4c",
"name": "Airpods Wireless Bluetooth Headphones",
"image": "/images/airpods.jpg",
"price": 89.99,
"qty": 3
}
],
"user": "6060f2eb6bea3f2280c08a4b",
"shippingAddress": {
"address": "1st Avenue Main St",
"city": "Boston",
"postalCode": "02011",
"country": "USA"
},
"paymentMethod": "Stripe",
"itemsPrice": 269.97,
"taxPrice": 40.50,
"shippingPrice": 20.00,
"totalPrice": 330.47
}
as I get the following error:
By the way I am trying to get that done through Postman
"Order validation failed: shippingAdress.country: Path shippingAdress.country is required., shippingAdress.postalCode: Path shippingAdress.postalCode is required., shippingAdress.city: Path shippingAdress.city is required., shippingAdress.address: Path shippingAdress.address is required."
You have a typo in your Model shippingAdress. It should be shippingAddress.

MongoDB custom methods to change one data when other two data is approved

Employee Leave Application. When Admin approves and Manager approves I want the approval status to be updated to Approved.
const leaveSchema = mongoose.Schema(
{
user: {
type: mongoose.Schema.ObjectId,
ref: "User",
required: [true, "It must belong to a user"],
},
adminApproval: {
type: String,
enum: ["Pending", "Approved", "Rejected"],
},
managerApproval: {
type: String,
enum: ["Pending", "Approved", "Rejected"],
},
approvalStatus: {
type: String,
enum: ["Pending", "Approved", "Rejected"],
default: "Pending",
},
active: {
type: Boolean,
default: true,
},
},
);
You could write a middleware to check this, after any time a document of that model is saved.
const leaveSchema = mongoose.Schema(
{
user: {
type: mongoose.Schema.ObjectId,
ref: "User",
required: [true, "It must belong to a user"],
},
adminApproval: {
type: String,
enum: ["Pending", "Approved", "Rejected"],
},
managerApproval: {
type: String,
enum: ["Pending", "Approved", "Rejected"],
},
approvalStatus: {
type: String,
enum: ["Pending", "Approved", "Rejected"],
default: "Pending",
},
active: {
type: Boolean,
default: true,
},
});
leaveSchema.post('save', function(doc) {
if (doc.adminApproval === 'Approved' && doc.managerApproval === 'Approved' && doc.approvalStatus !== 'Approved') {
doc.approvalStatus = 'Approved';
doc.save();
}
};

What is the correct way test the file upload through swagger-client?

I need to write a valid test of a file upload through swagger client but I can not figure out what I'm doing wrong or possible solutions
const requestBody = {
name: 'USUÁRIO TESTE',
cpf: 'PROCESSO DE TESTE/DEPLOY KAER-MORHEN',
email: 'POR FAVOR IGNORE ESTE E-MAIL',
phone: 'OBRIGADO',
id: 'cj2c3rtea00010epfu0sfdhf7',
back: tmp.fileSync({ prefix: 'document-', postfix: '.jpg' }).name
}
return client.apis.documents.sendDocumentsEmail(undefined, {
requestBody
})
.should.be.fulfilled()
.then(({ status, body }) => {
status.should.be.eql(200)
body.should.be.Object()
})
I'm also using Multer as middleware, and when I send the tmp file or creating a file with fs it doesn't get recognized and the attribute ends up beeing treated as a string.
What would be the simplest way to send a valid multipart/form-data with a valid file using swagger-client?
/documents/upload:
x-middlewares:
- "multer"
post:
tags:
- "documents"
- "kaermorhen"
summary: "Upload documents"
x-swagger-router-controller: "document.controller"
operationId: "sendDocumentsEmail"
requestBody:
description: "Documents upload"
required: true
x-name: "body"
content:
multipart/form-data:
schema:
type: "object"
required:
- "id"
properties:
id:
type: "string"
name:
type: "string"
cpf:
type: "string"
email:
type: "string"
phone:
type: "string"
front:
type: string
format: binary
back:
type: string
format: binary
residence:
type: string
format: binary

Resources