What is the reason of request body not matching the schema? - openapi

I'm using swagger to describe a simple endpoint to upload file with date(int64). There is request body:
put:
tags:
- files
description: Add an associated file to a project
operationId: addFile
parameters:
- name: projectId
in: path
required: true
schema:
type: string
format: uuid
requestBody:
content:
multipart/form-data:
schema:
$ref: "#/components/schemas/AddFileToProjectRequestBody"
required: true
responses:
200:
description: Successful
content: {}
and schema:
AddFileToProjectRequestBody:
required:
- file
type: object
properties:
file:
type: string
format: binary
description: The file to upload
fileDate:
type: integer
format: int64
minimum: 0
description: File modified date (Unix timestamp in milliseconds)
If I upload a simple file it works fine. But if I am trying to add a fileDate I get an error.
"request body has an error: doesn't match the schema: Error at "/fileDate": Field must be set to integer or not be present"
Looks like it fails to read number as int. Can someone explain to me why? I have tried to change it to string and it worked, but doesn't look logical to me to set date as string.
UPD:
------WebKitFormBoundaryGs2
Content-Disposition: form-data; name="fileDate"
1670485649000
------WebKitFormBoundaryGs2
Content-Disposition: form-data; name="projectFile"; filename="1.txt"
Content-Type: text/plain
------WebKitFormBoundaryGs2--

Related

Open API 3 - add headers on individual content-type in responses

