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

Fix enum in union example #4462

Merged
Merged
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
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
Loading