Skip to content

Commit fd4c329

Browse files
committed
Add clarifications on how to use mediaType trait
This commit clarifies what the mediaType trait is used for, the fact that it's a design-time contract, and adds a missing header serialization test to ensure that mediaType trait strings are base64 encoded.
1 parent de4dd08 commit fd4c329

File tree

3 files changed

+98
-8
lines changed

3 files changed

+98
-8
lines changed

docs/source/1.0/spec/core/protocol-traits.rst

+42-7
Original file line numberDiff line numberDiff line change
@@ -209,28 +209,63 @@ following document:
209209
-------------------
210210

211211
Summary
212-
Describes the contents of a blob or string shape using a media type as
213-
defined by :rfc:`6838` (e.g., "video/quicktime").
212+
Describes the contents of a blob or string shape using a design-time
213+
media type as defined by :rfc:`6838` (for example, ``application/json``).
214214
Trait selector
215215
``:is(blob, string)``
216216

217217
*Any blob or string*
218218
Value type
219219
``string``
220220

221-
The ``mediaType`` can be used in tools for documentation, validation,
222-
automated conversion or encoding in code, automatically determining an
223-
appropriate Content-Type for an HTTP-based protocol, etc.
224-
225-
The following example defines a video/quicktime blob:
221+
The following example defines a ``video/quicktime`` blob:
226222

227223
.. tabs::
228224

229225
.. code-tab:: smithy
230226

227+
namespace smithy.example
228+
231229
@mediaType("video/quicktime")
232230
blob VideoData
233231

232+
.. code-tab:: json
233+
234+
{
235+
"smithy": "1.0",
236+
"shapes": {
237+
"smithy.example#VideoData": {
238+
"type": "blob",
239+
"traits": {
240+
"smithy.api#mediaType": "video/quicktime"
241+
}
242+
}
243+
}
244+
}
245+
246+
.. rubric:: Use cases
247+
248+
The primary function of the ``mediaType`` trait is to send open content
249+
data over the wire inside of values that are isolated from the rest of
250+
a payload using exact representations of customer provided data. While the
251+
model does define the serialization format of values able to be stored in a
252+
shape at design-time using a media type, models are not required to define
253+
any kind of schema for the shape.
254+
255+
The ``mediaType`` trait can be used to aid tools in documentation,
256+
validation, special-cased helpers to serialize and deserialize media type
257+
contents in code, assigning a fixed Content-Type when using
258+
:ref:`HTTP bindings <http-traits>`, etc.
259+
260+
.. rubric:: Comparisons to document types
261+
262+
The serialization format of a shape marked with the ``@mediaType`` trait is
263+
an important part of its contract. In contrast, document types are
264+
serialized in a protocol-agnostic way and can only express data types as
265+
granular as the JSON-type system. Design-time media types are preferred over
266+
document types when the exact bytes of a value are required for an
267+
application to function.
268+
234269

235270
.. _timestampFormat-trait:
236271

smithy-aws-protocol-tests/model/restJson1/http-headers.smithy

+55-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ structure NullAndEmptyHeadersIO {
324324
c: StringList,
325325
}
326326

327-
/// The example tests how timestamp request and response headers are serialized.
327+
/// This example tests how timestamp request and response headers are serialized.
328328
@http(uri: "/TimestampFormatHeaders", method: "POST")
329329
operation TimestampFormatHeaders {
330330
input: TimestampFormatHeadersIO,
@@ -413,3 +413,57 @@ structure TimestampFormatHeadersIO {
413413
@httpHeader("X-targetDateTime")
414414
targetDateTime: DateTime,
415415
}
416+
417+
/// This example ensures that mediaType strings are base64 encoded in headers.
418+
@readonly
419+
@http(uri: "/MediaTypeHeader", method: "GET")
420+
operation MediaTypeHeader {
421+
input: MediaTypeHeaderInput,
422+
output: MediaTypeHeaderOutput
423+
}
424+
425+
apply MediaTypeHeader @httpRequestTests([
426+
{
427+
id: "MediaTypeHeaderInputBase64",
428+
documentation: "Headers that target strings with a mediaType are base64 encoded",
429+
protocol: restJson1,
430+
method: "GET",
431+
uri: "/MediaTypeHeader",
432+
headers: {
433+
"X-Json": "dHJ1ZQ=="
434+
},
435+
body: "",
436+
params: {
437+
json: "true"
438+
}
439+
},
440+
])
441+
442+
apply MediaTypeHeader @httpResponseTests([
443+
{
444+
id: "MediaTypeHeaderOutputBase64",
445+
documentation: "Headers that target strings with a mediaType are base64 encoded",
446+
protocol: restJson1,
447+
code: 200,
448+
headers: {
449+
"X-Json": "dHJ1ZQ=="
450+
},
451+
body: "",
452+
params: {
453+
json: "true"
454+
}
455+
},
456+
])
457+
458+
structure MediaTypeHeaderInput {
459+
@httpHeader("X-Json")
460+
json: JsonValue,
461+
}
462+
463+
structure MediaTypeHeaderOutput {
464+
@httpHeader("X-Json")
465+
json: JsonValue,
466+
}
467+
468+
@mediaType("application/json")
469+
string JsonValue

smithy-aws-protocol-tests/model/restJson1/main.smithy

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ service RestJson {
2323
NullAndEmptyHeadersClient,
2424
NullAndEmptyHeadersServer,
2525
TimestampFormatHeaders,
26+
MediaTypeHeader,
2627

2728
// @httpLabel tests
2829
HttpRequestWithLabels,

0 commit comments

Comments
 (0)