How should parameter examples be written? #4622
Replies: 9 comments 13 replies
-
This is a very interesting question! My understanding had always been that the example/examples for a query parameter would only contain the value, but now I see why that might not give the whole picture -- in particular for a parameter with parameters:
- name: colors
in: query
explode: true
schema:
type: array
items:
type: string
example: # what to put here? Specifying Hmmm. Are the tools just wrong on this? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I'm leaning towards the tooling being wrong here and I would venture to guess there aren't any tools that considerate the default serialization rules either. This may be a unique area of the spec where the output is transformed. I'm also not sure if the example value should contain the leading cc @tatomyr @AlexVarchuk something to consider for example validation and Redoc display of serialized content |
Beta Was this translation helpful? Give feedback.
-
It's not clear to me that the "requirements" have been clearly expressed or have been understood in the way you have interpreted them. I searched the OAS specs and the OAS examples on the Learn site to find an example to confirm or dispute this interpretation and found none. I could not find one example of a query parameter that specifies an "example" value (forgive me if I missed it but searching on "example" shows so many false positives!). From this I would conclude that we haven't really been clear about what an example value should be. If that's right, then I don't think it is unreasonable for tool vendors to have assumed that the example value for a query parameter is just the value for the parameter, without the "key=" embellishment. This is what I always assumed. Are there examples of OpenAPI descriptions in the wild that assumed the opposite -- that you have to include the "key=" in the example? |
Beta Was this translation helpful? Give feedback.
-
Here's my opinion and it is very biased. Example should be the "input value". We are trying to provide value to consumers of the OpenAPI description and having an example of the input value is useful. Having example show what the serialized version of that input value is a whole lot let interesting. There is a huge caveat to my suggestion. It assumes someone uses a URI Template library for generating their URL. It is quite likely that many people/tools do not do that. They try and hand craft their URLs and now the OpenAPI description is not helping to explain how to serialize the parameters in the URL. Personally, I don't think people should be hand crafting URLs when calling URLs. I do think it is a job of documentation tooling to show what a properly serialized URL should look like. I don't think it is the job of an OpenAPI description. It isn't completely obvious what we should do in the case of URLs, but consider the a case of a request body for an operation that has a |
Beta Was this translation helpful? Give feedback.
-
As much as it pains me to let implementations direct the spec, it really feels like there is such a strong consensus in implementations that we'll just be introducing pain by trying to fight it in @mikekistler asked me what I thought about this, so I tested the most popular linters as measured by GitHub stars:
@handrews I also tested |
Beta Was this translation helpful? Give feedback.
-
OK here's a revised proposal for
This object MAY be extended with Specification Extensions. Note that the serialized form of query parameters ( Picking one of the parameter examples from my now-hidden outdated comment above, plus some other parameters:
- name: color
schema:
type: array
items:
type: string
style: form
explode: true
examples:
"":
dataValue: [blue, black, brown]
serializedValue: color=blue&color=black&color=brown
- name: flag
schema:
type: boolean
examples:
"true":
summary: show representation of boolean true
dataValue: true
serializedValue: "flag=true"
"false":
summary: show representation of boolean false
dataValue: false
serializedValue: "flag=false"
- name: usernames
in: path
schema:
type: array
items:
type: string
style: matrix
explode: false
examples:
"":
dataValue: [handrews, 张]
serializedValue: ;handrews;%E5%BC%A0
- name: cookie
in: cookie
# We can't use `style` because it enforces URI percent-encoding, which is wrong for cookies
content:
text/plain; charset=utf-8:
schema:
type: string
examples:
"":
summary: There is no encoding to apply here
dataValue: 张
serializedValue: 张
examples:
"":
summary: Show that cookies use base64 encoding
dataValue: 张
serializedValue: 5byg
requestBody:
content:
image/png:
examples:
"":
summary: Raw binary images are not encoded
externalDataValue: ./examples/body.png
externalSerializedValue: ./examples/body.png |
Beta Was this translation helpful? Give feedback.
-
[NOTE: This is split out from the first version of the previous thread, which had too many things in it. This part can be an add-on, but is not needed for the basic proposal above. It could even be done totally separately, but probably doesn't make much sense without the first part.] In addition to the above, we should allow using Using the So in order to show an example of the fully serialized query string contents, we need to allow parameters
- in: querystring
name: selector
content:
application/jsonpath:
schema:
type: string
examples:
"":
dataValue: $.a.b[1:1]
serializedValue: $.a.b[1:1]
examples:
"":
dataValue: $.a.b[1:1]
serializedValue: %24.a.b%5B1%3A1%5D In this scenario, I would likely only use |
Beta Was this translation helpful? Give feedback.
-
A third part of this would be supporting This third proposal allows NOTE: I need to think a bit more on the correct serialized form in the Encoding Object- by parallel with the Parameter Object, it should include the parameter names when doing Separately, As this media type is (by definition) URL-ready, the Parameter Object implementation MUST NOT apply another layer of percent-encoding. This is just a thing that implementations will need to know about using certain media types in certain places- probably something that we need to put in the media type registry: parameters:
- name: qs
in: querystring
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
metadata:
type: object
properties:
name:
type: string
icon:
type: string
contentEncoding: base64url
encoding:
metadata:
examples:
"":
dataValue: {"name": "2x2 Red"}
icon:
contentType: image/png, image/jpeg
examples:
"":
summary: A solid red 2x2 pixel image
externalDataValue: ./examples/red2by2.png
# NOTE: It's possible that this should have `icon=` at the start, need to figure that out
serializedValue: iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAABGdBTUEAALGPC_xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAqADAAQAAAABAAAAAgAAAADO0J6QAAAAEElEQVQIHWP8zwACTGCSAQANHQEDqtPptQAAAABJRU5ErkJggg%3D%3D
examples:
"":
summary: Example using the dataValue or externalDataValue from each property's unnamed example in the Encoding Object, with JSON whitespace minimized
serializedValue: metadata=%7B%22name%22%3A%222x2%20Red%22%7D&icon=iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAABGdBTUEAALGPC_xhBQAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAAqADAAQAAAABAAAAAgAAAADO0J6QAAAAEElEQVQIHWP8zwACTGCSAQANHQEDqtPptQAAAABJRU5ErkJggg%3D%3D If we did the querystring example with the icon and metadata as a multipart body and did not base64-encode the PNG, we'd need to use requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
metadata:
type: object
properties:
name:
type: string
icon:
type: string
encoding:
metadata:
examples:
"":
dataValue: {"name": "2x2 Red"}
icon:
contentType: image/png, image/jpeg
examples:
"":
summary: A solid red 2x2 pixel image
externalDataValue: ./examples/red2by2.png
# NOTE: It's possible that this should be a complete part serialization, including part headers. Need to figure this out.
externalSerializedValue: ./examples/red2by2.png
examples:
"":
summary: Example using the dataValue or externalDataValue from each property's unnamed example in the Encoding Object, with JSON whitespace minimized
externalSerializedValue: ./examples/multipartWithBinaryPart.multipart |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to understand whether or not to apply serialization to parameter examples. If I'm following #3041 and the changes added to 3.0.4, it looks like parameter examples should be serialized according to this table:
https://spec.openapis.org/oas/v3.0.4.html#style-examples
So then I'd have parameters specified like this:
However this seems to cause problems for the tooling I've tested with (swagger-ui, spectral, vacuum) and seems to contradict documentation around parameters elsewhere (where the serialization:
https://swagger.io/docs/specification/v3_0/describing-parameters/#parameter-examples
I work on a tool that generates Open API and am adding support for emitting parameter examples, but came across issues around tooling support as part of validation. I'm curious if there are any tools that do work with the serialized examples.
Beta Was this translation helpful? Give feedback.
All reactions