Skip to content

[openapi3] add support for parameter examples via @opExample #7403

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

chrisradek
Copy link
Member

@chrisradek chrisradek commented May 20, 2025

Fixes #3873

This PR adds support for generating parameter examples in openapi3.

Notes:
From the OpenAPI spec (https://spec.openapis.org/oas/v3.0.4.html#fixed-fields-for-use-with-schema):

When example or examples are provided in conjunction with the schema field, the example SHOULD match the specified schema and follow the prescribed serialization strategy for the parameter.

The parameter-level examples are meant to be serialized as they would appear over the wire. This means in some cases, they conflict with the parameter schema. For example, when a header parameter is an array.

The OpenAPI spec provides a table that shows what an example should look like for any given parameter style:
https://spec.openapis.org/oas/v3.0.4.html#style-examples

Copy link
Contributor

github-actions bot commented May 20, 2025

All changed packages have been documented.

  • @typespec/openapi3
Show changes

@typespec/openapi3 - feature ✏️

Adds support for parameter examples via @opExample

if (!isSerializableScalarValue(value)) continue;
pairs.push(`${value.value}`);
}
return tk.value.createString(`?${name}=${encodeURIComponent(pairs.join(delimiter))}`);
Copy link
Member Author

Choose a reason for hiding this comment

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

I need to double check if there's anywhere else I need to encode URI components.

{
desc: "form (undefined)",
param: `@cookie color: string | null`,
paramExample: `null`,
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this test case be not passing the example value?

otherwise for null i feel like it probably still serialize as color=null (same for all (undefined)) cases

Copy link
Member Author

Choose a reason for hiding this comment

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

There's one more column to the tables, undefined, that I didn't include in the comments.

The undefined column replaces the empty column in previous versions of this specification in order to better align with [RFC6570] Section 2.3 terminology, which describes certain values including but not limited to null as “undefined” values with special handling; notably, the empty string is not undefined

So I took the value from the undefined columns for these cases.
(link to table: https://spec.openapis.org/oas/v3.0.4.html#style-examples)

@@ -234,4 +234,385 @@ worksFor(["3.0.0", "3.1.0"], ({ openApiFor }) => {
age: 2,
});
});

describe("parameters", () => {
it.each([
Copy link
Member

Choose a reason for hiding this comment

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

can each of the it.each be grouped under a describe with the current test used in the it message and replace the message of the it just as the desc defined

@azure-sdk
Copy link
Collaborator

azure-sdk commented May 20, 2025

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

op getColors(${param}): void;
`,
);
expect((res.paths[`/{color}`].get?.parameters[0] as OpenAPI3Parameter).example).toEqual(
Copy link
Member

Choose a reason for hiding this comment

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

should you also have a test to make sure it works with multiple examples

@@ -307,4 +307,47 @@ worksFor(["3.0.0", "3.1.0"], ({ openApiFor }) => {
},
});
});

it("supports encoding", async () => {
Copy link
Member

Choose a reason for hiding this comment

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

should have a test also that test that by default headers are encoded as rfc7231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend the @opExample decorator to support example values for path / query string parameters
3 participants