diff --git a/.chronus/changes/fix-enum-in-union-example-2024-8-17-18-25-2.md b/.chronus/changes/fix-enum-in-union-example-2024-8-17-18-25-2.md new file mode 100644 index 0000000000..d25024443d --- /dev/null +++ b/.chronus/changes/fix-enum-in-union-example-2024-8-17-18-25-2.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Fix examples with enums inside of unions diff --git a/packages/compiler/src/lib/examples.ts b/packages/compiler/src/lib/examples.ts index 58810d9b9a..241844ae56 100644 --- a/packages/compiler/src/lib/examples.ts +++ b/packages/compiler/src/lib/examples.ts @@ -74,11 +74,10 @@ function resolveUnions(program: Program, value: ObjectValue, type: Type): Type | } for (const variant of type.variants.values()) { if ( - variant.type.kind === "Model" && ignoreDiagnostics( program.checker.isTypeAssignableTo( - value, - { entityKind: "MixedParameterConstraint", valueType: variant.type }, + value.type.projectionBase ?? value.type, + variant.type.projectionBase ?? variant.type, value, ), ) diff --git a/packages/openapi3/test/examples.test.ts b/packages/openapi3/test/examples.test.ts index b1932901c2..064e2da47d 100644 --- a/packages/openapi3/test/examples.test.ts +++ b/packages/openapi3/test/examples.test.ts @@ -31,6 +31,25 @@ describe("schema examples", () => { ); expect(res.components.schemas.Test.example).toEqual({ dob: "2021-01-01" }); }); + + it("enum in union", async () => { + const res = await openApiFor( + ` + enum Types {a, b} + + model A { type: Types;} + + union Un { A } + + @example(#{ prop: #{ type: Types.a } }) + model Test { + prop: Un; + } + + `, + ); + expect(res.components.schemas.Test.example).toEqual({ prop: { type: "a" } }); + }); }); describe("operation examples", () => {