Surnet / swagger-jsdoc

Generates swagger/openapi specification based on jsDoc comments and YAML files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Options to pass components Schema to swaggerJsdoc

InfoDevkota opened this issue · comments

Is your feature request related to a problem? Please describe.
Problem: Need to write JOI validation Object and annotation again for doc.

we are writing annotations for my APIs, But for Components we have Joi validation object. With joi-to-swagger we can transform Joi schema objects into Swagger OAS 3.0 schema definitions.

Describe the solution you'd like
What if we can pass the schemas to swaggerJsdoc. We can pass the schema from joi-to-swagger.

const options = {
	definition: {},
	apis: [], // include routes,
	schemas: [userCreateSchema] // list of schema from joi-to-swagger
};

Will this be possible. Or is there any solution already?
If possible I would love to contribute as well.

hi @InfoDevkota, thanks for the issue.

Can you share a real use case from this?

Can be a tiny repo or prints, whatever you can do to help us.

Hello @daniloab , thanks for the response.

Here is the repo as requested, I tried to explain what I want, and how it would be useful.

Here is the repo: https://github.com/InfoDevkota/swagger-jsdoc-feature-request

Thank you!

need this, too.

same as @InfoDevkota

Hello @EdisonSL once after your comment, I thought of implementing it, then I started looking through code. The feature turned out to have already existed.

// JOI validation object for UserLoginDTO
const UserLoginDTO = Joi.object({
	phoneNumber: Joi.string().length(10).regex(/^\d+$/).required().messages({
		'string.pattern.base': `"phoneNumber" should contains numbers only`
	}),
	password: Joi.string().min(7).max(50).required()
});

// now let's use joi-to-swagger
const j2sUserLoginDTO = j2s(UserLoginDTO);

// Now with the help of joi-to-swagger we have this Schema,
const UserLoginDTOSchema = j2sUserLoginDTO.swagger;

Now we can pass that schema.

const options3 = {
	definition: {
		openapi: '3.0.0',
		info: {
			title: 'Sample API',
			description: 'Sample Project for feature request.',
			version: '1.0.0'
		},
		components: {
			schemas: {
				UserCreateModel2,
				UserLoginDTO: UserLoginDTOSchema
			}
		}
	},
	apis: ['*.js']
};

and we can use that as well.

/**
 * @openapi
 * /login:
 *  post:
 *      tags:
 *      - "User"
 *      description: login a user
 *      requestBody:
 *          content:
 *              'application/json':
 *                  schema:
 *                      $ref: "#/components/schemas/UserLoginDTO"
 *      responses:
 *          200:
 *              description: returns a token
 */
app.post('/login', (req, res, next) => {
	// stuffs
})

I have updated the feature request repo as well. https://github.com/InfoDevkota/swagger-jsdoc-feature-request/blob/main/index.js