In an OpenAPI document, how do you describe a multipart/form-data upload and what does encoding control?
answer
- Not parameters — a body with a media type key
- One schema property per part
- Part media types are inferred by default
- A sibling map overrides that inference
- The binary spelling changed between versions
basics
~20 sDeclare a requestBody with a multipart/form-data content entry whose schema is an object: each property becomes one part. The encoding map then overrides a part's Content-Type and adds part headers, since OpenAPI otherwise infers the part type from the property's schema.
solid answer
~40 sA multipart upload is a `requestBody` whose `content` has a `multipart/form-data` key. Its `schema` is an object, and each property becomes one part named after the property. OpenAPI infers each part's media type from the property's schema — an object property defaults to `application/json`, a plain scalar to `text/plain`, and binary content to `application/octet-stream`. When that inference is wrong you add an `encoding` map beside the schema, keyed by property name: `contentType` overrides the part's `Content-Type` (for example pinning an avatar to `image/png`), and `headers` declares extra part headers. Binary content is spelled differently across versions: OpenAPI 3.0 uses `type: string, format: binary`, while 3.1 aligns with JSON Schema 2020-12 and expresses it with `contentMediaType` and `contentEncoding`. An array-typed property means a repeated part — several files under one field name.
code
yaml · 26 linesrequestBody:
required: true
content:
multipart/form-data:
schema:
type: object
required:
- avatar
properties:
metadata:
type: object
properties:
caption:
type: string
avatar:
type: string
format: binary
encoding:
metadata:
contentType: application/json
avatar:
contentType: image/png, image/jpeg
headers:
X-Checksum:
schema:
type: stringgo deeper
Recall that an upload is a requestBody with a multipart/form-data content entry whose object schema has one property per part, not a list of parameters.
Explain the default content type each property type receives and what the encoding map's contentType and headers fields override.
Show that you track the 3.0 versus 3.1 spelling of binary content across the whole document and that you separate what the spec describes from what the server must enforce.
Own the upload contract across services — one binary spelling, one convention for metadata parts, and a clear statement of where size and type enforcement actually lives.
## The shape of a multipart declaration Multipart bodies are not parameters. They are a `requestBody` with a `multipart/form-data` key in the `content` map, and their Media Type Object holds two things that matter: `schema` and `encoding`. The `schema` is an object. **Each property becomes one part**, and the part's `name` in its `Content-Disposition` header is the property name. So a schema with properties `metadata` and `file` produces a body with a `metadata` part and a `file` part. `required` on the schema marks which parts must be present. ## How a part's content type is chosen OpenAPI infers a default `Content-Type` for each part from the property's schema: - an **object** property defaults to `application/json` - a **binary** string defaults to `application/octet-stream` - other **primitives** default to `text/plain` - an **array** takes its default from the type of its items That inference covers the common cases: a JSON metadata part and an opaque file part need nothing extra. ## The encoding object When the inference is wrong or insufficient, the Media Type Object's `encoding` map takes over. It is keyed by **property name** — the keys must exist in the schema's `properties` — and each value may set: - **`contentType`** — the part's media type, overriding the inferred default. This is how you say the `avatar` part must be `image/png` or `image/jpeg` rather than generic octets. - **`headers`** — a map of additional headers for that part (`Content-Disposition` and `Content-Type` are excluded, being determined by the multipart rules and `contentType`). - **`style`**, **`explode`**, **`allowReserved`** — these mirror the query-parameter serialization keywords, and they apply when the body is `application/x-www-form-urlencoded`, not to multipart parts. Seeing them on a multipart encoding entry is a smell. ## Binary content, 3.0 versus 3.1 This is the version-sensitive part of the answer. In **OpenAPI 3.0** binary payloads are written as `type: string` with `format: binary` (and `format: byte` for base64-encoded text). In **OpenAPI 3.1** the Schema Object is full JSON Schema 2020-12, which has its own vocabulary for this: `contentMediaType` and `contentEncoding` describe the content of a string, and the 3.0 `format: binary` convention is superseded. 3.1 also lets a media type entry omit `schema` altogether, meaning the body is simply that media type's bytes. A candidate who states one spelling without noticing it is version-bound has missed the part of the question that interviewers are probing, because a 3.0 document run through 3.1 tooling is exactly where uploads break. ## Multiple files under one field An array-typed property means the part repeats: a schema property `files` of `type: array` with binary items produces several parts all named `files`. This is the standard way to describe a multi-file upload, and it is worth calling out because generated clients differ in whether they expose it as a list argument or as repeated single-file calls. ## What the declaration does and does not do The document describes the wire format so that generators, mock servers, request validators and documentation renderers agree. It does not enforce anything at runtime — a size limit, an allowed-extension list or a virus scan are server concerns, not spec fields. You can *document* a size limit in `description`, and some teams add `maxLength` on the schema, but no consumer of the document is obliged to enforce it. Interviewers listen for this distinction: a candidate who claims the spec restricts upload size has confused description with enforcement. ## Practical checklist - One `multipart/form-data` entry under `content`, object schema, one property per part. - `required` on the schema for the parts that must be present. - `encoding.<property>.contentType` wherever the inferred type is too loose to be useful to a client. - The binary spelling that matches your document's OpenAPI version, stated consistently across the whole document. - Array properties for repeated parts. - Remember the operation almost always needs `requestBody.required: true` as well — it defaults to `false`.
- What content type does a multipart part get if you declare no encoding entry for it?OpenAPI infers it from the property's schema: an object property defaults to `application/json`, a binary string to `application/octet-stream`, other primitives to `text/plain`, and an array takes the default of its item type. The `encoding` map exists to override that inference — most commonly to narrow a file part from generic octets to a specific image type.
- How do you describe uploading several files under the same field name?Make the property an array whose items are the binary type. Each element becomes a separate part carrying the same name, which is what a browser sends for a multiple-file input. Generated clients typically surface it as a list argument, though how each generator names and types that argument varies, so it is worth checking against the client you ship.
- Can an OpenAPI document enforce a maximum upload size?No. The document describes the request; enforcement is the server's job. You can note a limit in `description` or add `maxLength` on the schema, but no consumer is obliged to act on it, and a request validator that does will not protect the service from a client that ignores the spec. Size limits, extension allow-lists and scanning belong in the server and its gateway.
saying these in an interview costs you the question
- Describes file uploads as parameters rather than a request body
- Uses format: binary in a 3.1 document without noticing the change
- Puts style and explode on multipart encoding entries
- Thinks the spec enforces upload size or file type
- Forgets requestBody required defaults to false