Technologies:
Tolerim
a month ago
I keep receiving the error message ""Unsupported OpenAPI / Swagger version=undefined"" when trying to use express-openapi-validate with node.js.
I am encountering an error message "Unsupported OpenAPI / Swagger version=undefined" while attempting to validate my API request with express-openapi-validator. Despite adopting code from others found on Github, the error persists and I am at a loss. I have included my API specification file, app.js file and some unsuccessful alternatives I tried. Can someone please provide guidance?
Here is my API specification file:
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
And here is my app.js file:
```javascript
"use strict";
const express = require('express');
const { OpenApiValidator } = require("express-openapi-validate");
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
// ** Setting Up Port
const port = 3000;
// ** Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true, }));
// ** Adding Cors
app.use(cors());
// ** Open API Validator
new OpenApiValidator({
"apiSpecPath": "./apiSpec.yaml"
}).install(app)
appAnswers(2)
Vogi
a month ago
Verified Answer
I'm sorry to hear that you are encountering an error message while attempting to validate your API request with express-openapi-validator. I can help you with that.
The error message "Unsupported OpenAPI / Swagger version=undefined" is usually caused by a mismatch between the version of the OpenAPI specification used in your API specification file and the version supported by the validator.
In your case, it seems that you are using OpenAPI 3.0.0 in your API specification file. However, the version of the validator you are using may not support this version of the specification.
You can try updating the version of the validator to one that supports OpenAPI 3.0.0 or downgrading your API specification file to a version supported by the validator.
I hope this helps! Let me know if you have any other questions.
Tolerim
a month ago
It looks like you are trying to use the express-openapi-validator library to validate your API request using an OpenAPI specification, but you are getting an error that says "Unsupported OpenAPI / Swagger version=undefined".
This error usually occurs when the OpenAPI version is not specified or is not recognized by the validator. In your apiSpec.yaml file, you have specified the OpenAPI version as 3.0.0, which should be supported by the library.
One thing you can try is to explicitly set the OpenAPI version in your app.js file using the validateApiSpec method of the OpenApiValidator instance. Here's an example:
const express = require('express');
const { OpenApiValidator } = require('express-openapi-validate');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const validator = new OpenApiValidator({
apiSpecPath: './apiSpec.yaml',
});
// validate the api spec
validator.validateApiSpec().then(() => {
console.log('API spec is valid');
}).catch((err) => {
console.error('API spec is invalid', err);
});
// Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Adding Cors
app.use(cors());
// Open API Validator
validator.install(app);
app.get('/', (req, res) => {
res.send('App Up & Running!');
});
// ...
In this example, we are using the validateApiSpec method to explicitly validate the API spec before installing the validator middleware. This may help in case the validator is not recognizing the OpenAPI version.
If this doesn't work, you may want to try another OpenAPI validator library or check your OpenAPI definition for any syntax errors or unsupported features.