express-openapi-validator not validating my request, am i missing something ? using nodejs: v18.6 and express-openapi-validator: ^5.0.4
girdharsoni opened this issue · comments
girdharsoni commented
app.js
"use strict";
const express = require('express');
const swaggerUI = require('swagger-ui-express');
const OpenApiValidator = require('express-openapi-validator');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
// ** Setting Up Port
const port = 3000;
// ** Swagger 3.0.0
const openApiValidation = require("./apiValidation/index");
// ** Injecting swaggerJSDocs In swagger
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(openApiValidation, null, null, null));
app.use(OpenApiValidator.middleware({
apiSpec: './openapi.json',
validateRequests: true,
}));
// ** Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true, }));
// ** Adding Cors
app.use(cors());
app.get("/", (req, res) => {
res.send(`App Up & Running!`);
});
app.get('/api/contact', (req, res) => {
res.status(200).json(contactList);
});
app.post("/api/contact", (req, res) => {
return res.json({ "message": "SuccessFully Added" });
});
app.get("/api/contact/:id", (req, res) => {
});
app.put("/api/contact/:id", (req, res) => {
});
app.delete("/api/contact/:id", (req, res) => {
});
app.use((err, req, res, next) => {
// ** format error
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
openapi.json
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "API validation using OpenAPI"
},
"paths": {
"/contact": {
"get": {
"tags": [
"Contact"
],
"summary": "Get all the contacts",
"description": "Get all the contacts",
"responses": {
"200": {
"description": "Contacts fetched successfully",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Contact"
}
}
}
}
},
"400": {
"description": "Error in fetching contacts",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
},
"post": {
"tags": [
"Contact"
],
"summary": "Save a new contact",
"description": "Save a new contact",
"requestBody": {
"$ref": "#/components/requestBodies/Contact"
},
"responses": {
"200": {
"description": "Contact saved successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
},
"400": {
"description": "Error in saving contact",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
}
},
"/contact/{id}": {
"get": {
"tags": [
"Contact"
],
"summary": "Get a contact",
"description": "Get a contact with the id specified in parameter",
"parameters": [
{
"in": "path",
"name": "id",
"description": "Contact id that needs to be fetched",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Contact fetched successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Contact"
}
}
}
},
"400": {
"description": "Error in fetching contact",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
},
"put": {
"tags": [
"Contact"
],
"summary": "Update a contact",
"description": "Update a contact",
"parameters": [
{
"in": "path",
"name": "id",
"description": "Contact id that needs to be updated",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"$ref": "#/components/requestBodies/Contact"
},
"responses": {
"200": {
"description": "Contact updated successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
},
"400": {
"description": "Error in updating contact",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
},
"delete": {
"tags": [
"Contact"
],
"summary": "Delete a contact",
"description": "Delete a contact with the id specified in parameter",
"parameters": [
{
"in": "path",
"name": "id",
"description": "Contact id that needs to be deleted",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Contact deleted successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
},
"400": {
"description": "Error in deleting contact",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
}
},
"/product": {
"get": {
"tags": [
"Product"
],
"summary": "Get all the products",
"description": "Get all the products",
"responses": {
"200": {
"description": "Products fetched successfully",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
}
}
}
}
},
"400": {
"description": "Error in fetching products",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
},
"post": {
"tags": [
"Product"
],
"summary": "Save a new product",
"description": "Save a new product",
"requestBody": {
"$ref": "#/components/requestBodies/Product"
},
"responses": {
"200": {
"description": "Product saved successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
},
"400": {
"description": "Error in saving product",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
}
},
"/product/{id}": {
"get": {
"tags": [
"Product"
],
"summary": "Get a product",
"description": "Get a product with the id specified in parameter",
"parameters": [
{
"in": "path",
"name": "id",
"description": "Product id that needs to be fetched",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Product fetched successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
}
}
},
"400": {
"description": "Error in fetching product",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
},
"put": {
"tags": [
"Product"
],
"summary": "Update a product",
"description": "Update a product",
"parameters": [
{
"in": "path",
"name": "id",
"description": "Product id that needs to be updated",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"$ref": "#/components/requestBodies/Product"
},
"responses": {
"200": {
"description": "Product updated successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
},
"400": {
"description": "Error in updating product",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
},
"delete": {
"tags": [
"Product"
],
"summary": "Delete a product",
"description": "Delete a product with the id specified in parameter",
"parameters": [
{
"in": "path",
"name": "id",
"description": "Product id that needs to be deleted",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Product deleted successfully",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
},
"400": {
"description": "Error in deleting product",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CommonResponse"
}
}
}
}
}
}
}
},
"servers": [
{
"url": "http://localhost:3000/api"
},
{
"url": "https://localhost:3000/api"
}
],
"components": {
"requestBodies": {
"Contact": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Contact"
}
}
},
"description": "Contact object",
"required": true
},
"Product": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
}
},
"description": "Product object",
"required": true
}
},
"schemas": {
"CommonResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"Contact": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"mobile": {
"type": "string"
},
"email": {
"type": "string"
}
}
},
"Product": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"price": {
"type": "number"
}
}
}
}
}
}
Carmine DiMascio commented
this question is best served for stackoverflow. if you believe this is a bug, please reopen with steps to reproduce