Skip to content

Commit

Permalink
Fix enum in union example (#4462)
Browse files Browse the repository at this point in the history
fix #4416
  • Loading branch information
timotheeguerin authored Oct 4, 2024
1 parent 0d1b320 commit 7c425cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions packages/compiler/src/lib/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
Expand Down
19 changes: 19 additions & 0 deletions packages/openapi3/test/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 7c425cd

Please sign in to comment.