Skip to content

Commit

Permalink
feat(tcgc): throw error when trying to flattening a model with polymo…
Browse files Browse the repository at this point in the history
…rphsim

resolve #959
  • Loading branch information
archerzz committed Jul 23, 2024
1 parent 5362d7c commit 4e6dfa4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Type,
Union,
createDiagnosticCollector,
getDiscriminator,
getNamespaceFullName,
getProjectedName,
ignoreDiagnostics,
Expand Down Expand Up @@ -991,6 +992,13 @@ export const $flattenProperty: FlattenPropertyDecorator = (
target: ModelProperty,
scope?: LanguageScopes
) => {
if (getDiscriminator(context.program, target)) {
reportDiagnostic(context.program, {
code: "flatten-polymorphism",
format: {},
target: target,
});
}
setScopedDecoratorData(context, $flattenProperty, flattenPropertyKey, target, true, scope); // eslint-disable-line deprecation/deprecation
};

Expand Down
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ export const $lib = createTypeSpecLibrary({
nonDecorator: paramMessage`Client name: "${"name"}" is defined somewhere causing nameing conflicts in language scope: "${"scope"}"`,
},
},
"flatten-polymorphism": {
severity: "error",
messages: {
default: `Flattening polymorphic types is not supported.`,
},
},
},
});

Expand Down
27 changes: 27 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,33 @@ describe("typespec-client-generator-core: decorators", () => {
code: "decorator-wrong-target",
});
});

it("throws error when used on a polymorphism type", async () => {
const diagnostics = await runner.diagnose(`
@service({})
@test namespace MyService {
@test
model Model1{
@flattenProperty
child: Model2;
}
@test
@discriminator("kind")
model Model2{
kind: string;
}
@test
@route("/func1")
op func1(@body body: Model1): void;
}
`);

expectDiagnostics(diagnostics, {
code: "flatten-polymorphism",
});
});
});

describe("@clientName", () => {
Expand Down

0 comments on commit 4e6dfa4

Please sign in to comment.