I have my spec which have a path with a 200 response code, that response code can access multiple content-types, I want to add the Content-Disposition Header to one of those content-types.
Here's a sample:
openapi: '3.0.3'
info:
...
servers:
...
paths:
/examples:
...
get:
...
responses:
'200':
content:
application/json:
...
application/pdf:
encoding:
file:
headers:
Content-Disposition:
schema:
type: string
example: attachment; filename="name.pdf"
examples:
file:
summary: File
externalValue: https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf
Here's the generated view:
no header
Here is an example where the header is added (for another endpoint)
responses:
'201':
description: Success
headers:
Location:
schema:
type: string
format: uri
description: The URI to the newly created example
And here's the generated view for that one:
with header
Am I doing something wrong?
encoding.<name>.headers is used to define headers for individual parts of a multipart/* request body, which is different from your scenario. Since your response is not multipart/*, the response headers must be defined in responses.<code>.headers.
However, OpenAPI does not have a way to vary response headers per media type. What you can do is define the Content-Disposition response header as optional and explain that it only applies to applicatioln/pdf responses.
paths:
/examples:
get:
responses:
'200':
description: ok
content:
application/pdf:
schema:
type: string
format: binary
headers:
Content-Disposition:
schema:
type: string
description: Used only with `application/pdf` responses
example: attachment; filename="name.pdf"

How to vary requestBody type based on a path parameter in OpenAPI 3.0?

In OpenAPI 2.0, in: body and in: formData parameters cannot exist together for the same operation, according to Swagger send body and formData parameter and OpenAPI 2.0 Specification. That makes sense.
But I'm using OpenAPI 3.0 and I wonder if there is a way to switch requestBody according to a path parameter? When store is path1, requestBody should use content with application/json; when store is path2, requestBody should use content with multipart/form-data.
/customs/{store}:
post:
description: Customs server calls Nomad to receive the filing result of one
order
operationId: post_customs_callback
parameters:
- description: ID of the store.
explode: true
in: path
name: store
required: true
schema:
type: string
style: simple
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties: # Request parts
openReq:
type: string
# application/json:
# schema:
# type: string
description: Order details
OpenAPI Specification does not have a way to vary request/response body based on specific parameter values. However, in your scenario you can use two non-parameterized paths instead – /customs/path1 for JSON requests and /customs/path2 for multipart requests.
openapi: 3.0.0
...
paths:
/customs/path1:
post:
...
requestBody:
required: true
content:
application/json:
schema:
...
/customs/path2:
post:
...
requestBody:
required: true
content:
multipart/form-data:
schema:
...

Validate OpenAPI response with dredd

I have an OpenAPI v3 specification file with the following (showing just fragments):
paths:
/global/name:
get:
description: Some description
tags:
- Global settings
operationId: getGlobalSettingsName
responses:
# Response code
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/globalSettingsName'
components:
schemas:
globalSettingsName:
type: object
properties:
name:
type: integer
description: 'ID'
example: 1
required:
- name
but the server response is:
{
"name": "somestring"
}
Note the name property type is integer and in the server response, it is a string (on purpose) but dredd request passes (success).
Doesn't dredd check for response property types?
I redefined the response as string (not JSON):
responses:
# Response code
'200':
description: Successful response
content:
application/json:
schema:
type: string
and dredd doesn't complain about either.
I even changed the property of the schema:
globalSettingsName:
type: object
properties:
www:
type: string
description: 'some description'
example: 'somestring'
required:
- www
And same (success) result when it is expected to fail.
Aren't these validation supported by dredd? Am I using specification wrong?
It results that the current version (8.0.5) only supports example value in content: https://github.com/apiaryio/dredd/issues/1281

How to change query parameter name in swagger?

I am trying to get this url:
https://search.me/Search/api/search?map=%7B%22query%22:%22CSCI+250%22,%22rows%22:500,%22term%22:%222181%22,%22career%22:%22%22,%7D
without converting special characters to HEX to URL would be:
https://search.me/Search/api/search?map={query:CSCI-250,rows:500,term:2181,career:}
I know it is not possible to serialize special characters as hex in openapi 3.0.0, so I am fine with the full JSON in the query parameter.
Where the parameter is a JSON -> URI
The JSON is:
{
map: {
"query": "CSCI 250",
"rows": 500,
"term": 2181,
"career": ""
}
}
but swagger instead renders:
https://search.me/search/api/search?query=CSCI-250&rows=500&term=2181&career=
As you can see, special characters are then converted into HEX.
Below is my path. What exactly am I doing wrong?
paths:
/search:
get:
summary: Search
parameters:
- in: query
name: map
description: JSON for lookup
required: true
schema:
$ref: '#/components/schemas/QueryParams'
responses:
200:
description: Course search response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/QueryResults'
400:
description: Bad request

Swagger send body and formData parameter [duplicate]

This question already has answers here:
In Swagger, how to define an API that consumes a file along with a schema parameter?
(3 answers)
Closed 2 years ago.
I'm using Swagger 2.0 and I have a problem to send multiple post parameters. I have a swagger error Operation cannot have a body parameter and a formData parameter and I don't know how to fix it. In my definition I have a body parameter and this parameter need a JSON format but a side I have other parameter like files to upload and filename.
How can I do to send body and formData parameters both ?
Here is the web service definition :
/updateDatas:
post:
summary: Upadate datas
description: |
Update datas
consumes:
- multipart/form-data
produces:
- application/json
parameters:
- name: firstFileName
in: formData
description: First file name.
required: true
type: string
- name: secondFileName
in: formData
description: Second file name.
required: true
type: string
- name: datas
in: body
description: Json object informations.
required: true
schema:
$ref: '#/definitions/Datas'
- name: firstFile
in: formData
description: First file .jpg
required: true
type: file
- name: clientFile
in: formData
description: Second file .jpg
required: true
type: file
tags:
- Application
responses:
'200':
description: Uploaded
schema:
$ref: '#/definitions/Upload'
'401':
description: Unauthorized Bad Token
According to the swagger specifications see, type:body and type:formData cannot exist together for the same operation.
One way to resolve the problem is to set "datas" as form parameter with the type "file". Here is an example:
parameters:
- name: petId
in: path
description: ID of pet to update
required: true
type: integer
format: int64
- name: additionalMetadata
in: formData
description: Additional data to pass to server
required: false
type: string
- name: file
in: formData
description: file to upload
required: false
type: file
Ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L257
UPDATE: body parameters and form parameters cannot co-exist: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject
Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be one body parameter. The name of the body parameter has no effect on the parameter itself and is used for documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist together for the same operation.