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

SchemaDifferences Referenced schema version validation #7272

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion common/api/ecschema-editing.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export type AnyIdentifier = ISchemaIdentifier | ISchemaItemIdentifier | IClassId
export type AnySchemaDifference = SchemaDifference | SchemaReferenceDifference | AnySchemaItemDifference | AnySchemaItemPathDifference | EntityClassMixinDifference | CustomAttributeDifference;

// @alpha
export type AnySchemaDifferenceConflict = SchemaDifferenceConflict<ConflictCode.ConflictingItemName, SchemaItemType> | SchemaDifferenceConflict<ConflictCode.ConflictingReferenceAlias, SchemaOtherTypes.SchemaReference> | SchemaDifferenceConflict<ConflictCode.ConflictingBaseClass, EcClassTypes> | SchemaDifferenceConflict<ConflictCode.RemovingBaseClass, EcClassTypes> | SchemaDifferenceConflict<ConflictCode.SealedBaseClass, EcClassTypes> | SchemaDifferenceConflict<ConflictCode.ConflictingClassModifier, EcClassTypes> | SchemaDifferenceConflict<ConflictCode.ConflictingEnumerationType, SchemaItemType.Enumeration> | SchemaDifferenceConflict<ConflictCode.ConflictingEnumeratorValue, SchemaOtherTypes.Enumerator> | SchemaDifferenceConflict<ConflictCode.ConflictingPersistenceUnit, SchemaItemType.KindOfQuantity> | SchemaDifferenceConflict<ConflictCode.MixinAppliedMustDeriveFromConstraint, SchemaOtherTypes.EntityClassMixin> | SchemaDifferenceConflict<ConflictCode.ConflictingPropertyName, SchemaOtherTypes.Property> | SchemaDifferenceConflict<ConflictCode.ConflictingPropertyKindOfQuantity, SchemaOtherTypes.Property> | SchemaDifferenceConflict<ConflictCode.ConflictingPropertyKindOfQuantityUnit, SchemaOtherTypes.Property> | SchemaDifferenceConflict<ConflictCode.AbstractConstraintMustNarrowBaseConstraints, SchemaOtherTypes.RelationshipConstraint> | SchemaDifferenceConflict<ConflictCode.DerivedConstraintsMustNarrowBaseConstraints, SchemaOtherTypes.RelationshipConstraint> | SchemaDifferenceConflict<ConflictCode.ConstraintClassesDeriveFromAbstractConstraint, SchemaOtherTypes.RelationshipConstraint>;
export type AnySchemaDifferenceConflict = SchemaDifferenceConflict<ConflictCode.ConflictingItemName, SchemaItemType> | SchemaDifferenceConflict<ConflictCode.ConflictingReferenceAlias, SchemaOtherTypes.SchemaReference> | SchemaDifferenceConflict<ConflictCode.ConflictingReferenceVersion, SchemaOtherTypes.SchemaReference> | SchemaDifferenceConflict<ConflictCode.ConflictingBaseClass, EcClassTypes> | SchemaDifferenceConflict<ConflictCode.RemovingBaseClass, EcClassTypes> | SchemaDifferenceConflict<ConflictCode.SealedBaseClass, EcClassTypes> | SchemaDifferenceConflict<ConflictCode.ConflictingClassModifier, EcClassTypes> | SchemaDifferenceConflict<ConflictCode.ConflictingEnumerationType, SchemaItemType.Enumeration> | SchemaDifferenceConflict<ConflictCode.ConflictingEnumeratorValue, SchemaOtherTypes.Enumerator> | SchemaDifferenceConflict<ConflictCode.ConflictingPersistenceUnit, SchemaItemType.KindOfQuantity> | SchemaDifferenceConflict<ConflictCode.MixinAppliedMustDeriveFromConstraint, SchemaOtherTypes.EntityClassMixin> | SchemaDifferenceConflict<ConflictCode.ConflictingPropertyName, SchemaOtherTypes.Property> | SchemaDifferenceConflict<ConflictCode.ConflictingPropertyKindOfQuantity, SchemaOtherTypes.Property> | SchemaDifferenceConflict<ConflictCode.ConflictingPropertyKindOfQuantityUnit, SchemaOtherTypes.Property> | SchemaDifferenceConflict<ConflictCode.AbstractConstraintMustNarrowBaseConstraints, SchemaOtherTypes.RelationshipConstraint> | SchemaDifferenceConflict<ConflictCode.DerivedConstraintsMustNarrowBaseConstraints, SchemaOtherTypes.RelationshipConstraint> | SchemaDifferenceConflict<ConflictCode.ConstraintClassesDeriveFromAbstractConstraint, SchemaOtherTypes.RelationshipConstraint>;

