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: root level reference are not handled for JSON Schema #829

Merged
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
43 changes: 39 additions & 4 deletions src/processors/JsonSchemaInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
const inputModel = new InputMetaModel();
inputModel.originalInput = input;
input = JsonSchemaInputProcessor.reflectSchemaNames(input, {}, 'root', true) as Record<string, any>;
await this.dereferenceInputs(input);
input = await this.dereferenceInputs(input);
const parsedSchema = Draft7Schema.toSchema(input);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
const metaModel = convertToMetaModel(newCommonModel);
Expand All @@ -82,7 +82,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
const inputModel = new InputMetaModel();
inputModel.originalInput = input;
input = JsonSchemaInputProcessor.reflectSchemaNames(input, {}, 'root', true) as Record<string, any>;
await this.dereferenceInputs(input);
input = await this.dereferenceInputs(input);
const parsedSchema = Draft4Schema.toSchema(input);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
const metaModel = convertToMetaModel(newCommonModel);
Expand All @@ -101,7 +101,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
const inputModel = new InputMetaModel();
inputModel.originalInput = input;
input = JsonSchemaInputProcessor.reflectSchemaNames(input, {}, 'root', true) as Record<string, any>;
await this.dereferenceInputs(input);
input = await this.dereferenceInputs(input);
const parsedSchema = Draft6Schema.toSchema(input);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
const metaModel = convertToMetaModel(newCommonModel);
Expand All @@ -110,7 +110,41 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
return inputModel;
}

private async dereferenceInputs(input: Record<string, any>) {
/**
* This is a hotfix and really only a partial solution as it does not cover all cases.
*
* But it's the best we can do until we find or build a better library to handle references.
*/
public handleRootReference(input: Record<string, any>): Record<string, any> {
//Because of https:/APIDevTools/json-schema-ref-parser/issues/201 the tool cannot handle root references.
//This really is a bad patch to fix an underlying problem, but until a full library is available, this is best we can do.
const hasRootRef = input.$ref !== undefined;
if (hasRootRef) {
Logger.warn('Found a root $ref, which is not fully supported in Modelina, trying to do what I can with it...');
//When we encounter it, manually try to resolve the reference in the definitions section
const hasDefinitionSection = input.definitions !== undefined;
if (hasDefinitionSection) {
const definitionLink = '#/definitions/';
const referenceLink = input.$ref.slice(0, definitionLink.length);
const referenceIsLocal = referenceLink === definitionLink;
if (referenceIsLocal) {
const definitionName = input.$ref.slice(definitionLink.length);
const definition = input.definitions[String(definitionName)];
const definitionExist = definition !== undefined;
if (definitionExist) {
delete input.$ref;
return {...definition, ...input};
}
}
}
//All other unhandled cases, means we cannot handle this input
throw new Error('Cannot handle input, because it has a root `$ref`, please manually resolve the first reference.');
}
return input;
}

public async dereferenceInputs(input: Record<string, any>): Promise<Record<string, any>> {
input = this.handleRootReference(input);
Logger.debug('Dereferencing all $ref instances');
const refParser = new $RefParser;
// eslint-disable-next-line no-undef
Expand All @@ -128,6 +162,7 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
throw new Error(errorMessage);
}
Logger.debug('Successfully dereferenced all $ref instances from input.', input);
return input;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions test/processors/JsonSchemaInputProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,38 @@ describe('JsonSchemaInputProcessor', () => {
});
});

describe('dereferenceInputs()', () => {
test('should handle root $ref', () => {
const processor = new JsonSchemaInputProcessor();
const schema = {
definitions: {
root: {
type: 'string'
}
},
$ref: '#/definitions/root'
};
const dereferencedSchema = processor.handleRootReference(schema);
expect(dereferencedSchema).toEqual({definitions: {root: {type: 'string'}}, type: 'string'});
});
test('should handle root $ref that cannot be processed', () => {
const processor = new JsonSchemaInputProcessor();
const schema = {
definitions: {
root: {
definitions: {
innerRoot: {
type: 'string'
}
}
}
},
$ref: '#/definitions/root/definitions/innerRoot'
};
expect(() => processor.handleRootReference(schema)).toThrowError('Cannot handle input, because it has a root `$ref`, please manually resolve the first reference.');
});
});

describe('convertSchemaToCommonModel()', () => {
test('Should ignore models which does not have $id', () => {
const model = new CommonModel();
Expand Down