WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
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,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Fixed examples generation for operations with unions and nested enums for versioned services
5 changes: 4 additions & 1 deletion packages/openapi3/src/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ export function getExampleOrExamples(
) {
const [example, type] = examples[0];
const encodeAs = getEncodeAs(program, type);
return { example: serializeValueAsJson(program, example.value, type, encodeAs) };
const exactValueType = program.checker.getValueExactType(example.value);
return {
example: serializeValueAsJson(program, example.value, exactValueType ?? type, encodeAs),
};
} else {
const exampleObj: Record<string, OpenAPI3Example> = {};
for (const [index, [example, type]] of examples.entries()) {
Expand Down
39 changes: 39 additions & 0 deletions packages/openapi3/test/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,4 +962,43 @@ worksFor(supportedVersions, ({ openApiFor }) => {
"2021-01-01T00:00:00Z",
);
});

it("supports example generation for union that contains enums", async () => {
const res = await openApiFor(
`
@service
namespace MyService {
enum Enum {
a: "a",
b: "b",
}

model Foo {
entityType: "foo";
}

model Bar {
entityType: "bar";
enumValue: Enum;
}

@opExample(#{ returnType: #[#{ entityType: "bar", enumValue: Enum.a }] })
op testOp(): (Foo | Bar)[];
}
`,
);

ok(res.components?.schemas);
ok(res.paths["/"].get);
ok(res.paths["/"].get.responses);
ok("200" in res.paths["/"].get.responses);
ok("content" in res.paths["/"].get.responses["200"]);
ok(res.paths["/"].get.responses["200"].content);
expect(res.paths["/"].get?.responses["200"].content["application/json"].example).toEqual([
{
entityType: "bar",
enumValue: "a",
},
]);
});
});
48 changes: 48 additions & 0 deletions packages/openapi3/test/versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,53 @@ worksFor(supportedVersions, ({ openApiFor, version: specVersion }) => {
`,
);
});

it("supports example generation for union that contains enums", async () => {
const { v1 } = await openApiForVersions(
`
namespace MyService {
enum Versions {
v1
}
}

@versioned(Versions)
@service
namespace MyService {
enum Enum {
a: "a",
b: "b",
}

model Foo {
entityType: "foo";
}

model Bar {
entityType: "bar";
enumValue: Enum;
}

@opExample(#{ returnType: #[#{ entityType: "bar", enumValue: Enum.a }] })
op testOp(): (Foo | Bar)[];
}
`,
["v1"],
);

strictEqual(v1.info.version, "v1");
ok(v1.components?.schemas);
ok(v1.paths["/"].get);
ok(v1.paths["/"].get.responses);
ok("200" in v1.paths["/"].get.responses);
ok("content" in v1.paths["/"].get.responses["200"]);
ok(v1.paths["/"].get.responses["200"].content);
deepStrictEqual(v1.paths["/"].get?.responses["200"].content["application/json"].example, [
{
entityType: "bar",
enumValue: "a",
},
]);
});
});
});