// @alpha
export type AnySchemaEdits = SkipEdit | RenameSchemaItemEdit | RenamePropertyEdit;
Expand Down Expand Up @@ -223,6 +223,8 @@ export enum ConflictCode {
// (undocumented)
ConflictingReferenceAlias = "C-002",
// (undocumented)
ConflictingReferenceVersion = "C-003",
// (undocumented)
ConstraintClassesDeriveFromAbstractConstraint = "C-1502",
// (undocumented)
DerivedConstraintsMustNarrowBaseConstraints = "C-1501",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/ecschema-editing",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/ecschema-editing"
}
2 changes: 2 additions & 0 deletions core/ecschema-editing/src/Differencing/SchemaConflicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export enum ConflictCode {

ConflictingItemName = "C-001",
ConflictingReferenceAlias = "C-002",
ConflictingReferenceVersion = "C-003",

ConflictingBaseClass = "C-100",
RemovingBaseClass = "C-101",
Expand Down Expand Up @@ -96,6 +97,7 @@ type EcClassTypes = SchemaItemType.CustomAttributeClass | SchemaItemType.Entity
export type AnySchemaDifferenceConflict =
SchemaDifferenceConflict<ConflictCode.ConflictingItemName, SchemaItemType> |
SchemaDifferenceConflict<ConflictCode.ConflictingReferenceAlias, SchemaOtherTypes.SchemaReference> |
SchemaDifferenceConflict<ConflictCode.ConflictingReferenceVersion, SchemaOtherTypes.SchemaReference> |
SchemaDifferenceConflict<ConflictCode.ConflictingBaseClass, EcClassTypes> |
SchemaDifferenceConflict<ConflictCode.RemovingBaseClass, EcClassTypes> |
SchemaDifferenceConflict<ConflictCode.SealedBaseClass, EcClassTypes> |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @module Differencing
*/

import { classModifierToString, ECClass, ECClassModifier, EntityClass, Enumeration, KindOfQuantity, LazyLoadedSchemaItem, Mixin, parseClassModifier, primitiveTypeToString, Property, propertyTypeToString, Schema, SchemaItem, SchemaItemKey } from "@itwin/ecschema-metadata";
import { classModifierToString, ECClass, ECClassModifier, EntityClass, Enumeration, KindOfQuantity, LazyLoadedSchemaItem, Mixin, parseClassModifier, primitiveTypeToString, Property, propertyTypeToString, Schema, SchemaItem, SchemaItemKey, SchemaMatchType } from "@itwin/ecschema-metadata";
import { AnyClassItemDifference, AnySchemaDifference, AnySchemaItemDifference, ClassPropertyDifference, ConstantDifference, CustomAttributeClassDifference, CustomAttributeDifference, EntityClassDifference, EntityClassMixinDifference, EnumerationDifference, EnumeratorDifference, FormatDifference, InvertedUnitDifference, KindOfQuantityDifference, MixinClassDifference, PhenomenonDifference, PropertyCategoryDifference, RelationshipClassDifference, RelationshipConstraintClassDifference, RelationshipConstraintDifference, SchemaDifference, SchemaReferenceDifference, StructClassDifference, UnitDifference, UnitSystemDifference } from "./SchemaDifference";
import { AnySchemaDifferenceConflict, ConflictCode } from "./SchemaConflicts";
import { SchemaDifferenceVisitor, SchemaDifferenceWalker } from "./SchemaDifferenceVisitor";
Expand Down Expand Up @@ -63,16 +63,30 @@ class SchemaDifferenceValidationVisitor implements SchemaDifferenceVisitor {
*/
public async visitSchemaReferenceDifference(entry: SchemaReferenceDifference) {
const sourceSchemaReference = await this._sourceSchema.getReference(entry.difference.name) as Schema;
const targetSchemaReference = this._targetSchema.getReferenceNameByAlias(sourceSchemaReference.alias);
if (targetSchemaReference && targetSchemaReference !== sourceSchemaReference.name) {
const targetSchemaReferenceName = this._targetSchema.getReferenceNameByAlias(sourceSchemaReference.alias);
if (targetSchemaReferenceName && targetSchemaReferenceName !== sourceSchemaReference.name) {
this.addConflict({
code: ConflictCode.ConflictingReferenceAlias,
difference: entry,
source: entry.difference.name,
target: targetSchemaReference,
target: targetSchemaReferenceName,
description: "Target schema already references a different schema with this alias.",
});
}

const sourceSchemaKey = sourceSchemaReference.schemaKey;
const targetSchemaKey = await this._targetSchema.getReference(entry.difference.name)
.then((schema) => schema?.schemaKey);

if(entry.changeType === "modify" && targetSchemaKey && !sourceSchemaKey.matches(targetSchemaKey, SchemaMatchType.LatestWriteCompatible)) {
return this.addConflict({
code: ConflictCode.ConflictingReferenceVersion,
difference: entry,
description: "Schema reference cannot be updated, incompatible versions",
source: sourceSchemaKey.toString(),
target: targetSchemaKey.toString(),
});
}
}

/**
Expand Down
47 changes: 47 additions & 0 deletions core/ecschema-editing/src/test/Differencing/Conflicts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,53 @@ describe("Schema Difference Conflicts", () => {
return true;
});
});

it("should find a conflict schema references aren't compartible", async () => {
const sourceContext = new SchemaContext();
await Schema.fromJson({
$schema: "https://dev.bentley.com/json_schemas/ec/32/ecschema",
name: "ReferenceA",
version: "2.0.0",
alias: "ref",
}, sourceContext);
const sourceSchema = await Schema.fromJson({
...schemaHeader,
references: [
{
name: "ReferenceA",
version: "2.0.0",
},
],
}, sourceContext);

const targetContext = new SchemaContext();
await Schema.fromJson({
$schema: "https://dev.bentley.com/json_schemas/ec/32/ecschema",
name: "ReferenceA",
version: "1.0.0",
alias: "ref",
}, targetContext);

const targetSchema = await Schema.fromJson({
...schemaHeader,
references: [
{
name: "ReferenceA",
version: "1.0.0",
},
],
}, targetContext);

const differences = await getSchemaDifferences(targetSchema, sourceSchema);
expect(differences.conflicts).to.have.a.lengthOf(1).and.satisfies(([conflict]: AnySchemaDifferenceConflict[]) => {
expect(conflict).to.have.a.property("code", ConflictCode.ConflictingReferenceVersion);
expect(conflict).to.have.a.property("source", "ReferenceA.02.00.00");
expect(conflict).to.have.a.property("target", "ReferenceA.01.00.00");
expect(conflict).to.have.a.property("description", "Schema reference cannot be updated, incompatible versions");
expect(conflict).to.have.a.nested.property("difference.schemaType", "SchemaReference");
return true;
});
});
});

describe("Different schema item type conflicts", () => {
Expand Down
Loading