Skip to content

Commit

Permalink
fix: nested object models are not split (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Jun 10, 2021
1 parent 2240de6 commit 9d0d42e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/interpreter/PostInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { isModelObject } from './Utils';
* @param model
*/
export function postInterpretModel(model: CommonModel): CommonModel[] {
const iteratedModels: CommonModel[] = [];
ensureModelsAreSplit(model, iteratedModels);
return iteratedModels;
const splitModels: CommonModel[] = [model];
ensureModelsAreSplit(model, splitModels);
return splitModels;
}

/**
Expand All @@ -19,15 +19,19 @@ export function postInterpretModel(model: CommonModel): CommonModel[] {
* @param model check if it should be split up
* @param iteratedModels which have already been split up
*/
function trySplitModels(model: CommonModel, iteratedModels: CommonModel[]): CommonModel {
function trySplitModels(model: CommonModel, splitModels: CommonModel[], iteratedModels: CommonModel[]): CommonModel {
let modelToReturn: CommonModel = model;
if (isModelObject(model)) {
Logger.info(`Splitting model ${model.$id || 'any'} since it should be on its own`);
const switchRootModel = new CommonModel();
switchRootModel.$ref = model.$id;
ensureModelsAreSplit(model, iteratedModels);
return switchRootModel;
modelToReturn = switchRootModel;
if (!splitModels.includes(model)) {
splitModels.push(model);
}
}
return model;
ensureModelsAreSplit(model, splitModels, iteratedModels);
return modelToReturn;
}

/**
Expand All @@ -36,34 +40,33 @@ function trySplitModels(model: CommonModel, iteratedModels: CommonModel[]): Comm
* @param model to ensure are split
* @param iteratedModels which are already split
*/
function ensureModelsAreSplit(model: CommonModel, iteratedModels: CommonModel[]): void {
function ensureModelsAreSplit(model: CommonModel, splitModels: CommonModel[], iteratedModels: CommonModel[] = []): void {
// eslint-disable-next-line sonarjs/no-collapsible-if
if (iteratedModels.includes(model)) { return; }

iteratedModels.push(model);
if (model.properties) {
const existingProperties = model.properties;
for (const [prop, propSchema] of Object.entries(existingProperties)) {
model.properties[String(prop)] = trySplitModels(propSchema, iteratedModels);
model.properties[String(prop)] = trySplitModels(propSchema, splitModels, iteratedModels);
}
}
if (model.patternProperties) {
const existingPatternProperties = model.patternProperties;
for (const [pattern, patternModel] of Object.entries(existingPatternProperties)) {
model.patternProperties[String(pattern)] = trySplitModels(patternModel, iteratedModels);
model.patternProperties[String(pattern)] = trySplitModels(patternModel, splitModels, iteratedModels);
}
}
if (model.additionalProperties) {
model.additionalProperties = trySplitModels(model.additionalProperties, iteratedModels);
model.additionalProperties = trySplitModels(model.additionalProperties, splitModels, iteratedModels);
}
if (model.items) {
let existingItems = model.items;
if (Array.isArray(existingItems)) {
for (const [itemIndex, itemModel] of existingItems.entries()) {
existingItems[Number(itemIndex)] = trySplitModels(itemModel, iteratedModels);
existingItems[Number(itemIndex)] = trySplitModels(itemModel, splitModels, iteratedModels);
}
} else {
existingItems = trySplitModels(existingItems, iteratedModels);
existingItems = trySplitModels(existingItems, splitModels, iteratedModels);
}
model.items = existingItems;
}
Expand Down
47 changes: 47 additions & 0 deletions test/interpreter/PostInterpreter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,53 @@ describe('PostInterpreter', () => {
expect(postProcessedModels).toHaveLength(1);
expect(postProcessedModels[0]).toMatchObject(expectedSchema1Model);
});
test('should split models when nested models occur', () => {
const rawModel = {
$id: 'schema1',
properties: {
testProp: {
type: 'array',
items: {
$id: 'schema2',
type: 'object'
}
}
}
};
const model = CommonModel.toCommonModel(rawModel);
(isModelObject as jest.Mock)
.mockReturnValueOnce(false)
.mockReturnValueOnce(true);

const postProcessedModels = postInterpretModel(model);

const expectedSchema1Model = CommonModel.toCommonModel({
$id: 'schema1',
properties: {
testProp: {
type: 'array',
items: {
$ref: 'schema2'
}
}
}
});
const expectedSchema2Model = CommonModel.toCommonModel({
$id: 'schema2',
type: 'object'
});

expect(postProcessedModels).toHaveLength(2);
expect(isModelObject).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: 'array'
}));
expect(isModelObject).toHaveBeenNthCalledWith(2, {
$id: 'schema2',
type: 'object'
});
expect(postProcessedModels[0]).toMatchObject(expectedSchema1Model);
expect(postProcessedModels[1]).toMatchObject(expectedSchema2Model);
});
test('should split models if properties contains model object', () => {
const rawModel = {
$id: 'schema1',
Expand Down

0 comments on commit 9d0d42e

Please sign in to comment.