Creating reusable array definitions in swagger - arrays

I'm working on a definition/documentation-first specification of an API using Swagger 2.0. I've managed to break-out most reusable components into a definitions section however I am having trouble figuring out how to create reusable definitions for arrays of constants.
For example, I have a few paths that will return images, like this one:
paths:
/resource/{imageId}:
get:
produces:
- image/jpeg
- image/png
- image/gif
parameters:
- in: path
name: imageId
type: string
required: true
responses:
200:
description: Success
schema:
type: file
Which works just fine, but I would like to be able to define a reusable array of the values for the "produces" element so that I can reuse the same list for any path that will produce an image.
The following seemed like the intuitive approach, but swagger reports that the definition for imageMimeTypes is not valid:
paths:
/resource/{imageId}:
get:
produces:
$ref: "#/definitions/imageMimeTypes"
parameters:
- in: path
name: imageId
type: string
required: true
responses:
200:
description: Success
schema:
type: file
definitions:
imageMimeTypes:
- image/jpeg
- image/png
- image/gif
Is it possible to create a definition for an array like this? If so, what syntax should be used?

First of all, if these produces values are used in most operations, you can define them as global produces and override where needed.
produces:
- image/jpeg
- image/png
- image/gif
paths:
/resource/{imageId}:
get:
# Inherits global "produces"
...
/something:
get:
# Overrides global "produces"
produces:
- application/json
...
Your second example is not valid, because produces cannot have a $ref value. But you can use YAML anchors to achieve a similar effect. Note that an anchor must be defined before it is used, so you need to put the list above the path definition.
x-types:
imageMimeTypes: &IMAGE-MIME-TYPES
- image/jpeg
- image/png
- image/gif
paths:
/resource/{imageId}:
get:
produces: *IMAGE-MIME-TYPES
parameters:
- in: path
name: imageId
type: string
required: true
responses:
200:
description: Success
schema:
type: file
I put the list under an extension key x-types instead of definitions 1) because definitions is intended for input and output models and not random lists, and 2) to prevent the "unused definition" warning in the Swagger Editor.
This works in (at least) Swagger Editor and Swagger UI.

Related

How can I validate an XML with String arrays using a RAML DataType?

I'm having an issue where a simple string array or integer array creates a warning and prevents the validation of XMLs within my RAML API specification in Anypoint Platform.
These are the files I'm currently using:
customerNames.raml:
#%RAML 1.0 DataType
displayName: customerNames
description: Full detail view on a customerNames array
additionalProperties: false
properties:
customerName:
type: array
items: string
customerNames.xml:
<?xml version="1.0" encoding="UTF-8"?>
<customerNames>
<customerName>John Doe</customerName>
<customerName>Jane Doe</customerName>
</customerNames>
Validation results in the following warning:
validation error
I've tried other notations as well:
#%RAML 1.0 DataType
displayName: customerNames
description: Full detail view on a customerNames array
additionalProperties: false
properties:
customerName:
type: string[]
But to no avail.
When I try to recreate this issue using JSON files, both notations do not yield the warning and validation can happen as expected.
Is this an Anypoint Platform bug or is my syntax incorrect?

Model form-data body request using Swagger 2.0 in yaml file

I am using swagger 2.0 and I have an endpoint which is using form-data as the payload for the request, I am using the same form-data body request in several places and I don't want to write it again and again (duplicate it) and I don't know how to model it inside my yaml file (I do know how to model object when it is part of the request body as payload).
This is the form-data payload I am trying to model:
I looked at swagger's documentation: https://swagger.io/docs/specification/2-0/file-upload/
and tried to do the same but I got errors as follow:
I don't want to duplicate the same values (if I do so it will work but the solution won't be elegant).
In OpenAPI 2.0, you can only reuse the definitions of individual form fields/parameters. There's no way to reuse a set of fields/parameters.
swagger: '2.0'
parameters:
name:
in: formData
name: name
type: string
upfile1:
in: formData
name: upfile1
type: file
...
paths:
/foo:
post:
consumes:
- multipart/form-data
parameters:
- $ref: '#/parameters/name'
- $ref: '#/parameters/upfile1'
- ...
/bar:
post:
consumes:
- multipart/form-data
parameters:
- $ref: '#/parameters/name'
- $ref: '#/parameters/upfile1'
- ...
To reuse the entire request body definition, you need OpenAPI 3.0. In OAS 3, request bodies can be defined in the global components/requestBodies section and referenced using $ref:
openapi: 3.0.0
paths:
/foo:
post:
requestBody:
$ref: '#/components/requestBodies/statusMode'
responses:
...
/bar:
post:
requestBody:
$ref: '#/components/requestBodies/statusMode'
responses:
...
components:
requestBodies:
statusMode:
required: true
content:
multipart/form-data:
schema:
type: object
# The properties correspond to individual form fields
properties:
name:
type: string
upfile1:
type: string
format: binary

