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: duplicate dependencies being rendered #842

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
27 changes: 12 additions & 15 deletions src/models/ConstrainedMetaModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export class ConstrainedTupleModel extends ConstrainedMetaModel {
).map((tupleModel) => {
return tupleModel.value = tupleModel.value as ConstrainedReferenceModel;
});
//Ensure no duplicate references
dependencyModels = [...new Set(dependencyModels)];
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved

//Ensure no self references
dependencyModels = dependencyModels.filter((referenceModel) => {
return referenceModel.name !== this.name;
Expand Down Expand Up @@ -104,6 +107,9 @@ export class ConstrainedUnionModel extends ConstrainedMetaModel {
).map((unionModel) => {
return unionModel as ConstrainedReferenceModel;
});
//Ensure no duplicate references
dependencyModels = [...new Set(dependencyModels)];

//Ensure no self references
dependencyModels = dependencyModels.filter((referenceModel) => {
return referenceModel.name !== this.name;
Expand All @@ -125,21 +131,6 @@ export class ConstrainedEnumModel extends ConstrainedMetaModel {
public values: ConstrainedEnumValueModel[]) {
super(name, originalInput, type);
}

getNearestDependencies(): ConstrainedReferenceModel[] {
let dependencyModels = Object.values(this.values).filter(
(enumModel) => {
return enumModel.value instanceof ConstrainedReferenceModel;
}
).map((enumModel) => {
return enumModel.value as ConstrainedReferenceModel;
});
//Ensure no self references
dependencyModels = dependencyModels.filter((referenceModel) => {
return referenceModel.name !== this.name;
});
return dependencyModels;
}
}
export class ConstrainedDictionaryModel extends ConstrainedMetaModel {
constructor(
Expand All @@ -161,6 +152,9 @@ export class ConstrainedDictionaryModel extends ConstrainedMetaModel {
).map((model) => {
return model as ConstrainedReferenceModel;
});
//Ensure no duplicate references
dependencyModels = [...new Set(dependencyModels)];

//Ensure no self references
dependencyModels = dependencyModels.filter((referenceModel) => {
return referenceModel.name !== this.name;
Expand All @@ -186,6 +180,9 @@ export class ConstrainedObjectModel extends ConstrainedMetaModel {
).map((modelProperty) => {
return modelProperty.property as ConstrainedReferenceModel;
});
//Ensure no duplicate references
dependencyModels = [...new Set(dependencyModels)];

//Ensure no self references
dependencyModels = dependencyModels.filter((referenceModel) => {
return referenceModel.name !== this.name;
Expand Down
3 changes: 3 additions & 0 deletions test/blackbox/blackbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const filesToTest = [
}).filter(({ file }) => {
// Related to https://github.com/asyncapi/modelina/issues/389
return !file.includes('jenkins-config.json');
}).filter(({file}) => {
// Related to https://github.com/asyncapi/modelina/issues/840
// Related to https://github.com/asyncapi/modelina/issues/841
}).filter(({ file }) => {
// Related to https://github.com/asyncapi/modelina/issues/825
return !file.includes('circleci-config.json');
Expand Down
66 changes: 66 additions & 0 deletions test/models/ConstrainedMetaModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ describe('ConstrainedMetaModel', () => {
expect(dependencies).toHaveLength(1);
expect(dependencies[0]).toEqual(model.tuple[0].value);
});

test('should not return duplicate dependencies', () => {
const stringModel = new StringModel('', undefined);
const referenceModel = new ReferenceModel('', undefined, stringModel);
const referenceTupleModel = new TupleValueModel(0, referenceModel);
const reference2TupleModel = new TupleValueModel(1, referenceModel);
const rawModel = new TupleModel('test', undefined, [referenceTupleModel, reference2TupleModel]);

const model = constrainMetaModel(mockedTypeMapping, mockedConstraints, {
metaModel: rawModel,
constrainedName: '',
options: undefined
}) as ConstrainedTupleModel;
const dependencies = model.getNearestDependencies();
expect(dependencies).toHaveLength(1);
expect(dependencies[0]).toEqual(model.tuple[0].value);
});
});
describe('ObjectModel', () => {
test('should return all reference dependencies', () => {
Expand All @@ -121,6 +138,26 @@ describe('ConstrainedMetaModel', () => {
expect(dependencies[0]).toEqual(model.properties['reference'].property);
});

test('should not return duplicate dependencies', () => {
const stringModel = new StringModel('string', undefined);
const referenceModel = new ReferenceModel('reference', undefined, stringModel);
const referenceObjectPropertyModel = new ObjectPropertyModel('reference', false, referenceModel);
const reference2ObjectPropertyModel = new ObjectPropertyModel('reference2', false, referenceModel);
const rawModel = new ObjectModel('test', undefined, {
reference: referenceObjectPropertyModel,
reference2: reference2ObjectPropertyModel
});

const model = constrainMetaModel(mockedTypeMapping, mockedConstraints, {
metaModel: rawModel,
constrainedName: '',
options: undefined
}) as ConstrainedObjectModel;
const dependencies = model.getNearestDependencies();
expect(dependencies).toHaveLength(1);
expect(dependencies[0]).toEqual(model.properties['reference'].property);
});

describe('containsPropertyType', () => {
test('should find present property type and those who are not', () => {
const stringModel = new ConstrainedStringModel('', undefined, '');
Expand Down Expand Up @@ -154,6 +191,20 @@ describe('ConstrainedMetaModel', () => {
const referenceModel = new ReferenceModel('', undefined, stringModel);
const rawModel = new DictionaryModel('test', undefined, referenceModel, stringModel);

const model = constrainMetaModel(mockedTypeMapping, mockedConstraints, {
metaModel: rawModel,
constrainedName: '',
options: undefined
}) as ConstrainedDictionaryModel;
const dependencies = model.getNearestDependencies();
expect(dependencies).toHaveLength(1);
expect(dependencies[0]).toEqual(model.key);
});
test('should not return duplicate dependencies', () => {
const stringModel = new StringModel('', undefined);
const referenceModel = new ReferenceModel('', undefined, stringModel);
const rawModel = new DictionaryModel('test', undefined, referenceModel, referenceModel);

const model = constrainMetaModel(mockedTypeMapping, mockedConstraints, {
metaModel: rawModel,
constrainedName: '',
Expand Down Expand Up @@ -207,5 +258,20 @@ describe('ConstrainedMetaModel', () => {
expect(dependencies).toHaveLength(1);
expect(dependencies[0]).toEqual(model.union[0]);
});

test('should not return duplicate dependencies', () => {
const stringModel = new StringModel('', undefined);
const referenceModel = new ReferenceModel('', undefined, stringModel);
const rawModel = new UnionModel('test', undefined, [referenceModel, referenceModel]);

const model = constrainMetaModel(mockedTypeMapping, mockedConstraints, {
metaModel: rawModel,
constrainedName: '',
options: undefined
}) as ConstrainedUnionModel;
const dependencies = model.getNearestDependencies();
expect(dependencies).toHaveLength(1);
expect(dependencies[0]).toEqual(model.union[0]);
});
});
});