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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution but I am not sure this is the right fix for this issue. I assume the problem must be that the enum must not be mutated correctly so it return a different instance for the value and the type. While this fix probably workaround this issue I think it also has some negative issues of lossing the original type information which might have encoding and other things

};
} 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",
},
]);
});
});
});
Loading