OpenAPI 2.0 has a type: "file" which describes part of a request/response with a multipart/form-data MIME type. OpenAPI 3.0 does not have that type anymore.
Details of how typefile works
The OpenAPI 2.0 specification is vague so I went through several GitHub issues/PRs of the specification to figure out the following:
only available for a request parameter with "in": "formData" or for the response body
the multipart/form-datafilename property must be specified
in a formData request parameter with multipart/form-data MIME type, parameter.name describes the multipart/form-dataname property.
in a response body, the value of the name property is unspecified. This means it is unspecified which file in the response body is targeted (since multipart/form-data can contain several files), unless there is only one file.
in a response body with typefile, the following JSON schema keywords are not allowed: default, title, readOnly, required, format, description, externalDocs, example
request parameters with "in": "formData" have MIME types multipart/form-data or x-www-form-urlencoded, whether of not "type": "file" is used. Response body can also have those MIME types. The consumes and produces properties can also be used. This means the only real reason to use "type": "file" is to specify the filename property (and communicate that the file is semantically meant to be downloaded/saved).
In that example, it is unspecified whether the schema target myContent or myOtherContent.
Does typefile makes sense with JSON schemas?
I think the typefile does not imply anything about the content being JSON or not JSON.
The content of a request body or response body could be JSON. It could also be treated as a string where we want to validate the maxLength or pattern.
On the other hand, a formData or body request parameter or a response body with typestring or with no type can be non-JSON.
Whether the content is JSON compatible depends on the MIME types, which are described by consumes and produces (OpenAPI 2.0) or the Accept and Content-Type request headers (OpenAPI 3.0). Even then a non-JSON compatible request body or response body might be parsed to a JSON compatible value.
So I think the content of a request body or response body with typefile might still be described by a JSON schema.
Purpose of supporting "type": "file"
It seems to me that OpenAPI 2.0. created the "type": "file" solely to specify whether the filename property is present or not. Overloading the JSON schema type was probably a bad decision that they reverted in OpenAPI 3.0. However users might still want to use the other JSON schema properties to validate the targeted part of the request body or response body. In the example above users might still want to validate the file content against the maxProperties constraint.
The following proposals would allow users to re-use those JSON schemas as opposed to the current behavior of throwing when encountering that type.
Solution 1
Removing the type property.
Solution 2
Guessing the type property from the sibling properties. E.g. { "type": "file", "minProperties": 1, "minLength": 1 } would become { "type": ["object", "string"], "minProperties": 1, "minLength": 1 }. If the type cannot be guessed, it is removed.
This is a continuation of #9
OpenAPI 2.0 has a
type: "file"
which describes part of a request/response with amultipart/form-data
MIME type. OpenAPI 3.0 does not have thattype
anymore.Details of how
type
file
works The OpenAPI 2.0 specification is vague so I went through several GitHub issues/PRs of the specification to figure out the following:"in": "formData"
or for the response bodymultipart/form-data
filename
property must be specifiedformData
request parameter withmultipart/form-data
MIME type,parameter.name
describes themultipart/form-data
name
property.name
property is unspecified. This means it is unspecified which file in the response body is targeted (sincemultipart/form-data
can contain several files), unless there is only one file.type
file
, the following JSON schema keywords are not allowed:default
,title
,readOnly
,required
,format
,description
,externalDocs
,example
"in": "formData"
have MIME typesmultipart/form-data
orx-www-form-urlencoded
, whether of not"type": "file"
is used. Response body can also have those MIME types. Theconsumes
andproduces
properties can also be used. This means the only real reason to use"type": "file"
is to specify thefilename
property (and communicate that the file is semantically meant to be downloaded/saved).Example 1
describes the following request body
multipart/form-data; boundary=%%%
:Example 2
describes the following request body
multipart/form-data; boundary=%%%
:Example 3
could describe the following response body
multipart/form-data; boundary=%%%
:In that example, it is unspecified whether the schema target
myContent
ormyOtherContent
.Does
type
file
makes sense with JSON schemas?I think the
type
file
does not imply anything about the content being JSON or not JSON.The content of a request body or response body could be JSON. It could also be treated as a string where we want to validate the
maxLength
orpattern
.On the other hand, a
formData
orbody
request parameter or a response body withtype
string
or with notype
can be non-JSON.Whether the content is JSON compatible depends on the MIME types, which are described by
consumes
andproduces
(OpenAPI 2.0) or theAccept
andContent-Type
request headers (OpenAPI 3.0). Even then a non-JSON compatible request body or response body might be parsed to a JSON compatible value.So I think the content of a request body or response body with
type
file
might still be described by a JSON schema.Purpose of supporting
"type": "file"
It seems to me that OpenAPI 2.0. created the
"type": "file"
solely to specify whether thefilename
property is present or not. Overloading the JSON schematype
was probably a bad decision that they reverted in OpenAPI 3.0. However users might still want to use the other JSON schema properties to validate the targeted part of the request body or response body. In the example above users might still want to validate the file content against themaxProperties
constraint.The following proposals would allow users to re-use those JSON schemas as opposed to the current behavior of throwing when encountering that type.
Solution 1
Removing the
type
property.Solution 2
Guessing the
type
property from the sibling properties. E.g.{ "type": "file", "minProperties": 1, "minLength": 1 }
would become{ "type": ["object", "string"], "minProperties": 1, "minLength": 1 }
. If thetype
cannot be guessed, it is removed.I can submit a PR if needed.