Skip to content

Commit

Permalink
fix(parse): solve remote and circular references
Browse files Browse the repository at this point in the history
Swagger-parser resolves circular references with Object references. PathsParser does not handle this case and will result in an infinite loop.

fix(ajv-openapi)!: remove draft-04 support

To solve an error importing AJV when OpenAPI Mocker is used as API from another application, that is using higher versions of AJV
Error: Cannot find module 'ajv/lib/refs/json-schema-draft-04.json'
Require stack:
[private-path]/[app-using-open-api-mocker-with-higher-ajv-version]/node_modules/ajv-openapi/index.js
[private-path]/[app-using-open-api-mocker-with-higher-ajv-version]/node_modules/open-api-mocker/lib/schema-validator/index.js
[private-path]/[app-using-open-api-mocker-with-higher-ajv-version]/node_modules/open-api-mocker/lib/paths/path.js
[private-path]/[app-using-open-api-mocker-with-higher-ajv-version]/node_modules/open-api-mocker/lib/paths/parser.js
[private-path]/[app-using-open-api-mocker-with-higher-ajv-version]/node_modules/open-api-mocker/lib/paths/index.js
[private-path]/[app-using-open-api-mocker-with-higher-ajv-version]/node_modules/open-api-mocker/lib/open-api-mocker.js
  • Loading branch information
javier-sierra-sngular authored and jorgecasar committed Dec 22, 2023
1 parent fad245e commit 0391803
Show file tree
Hide file tree
Showing 4 changed files with 17,305 additions and 6,690 deletions.
9 changes: 5 additions & 4 deletions lib/open-api-mocker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const jsonRefs = require('json-refs');
const SwaggerParser = require('@apidevtools/swagger-parser');

const { replaceCircularObject } = require('replace-circular-object');
const { Parser: OpenApiParser } = require('./openapi');
const { Parser: ServersParser } = require('./servers');
const { Parser: PathsParser } = require('./paths');
Expand Down Expand Up @@ -41,16 +42,16 @@ class OpenApiMocker {
this.schema = await this.schema;

try {
const parsedSchemas = await jsonRefs.resolveRefs(this.schema);

this.schema = parsedSchemas.resolved;
this.schema = await SwaggerParser.validate(this.schema);

const openApiParser = new OpenApiParser();
const openapi = openApiParser.parse(this.schema);

const serversParser = new ServersParser();
const servers = serversParser.parse(this.schema);

this.schema = replaceCircularObject(this.schema);

const pathsParser = new PathsParser();
const paths = pathsParser.parse(this.schema);

Expand Down
2 changes: 1 addition & 1 deletion lib/schema-validator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ajvOptions = {
};

const openApiOptions = {
useDraft04: true
useDraft04: false
};

const ajv = openApi(
Expand Down
Loading

0 comments on commit 0391803

Please sign in to comment.