diff --git a/common/changes/@autorest/modelerfour/fix-m4-properties-duplicates_2021-11-17-23-11.json b/common/changes/@autorest/modelerfour/fix-m4-properties-duplicates_2021-11-17-23-11.json new file mode 100644 index 000000000..673573eaa --- /dev/null +++ b/common/changes/@autorest/modelerfour/fix-m4-properties-duplicates_2021-11-17-23-11.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@autorest/modelerfour", + "comment": "**Fix** issue with properties causing name conflict when consecutive words are deduplicated", + "type": "patch" + } + ], + "packageName": "@autorest/modelerfour", + "email": "tiguerin@microsoft.com" +} \ No newline at end of file diff --git a/packages/extensions/modelerfour/src/prenamer/prenamer.ts b/packages/extensions/modelerfour/src/prenamer/prenamer.ts index bdff134ec..c2d4f5c7a 100644 --- a/packages/extensions/modelerfour/src/prenamer/prenamer.ts +++ b/packages/extensions/modelerfour/src/prenamer/prenamer.ts @@ -169,9 +169,16 @@ export class PreNamer { for (const schema of values(this.codeModel.schemas.objects)) { scopeNamer.add(schema, this.format.type, ""); + const propertyScopeName = new ScopeNamer(this.session, { + deduplicateNames: true, + overrides: this.format.override, + }); + for (const property of values(schema.properties)) { - setName(property, this.format.property, "", this.format.override); + propertyScopeName.add(property, this.format.property, ""); + // setName(property, this.format.property, "", this.format.override); } + propertyScopeName.process(); } for (const schema of values(this.codeModel.schemas.groups)) { diff --git a/packages/extensions/modelerfour/test/prenamer/prenamer.test.ts b/packages/extensions/modelerfour/test/prenamer/prenamer.test.ts index abaa1f491..040f23806 100644 --- a/packages/extensions/modelerfour/test/prenamer/prenamer.test.ts +++ b/packages/extensions/modelerfour/test/prenamer/prenamer.test.ts @@ -1,4 +1,4 @@ -import { ChoiceSchema, CodeModel, ObjectSchema, Operation } from "@autorest/codemodel"; +import { ChoiceSchema, CodeModel, ObjectSchema, Operation, Property, StringSchema } from "@autorest/codemodel"; import { ModelerFourOptions } from "modeler/modelerfour-options"; import { PreNamer } from "../../src/prenamer/prenamer"; import { createTestSessionFromModel } from "../utils"; @@ -12,6 +12,8 @@ const runPrenamer = async (model: CodeModel, options: ModelerFourOptions = {}) = describe("Prenamer", () => { let model: CodeModel; + const stringSchema = new StringSchema("", ""); + beforeEach(() => { model = new CodeModel("TestPrenamer"); }); @@ -65,6 +67,52 @@ describe("Prenamer", () => { }); }); + describe("in objects properties", () => { + it("Remove duplicate consecutive words by default", async () => { + const schema = new ObjectSchema("MyObject", "Description"); + schema.addProperty(new Property("fooBarBar", "desc", stringSchema)); + model.schemas.add(schema); + const result = await runPrenamer(model); + const obj = result.schemas.objects?.[0]; + expect(obj?.properties?.[0].language.default.name).toEqual("fooBar"); + }); + + it("Keeps duplicate consecutive words if the new name already exists", async () => { + const schema = new ObjectSchema("MyObject", "Description"); + schema.addProperty(new Property("fooBar", "desc", stringSchema)); + schema.addProperty(new Property("fooBarBar", "desc", stringSchema)); + model.schemas.add(schema); + const result = await runPrenamer(model); + const obj = result.schemas.objects?.[0]; + expect(obj?.properties?.[0].language.default.name).toEqual("fooBar"); + expect(obj?.properties?.[1].language.default.name).toEqual("fooBarBar"); + }); + + it("Keeps duplicate consecutive words if the new name already exists and still style the word", async () => { + const schema = new ObjectSchema("MyObject", "Description"); + schema.addProperty(new Property("fooBar", "desc", stringSchema)); + schema.addProperty(new Property("fooBar-Bar", "desc", stringSchema)); + model.schemas.add(schema); + const result = await runPrenamer(model); + const obj = result.schemas.objects?.[0]; + expect(obj?.properties?.[0].language.default.name).toEqual("fooBar"); + expect(obj?.properties?.[1].language.default.name).toEqual("fooBarBar"); + }); + + it("deduplicate property names if it can't find a better name", async () => { + const schema = new ObjectSchema("MyObject", "Description"); + schema.addProperty(new Property("fooBar", "desc", stringSchema)); + schema.addProperty(new Property("fooBarBar", "desc", stringSchema)); + schema.addProperty(new Property("fooBar-Bar", "desc", stringSchema)); + model.schemas.add(schema); + const result = await runPrenamer(model); + const obj = result.schemas.objects?.[0]; + expect(obj?.properties?.[0].language.default.name).toEqual("fooBar"); + expect(obj?.properties?.[1].language.default.name).toEqual("fooBarBar"); + expect(obj?.properties?.[2].language.default.name).toEqual("fooBarBarAutoGenerated"); + }); + }); + describe("in operation names", () => { it("Remove duplicate consecutive words by default", async () => { model.getOperationGroup("foo").addOperation(