openapi-parser: Unsupported OpenAPI version: 3.0.4 #923
Open
Description
Ideally I'd raise this issue against openapi-parser repo, but it doesn't have issues enabled, so I report it here.
Steps to reproduce
Create the following files:
package.json
{
"name": "repro",
"version": "1.0.0",
"type": "module",
"dependencies": {
"oas": "^25.2.1",
"oas-normalize": "^12.0.0"
}
}
// index.js
import OASNormalize from 'oas-normalize';
const yaml = `
openapi: 3.0.4
info:
version: 1.0.0
title: Petstore simple w/o tags
servers:
- url: https://httpbin.org
paths:
'/pet/{id}':
parameters:
- name: id
in: path
required: true
schema:
type: integer
get:
summary: Find a pet
description: This operation will find a pet in the database.
responses:
'400':
description: Invalid status value
security: []
`;
const oas = new OASNormalize(yaml);
const definition = await oas.validate();
Install and run:
npm install
node index.js
Expected result
The script would run without any issues.
Actual result
The script crashes with error:
SyntaxError: Unsupported OpenAPI version: 3.0.4. Swagger Parser only supports versions 3.0.0, 3.0.1, 3.0.2, 3.0.3, 3.1.0
at OpenAPIParser.parse (/path/to/repro/node_modules/@readme/openapi-parser/lib/index.js:95:19)
at async OpenAPIParser.resolve (/path/to/repro/node_modules/@readme/json-schema-ref-parser/lib/index.js:189:5)
at async OpenAPIParser.dereference (/path/to/repro/node_modules/@readme/json-schema-ref-parser/lib/index.js:271:5)
at async OpenAPIParser.validate (/path/to/repro/node_modules/@readme/openapi-parser/lib/index.js:152:5)
at async file:///path/to/repro/repro.js:28:20 {
toJSON: [Function: toJSON],
[Symbol(nodejs.util.inspect.custom)]: [Function: inspect]
}
Node.js v23.5.0
Notes
Support for the latest minor versions of OAS was quite recently fixed in the upstream openapi-parser: APIDevTools/swagger-parser#264
Since there are no functional changes in the latest versions, it's really only about adding support for the newer versions.
As a very ugly workaround, I can (sometimes) modify the OAS before parsing, e.g. for the example above:
// yaml = ...
const fixedYaml = yaml.replace(/^openapi:\s*3\.0\.4/m, 'openapi: 3.0.3');
const oas = new OASNormalize(fixedYaml);
const definition = await oas.validate();