Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: edge case where references was incorrect handled for JSON Schema #1754

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 69 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"./LICENSE"
],
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.9",
"@apidevtools/json-schema-ref-parser": "^11.1.0",
"@apidevtools/swagger-parser": "^10.0.3",
"@asyncapi/parser": "3.0.0-next-major-spec.8",
"@smoya/multi-parser": "^4.0.0",
Expand Down
26 changes: 17 additions & 9 deletions src/processors/JsonSchemaInputProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractInputProcessor } from './AbstractInputProcessor';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { dereference } from '@apidevtools/json-schema-ref-parser';
import path from 'path';
import {
CommonModel,
Expand All @@ -15,6 +15,7 @@ import {
import { Logger } from '../utils';
import { Interpreter } from '../interpreter/Interpreter';
import { convertToMetaModel } from '../helpers';
import { ParserOptions } from '@apidevtools/json-schema-ref-parser/dist/lib/options';

/**
* Class for processing JSON Schema
Expand Down Expand Up @@ -84,7 +85,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
{},
'root',
true
) as any;
);
input = await this.dereferenceInputs(input);
const parsedSchema = Draft7Schema.toSchema(input);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(
Expand Down Expand Up @@ -114,7 +115,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
{},
'root',
true
) as any;
);
input = await this.dereferenceInputs(input);
const parsedSchema = Draft4Schema.toSchema(input);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(
Expand Down Expand Up @@ -144,7 +145,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
{},
'root',
true
) as any;
);
input = await this.dereferenceInputs(input);
const parsedSchema = Draft6Schema.toSchema(input);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(
Expand Down Expand Up @@ -197,20 +198,27 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
public async dereferenceInputs(input: any): Promise<any> {
input = this.handleRootReference(input);
Logger.debug('Dereferencing all $ref instances');
const refParser = new $RefParser();
// eslint-disable-next-line no-undef
const localPath = `${process.cwd()}${path.sep}`;
const deRefOption: $RefParser.Options = {
const deRefOption: ParserOptions = {
continueOnError: true,
dereference: { circular: true }
dereference: {
circular: true,
excludedPathMatcher: (path: string) => {
return (
path.includes('/examples/') &&
!path.includes('/properties/examples/')
);
}
}
};
Logger.debug(
`Trying to dereference all $ref instances from input, using option ${JSON.stringify(
deRefOption
)}.`
);
try {
await refParser.dereference(localPath, input, deRefOption);
await dereference(localPath, input, deRefOption);
} catch (e: any) {
const errorMessage = `Could not dereference $ref in input, is all the references correct? ${e.message}`;
Logger.error(errorMessage, e);
Expand Down Expand Up @@ -252,7 +260,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
return schema;
}

schema = Object.assign({}, schema);
schema = { ...schema };
if (isRoot) {
namesStack[String(name)] = 0;
(schema as any)[this.MODELGEN_INFFERED_NAME] = name;
Expand Down
34 changes: 33 additions & 1 deletion test/processors/JsonSchemaInputProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AnyModel, CommonModel } from '../../src/models';
jest.mock('../../src/utils/LoggingInterface');
jest.spyOn(JsonSchemaInputProcessor, 'convertSchemaToCommonModel');
let mockedReturnModels = [new CommonModel()];
const mockedMetaModel = new AnyModel('test', undefined);
const mockedMetaModel = new AnyModel('test', undefined, {});
jest.mock('../../src/helpers/CommonModelToMetaModel', () => {
return {
convertToMetaModel: jest.fn().mockImplementation(() => {
Expand Down Expand Up @@ -261,6 +261,38 @@ describe('JsonSchemaInputProcessor', () => {
'Cannot handle input, because it has a root `$ref`, please manually resolve the first reference.'
);
});
test('should not resolve example containing $ref keyword', async () => {
const processor = new JsonSchemaInputProcessor();
const schema = {
examples: [{ $ref: '#/none/existing/reference' }],
properties: {
$ref: {
type: 'string'
}
}
};
const dereferencedSchema = await processor.dereferenceInputs(schema);
expect(dereferencedSchema.examples[0]).toEqual({
$ref: '#/none/existing/reference'
});
});
test('should resolve example containing $ref keyword if part of properties', async () => {
const processor = new JsonSchemaInputProcessor();
const schema = {
definitions: {
reference: {
type: 'string'
}
},
properties: {
$ref: { $ref: '#/definitions/reference' }
}
};
const dereferencedSchema = await processor.dereferenceInputs(schema);
expect(dereferencedSchema.properties.$ref).toEqual({
type: 'string'
});
});
});

describe('convertSchemaToCommonModel()', () => {
Expand Down
Loading