Skip to content

v3.2 Arrange encoding information more clearly #4562

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 5 commits into
base: v3.2-dev
Choose a base branch
from

Conversation

handrews
Copy link
Member

@handrews handrews commented May 1, 2025

[NOTE: The empty "Media Type Registry" header is there to avoid build errors- it will be a minor conflict to resolve when PR #4554 is merged]

TL;DR: Supporting multipart/mixed (streaming or otherwise) will require a different way to map Encoding Objects to data structures, so this splits the mapping definition (controlled by the Media Type Object's encoding field) from the logic of the Encoding Object itself. Previously the mapping details were in the Encoding Object description, despite the Encoding Object only applying to one value at a time.


As dicussed in today's TDC call, we have increasing (and modern) use cases for supporting multipart/mixed (which we previously claimed to support but never did). This refactor makes possible future support easier by moving the array special case, which is governed by the multipart/form-data RFC, out of the Encoding Object (which needs to work with other multipart formats) and placing it with the Media Type Object's encoding field (which is web form-format-specific).

This PR refactors encoding guidance to put the rules for mapping Encoding Objects to valules with the encoding field (which performs the mapping) rather than having most of it in the Encoding Object (which should focus on how to apply a single Encoding Object to a single value).

This notably takes the special handling of arrays as repeated values out of the Encoding Object section (and the default contentType field value table) and moves it to the Media Type Object. The Encoding Object behavior is now consistent for all types, while the mapping done by the encoding field handles the special case.

The only change (as opposed to re-organization and re-wording) in this PR is the addition of a default contentType of application/json for array values, which in the context of the existing behavior is only relevant for array values nested under a top-level array. Past OAS versions were silent on this topic, and presumably it just does not come up much, but it was a gap we should fill.

  • schema changes are included in this pull request
  • schema changes are needed for this pull request but not done yet
  • no schema changes are needed for this pull request

Refactor this to put the rules for mapping Encoding Objects
to valules with the `encoding` field (which performs the mapping)
rather than having most of it in the Encoding Object (which should
focus on how to apply a single Encoding Object to a single value).

This notably takes the special handling of arrays as repeated
values out of the Encoding Object section (and the default
`contentType` field value table) and moves it to the Media Type
Object.  The Encoding Object behavior is now consistent for all
types, while the _mapping_ done by the `encoding` field handles
the special case.

The only change (as opposed to re-organization and re-wording)
in this PR is the addition of a default `contentType` of
`application/json` for array values, which in the context of
the existing behavior is only relevant for array values nested
under a top-level array.  Past OAS versions were silent on this
topic, and presumably it just does not come up much, but it was
a gap we should fill.

As dicussed in today's TDC call, we have increasing (and modern)
use cases for supporting `multipart/mixed` (which we previously
claimed to support but never did).  This refactor makes possible
future support easier by moving the array special case, which
is governed by the `multipart/form-data` RFC, out of the Encoding
Object (which needs to work with other `multipart` formats) and
places it with the `encoding` field (which is web form-format-specific).
@handrews handrews added the media and encoding Issues regarding media type support and how to encode data (outside of query/path params) label May 1, 2025
@handrews handrews added this to the v3.2.0 milestone May 1, 2025
@handrews handrews requested review from a team as code owners May 1, 2025 20:56
@handrews handrews changed the title Arrange encoding information more clearly v3.2 Arrange encoding information more clearly May 1, 2025
@handrews
Copy link
Member Author

handrews commented May 1, 2025

Note that a lot of this could be backported to 3.1.2, although it's not clear to me that doing so would be the ideal move.

Copy link
Contributor

@mikekistler mikekistler left a comment

Choose a reason for hiding this comment

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

This mostly looks right, but I don't feel confident in my own understanding of the details to "approve". But I did suggest a few minor rewordings and asked for clarification on one curious point.

@@ -1615,10 +1617,33 @@ See [Working With Examples](#working-with-examples) for further guidance regardi
| <a name="media-type-schema"></a>schema | [Schema Object](#schema-object) | The schema defining the content of the request, response, parameter, or header. |
| <a name="media-type-example"></a>example | Any | Example of the media type; see [Working With Examples](#working-with-examples). |
| <a name="media-type-examples"></a>examples | Map[ `string`, [Example Object](#example-object) \| [Reference Object](#reference-object)] | Examples of the media type; see [Working With Examples](#working-with-examples). |
| <a name="media-type-encoding"></a>encoding | Map[`string`, [Encoding Object](#encoding-object)] | A map between a property name and its encoding information. The key, being the property name, MUST exist in the schema as a property. The `encoding` field SHALL only apply when the media type is `multipart` or `application/x-www-form-urlencoded`. If no Encoding Object is provided for a property, the behavior is determined by the default values documented for the Encoding Object. |
| <a name="media-type-encoding"></a>encoding | Map[`string`, [Encoding Object](#encoding-object)] | A map between a property name and its encoding information for media types supporting name-value pairs and allowing duplicate names, as defined under [Encoding Usage and Restrictions](#encoding-usage-and-restrictions). |
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm unclear why it is necessary for the media type to allow duplicate names. What breaks if the media type does not allow duplicate names?

Copy link
Member Author

@handrews handrews May 5, 2025

Choose a reason for hiding this comment

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

@mikekistler If you try to use an array value with a media type that does not support duplicate names, you will get duplicate names anyway, and that usually isn't good.

The only media types for which the encoding field (and indeed the Encoding Object as a whole) has well-defined behavior at all are application/x-www-form-urlencoded and multipart/form-data, both of which support duplicate names (which is how you are required to specify multiple file uploads). So this does not add any new restriction, it just documents the implicit restriction that already exists.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mikekistler I just pushed a commit noting the history of this field in implementing web forms, which (to me, at least) explains why the field has such a specific usage pattern. Please let me know if that helps! I agree that otherwise it seems quite random.

handrews and others added 3 commits May 5, 2025 16:40
Co-authored-by: Mike Kistler <[email protected]>
The oddities of its media type support derive from its history
as the OAS implementation of web forms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
media and encoding Issues regarding media type support and how to encode data (outside of query/path params)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants