I'm trying to create a REST-API just from my Mongoose Model. After trying out multiple libraries I don't understand if this is possible, because I was only able to output the Swagger Schema using mongoose-to-swagger which looks like this:
{
title: 'Time',
required: [ 'startingTime' ],
properties: {
project: { type: 'string', required: false },
description: { type: 'string', required: false },
startingTime: { type: 'string', format: 'date-time' },
endingTime: { type: 'string', required: false, format: 'date-time' },
_id: { type: 'string' }
}
}
But when creating a SwaggerSpec using swagger-jsdoc it looks like the following and works. But that way I still need to annotate the Model...
{
openapi: '3.0.0',
info: {
title: 'Your API Documentation',
version: '1.0.0',
description: 'API documentation generated with Swagger'
},
paths: {
'/api/v1/Time': { get: [Object], post: [Object] },
'/api/v1/Time/{id}': { get: [Object], put: [Object], patch: [Object], delete: [Object] }
},
components: { schemas: { Time: [Object] } },
tags: []
}
Mongoose Model / Schema:
const mongoose = require('mongoose');
const TimeSchema = new mongoose.Schema({
project: {
type: String,
required: false,
minLength: 1,
maxLength: 50,
trim: false,
},
description: {
type: String,
required: false,
maxLength: 100,
trim: false,
},
startingTime: {
type: Date,
required: true,
},
endingTime: {
type: Date,
required: false,
validate: {
validator: function (value) {
return this.startingTime <= value;
},
message: 'Ending time must be after the starting time',
},
},
});
const Time = mongoose.model('Time', TimeSchema);
module.exports = Time;
Tried to dynamically create a Swagger documentation using swagger-jsdoc which works but only with annotations.
When using mongoose-to-swagger it does not work because it looks like the output is not the array that swagger-ui-express expects.