Unable to parse Open API, or Google Service Configuration specification from openapiapp.yaml

I try to follow exactly [this tutorial] (https://cloud.google.com/community/tutorials/exposing-aspnet-webapi-using-dotnetcore-with-cloud-endpoints ), but I get the following error at trying gcloud endpoints services deploy openapi.yaml :
ERROR: (gcloud.endpoints.services.deploy) Unable to parse Open API, or Google Service Configuration specification from [SampleSolution]
The body of openapi.yaml :
openapi: 3.0.1
info:
title: Notes API
version: v1
host: [google cloud project ID].appspot.com
paths:
/WeatherForecast:
get:
tags:
- WeatherForecast
responses:
'200':
description: Success
content:
text/plain:
schema:
type: array
items:
$ref: '#/components/schemas/WeatherForecast'
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/WeatherForecast'
text/json:
schema:
type: array
items:
$ref: '#/components/schemas/WeatherForecast'
components:
schemas:
WeatherForecast:
type: object
properties:
date:
type: string
format: date-time
temperatureC:
type: integer
format: int32
temperatureF:
type: integer
format: int32
readOnly: true
summary:
type: string
nullable: true
additionalProperties: false
I only see two things there:
openapi: 3.0.1. This should be swagger: "2.0" accordint ot the Basic structure of an OpenAPI document
host: [google cloud project ID].appspot.com. This should contain the proper project Id.
UPDATE
After checking the composition of your file and the Endpoints openAPI Docs:
OpenAPI feature limitations:
Currently, Cloud Endpoints accepts only version 2 of the OpenAPI Specification.
The Components Section in the Swagger docs mentions that applies to openAPI3. This is not compatible with point 1.
I suggest to remake your file following The basic structure of an OpenAPI document
Swashbuckle.AspNetCore now supports OpenApi 3, but it also has backwards compatibility with Swagger v2, by setting it up in Configure method (Startup class) with:
app.UseSwagger(c =>
{
c.SerializeAsV2 = true;
});

RAML for multiple files as output

I am new to RAML 1.0 I want to mention multiple csv files as response.
My API will be creating multiple csv file for different entities.
I want to mention this multiple file in response in RAML. Can you suggest what should be response for multiple files in RAML.
The body in HTTP requests and reponses is formed of one payload. You should use one zip, or a tar, tar.gz, etc, file to compress the files inside one file, or use a multi-part response to convey that each file is an attachement.
Example of a zip response:
#%RAML 1.0
title: test-attachment
/resource:
post:
responses:
200:
body:
application/zip:
Example of multi-part response:
#%RAML 1.0
title: test-attachment
/resource:
post:
responses:
200:
body:
multipart/form-data:
properties:
file1:
type: file
fileTypes: ['application/csv']
file2:
type: file
fileTypes: ['application/csv']

swagger editor does not generate correct request for file upload

I am using Swagger editor to build a post request to upload a file. My swagger json file is defined as follows.
swagger: '2.0'
info:
version: 0.0.0
title: '<enter your title>'
host: 'localhost:11235'
paths:
/form/upload:
post:
description: |
Post files.
consumes:
- multipart/form-data
produces:
- application/octet-stream
parameters:
- name: file
in: formData
description: upload file
required: true
type: file
- name: text
description: additional info
in: formData
type: string
responses:
'200':
description: Successful response
schema:
type: file
Then on the right side of the editor. I click the Try this operation. I select a file. The constructed request looks like this.
POST http://localhost:11235/form/upload HTTP/1.1
....
Content-Length: 40
Content-Type: multipart/form-data
file: C:\fakepath\swagger.json
text: 123
The file content is not transferred in the request. Only the file name is written into the request. Is this because swagger editor does not support file upload or my swagger json is wrong? Could anyone kindly help me with this? Thanks.
There is still an open issue for swagger-editor:
https://github.com/swagger-api/swagger-editor/issues/599
as reported it has been fixed in swagger-api:
https://github.com/swagger-api/swagger-ui/issues/838
Use swagger-ui for file uploading, it is working:
- in: formData
name: document
description: Documento Fiscal
required: true
type: file

Resources