-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Logger } from '../utils'; | ||
import { CommonModel } from '../models/CommonModel'; | ||
import { Schema } from '../models/Schema'; | ||
import { Interpreter } from './Interpreter'; | ||
import { isModelObject } from './Utils'; | ||
|
||
/** | ||
* Interpreter function for JSON Schema draft 7 allOf keyword. | ||
* | ||
* It either merges allOf schemas into existing model or if allowed, create inheritance. | ||
* | ||
* @param schema | ||
* @param model | ||
* @param interpreter | ||
*/ | ||
export default function interpretAllOf(schema: Schema | boolean, model: CommonModel, interpreter : Interpreter) { | ||
if (typeof schema === 'boolean' || schema.allOf === undefined) return; | ||
|
||
for (const allOfSchema of schema.allOf) { | ||
const interpretedModels = interpreter.interpret(allOfSchema); | ||
if (interpretedModels.length === 0) continue; | ||
const interpretedModel = interpretedModels[0]; | ||
if (isModelObject(interpretedModel) && interpreter.options.allowInheritance === true) { | ||
Logger.info(`Processing allOf, inheritance is allowed, ${model.$id} inherits from ${interpretedModel.$id}`, model, interpretedModel); | ||
model.addExtendedModel(interpretedModel); | ||
} else { | ||
Logger.info('Processing allOf, inheritance is not allowed. AllOf model is merged together with already interpreted model', model, interpretedModel); | ||
interpreter.combineSchemas(allOfSchema, model, schema); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* eslint-disable no-undef */ | ||
import { CommonModel } from '../../src/models/CommonModel'; | ||
import { Interpreter } from '../../src/interpreter/Interpreter'; | ||
import { isModelObject } from '../../src/interpreter/Utils'; | ||
import interpretAllOf from '../../src/interpreter/InterpretAllOf'; | ||
import { SimplificationOptions } from '../../src/models/SimplificationOptions'; | ||
|
||
let interpreterOptions: SimplificationOptions = {}; | ||
let interpretedReturnModels = [new CommonModel()]; | ||
jest.mock('../../src/interpreter/Interpreter', () => { | ||
return { | ||
Interpreter: jest.fn().mockImplementation(() => { | ||
return { | ||
interpret: jest.fn().mockImplementation(() => {return interpretedReturnModels}), | ||
combineSchemas: jest.fn(), | ||
options: interpreterOptions | ||
}; | ||
}) | ||
}; | ||
}); | ||
jest.mock('../../src/models/CommonModel'); | ||
let mockedIsModelObjectReturn = false; | ||
jest.mock('../../src/interpreter/Utils', () => { | ||
return { | ||
isModelObject: jest.fn().mockImplementation(() => { | ||
return mockedIsModelObjectReturn; | ||
}) | ||
} | ||
}); | ||
CommonModel.mergeCommonModels = jest.fn(); | ||
/** | ||
* Some of these test are purely theoretical and have little if any merit | ||
* on a JSON Schema which actually makes sense but are used to test the principles. | ||
*/ | ||
describe('Interpretation of allOf', () => { | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
interpreterOptions = {allowInheritance: true}; | ||
interpretedReturnModels = [new CommonModel()]; | ||
mockedIsModelObjectReturn = false; | ||
}); | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('should not do anything if schema does not contain allOf', function() { | ||
const model = new CommonModel(); | ||
const interpreter = new Interpreter(); | ||
interpretAllOf({}, model, interpreter); | ||
expect(interpreter.combineSchemas).not.toHaveBeenCalled(); | ||
expect(JSON.stringify(model)).toEqual(JSON.stringify(new CommonModel())); | ||
}); | ||
|
||
test('should combine schemas if inheritance is disabled', function() { | ||
const model = new CommonModel(); | ||
const schema = { allOf: [{}] }; | ||
interpreterOptions.allowInheritance = false; | ||
const interpreter = new Interpreter(interpreterOptions); | ||
interpretAllOf(schema, model, interpreter); | ||
expect(interpreter.combineSchemas).toHaveBeenNthCalledWith(1, schema.allOf[0], model, schema); | ||
expect(JSON.stringify(model)).toEqual(JSON.stringify(new CommonModel())); | ||
}); | ||
|
||
test('should ignore model if interpreter cannot interpret schema', function() { | ||
const model = new CommonModel(); | ||
const schema = { allOf: [{}] }; | ||
interpretedReturnModels.pop(); | ||
const interpreter = new Interpreter(interpreterOptions); | ||
interpretAllOf(schema, model, interpreter); | ||
expect(interpreter.combineSchemas).not.toHaveBeenCalled(); | ||
expect(JSON.stringify(model)).toEqual(JSON.stringify(new CommonModel())); | ||
}); | ||
test('should handle empty allOf array', function() { | ||
const model = new CommonModel(); | ||
const schema = { allOf: [] }; | ||
const interpreter = new Interpreter(interpreterOptions); | ||
interpretAllOf(schema, model, interpreter); | ||
expect(interpreter.combineSchemas).not.toHaveBeenCalled(); | ||
expect(JSON.stringify(model)).toEqual(JSON.stringify(new CommonModel())); | ||
}); | ||
test('should extend model', function() { | ||
const model = new CommonModel(); | ||
const schema = { allOf: [{type: "object", $id: "test"}] }; | ||
interpretedReturnModels[0].$id = "test"; | ||
mockedIsModelObjectReturn = true; | ||
const interpreter = new Interpreter(interpreterOptions); | ||
interpretAllOf(schema, model, interpreter); | ||
expect(interpreter.combineSchemas).not.toHaveBeenCalled(); | ||
expect(isModelObject).toHaveBeenCalled(); | ||
expect(model.addExtendedModel).toHaveBeenCalledWith(interpretedReturnModels[0]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters