How do I describe schema object with value? - swagger

This is what I want
<messages>
<message from="aaa" date="2019-07-09T13:41:12Z">sampletext1</message>
<message from="bbb" date="2019-07-09T14:26:36Z">sampletext2</message>
</messages>
This is what I tried
messages:
type: "object"
properties:
message:
type: "array"
xml:
name: "message"
items:
$ref: "#/components/schemas/message"
xml:
name: "messages"
message:
type: "object"
xml:
name: "message"
properties:
from:
type: "string"
date:
type: "string"
format: "date-time"
value:
type: "string"
example: "sample text"
But this represents the following.
<messages>
<message>
<from>string</from>
<date>2019-07-09T19:47:47.264Z</date>
<value>sample text</value>
</message>
</messages>
How do I get the properties "from" and "date" in the "message" tag, and represent the sample text between the message tags as a value with no "value" tags with openapi 3.0.2?

Related

Swagger - discriminator: resouce_type does not show in UI

I'm using discriminator: resouce_type in my YAML for some of the definitions. They are not showing on UI. Can someone please help, is it supported in swagger UI or Am I doing something wrong..
Swagger/OpenAPI definition:
definitions:
myModel:
allOf:
- $ref: '#/definitions/ResponseWrapper'
- type: object
type: object
properties:
field1:
type: number
format: string
description: "My field 1"
field2:
type: string
description: "My field 2"
ResponseWrapper:
allOf:
- type: object
discriminator: resouce_type
Swagger-UI configuration options:
SwaggerUI({
"library": "spring-mvc",
"dateLibrary": "legacy",
"hideGenerationTimestamp": true,
"modelPackage": "com.example",
"apiPackage": "com.example",
"useTags": true,
"importMappings": {
"ResponseWrapper": "com.example.ResponseWrapper"
}
}
)
Can swagger read those classes from classpath and resolve and show in UI.?

$ref in response example

The response example I would like to put in the yaml file is something like:
{
"name": "Element",
"schema": {
"properties": {
"id": {
"type": "string"
},
"extension": {
"type": "array",
"items": {
"$ref": "#/definitions/Extension"
}
}
}
}
}
And the ymal of this example will be:
responses:
200:
description: "successful operation, return the definition of the resource type in the body"
examples:
application/json:
name: Element
schema:
properties:
id:
type: string
extension:
type: array
items:
$ref: '#/definitions/Extension'
As you can see the last line is "$ref: '#/definitions/Extension'", thus Swagger thinks it is a reference which it can not find anywhere in the ymal file.
Is it possible to escape this to get way from being a reference?
This is a bug in Swagger Editor and UI.
As a workaround, define a schema for your response and use a schema example instead. Schema examples are displayed fine in Swagger Editor 3.6.6 and Swagger UI 3.17.5. You might still see a $ref resolution error in the editor, but at least the example is displayed correctly.
responses:
200:
description: "successful operation, return the definition of the resource type in the body"
schema:
$ref: '#/definitions/MyResponseSchema'
definitions:
MyResponseSchema:
type: object
properties:
name:
type: string
schema:
type: object
properties:
...
example: # <------------
name: Element
schema:
properties:
id:
type: string
extension:
type: array
items:
$ref: '#/definitions/Extension'

swagger 3/OAS 3 submit file data along with regular data mutlipart request

I have the following swagger code addCustomer for sending a request that includes an image along with other json data. When I run code from swagger hub; it just sends json. Am I defining this right? Shouldn't it send multipart request?
post:
tags:
- customers
summary: Add a new customer to the store
description: ''
operationId: addCustomer
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/NewCustomerWithImageUrl'
application/xml:
schema:
$ref: '#/components/schemas/NewCustomerWithImageUrl'
multipart/form-data:
schema:
$ref: '#/components/schemas/NewCustomerWithImage'
description: Customer object that needs to be added to the store
required: true
NewCustomerWithImage:
type: object
allOf:
- $ref: '#/components/schemas/NewCustomer'
properties:
Image:
type: string
format: binary
Person:
type: object
required:
- first_name
properties:
first_name:
type: string
example: John
last_name:
type: string
example: Doe
email:
type: string
example: john#example.com
phone_number:
type: string
example: 555-555-5555
address_1:
type: string
example: 123 Nowhere Street
address_2:
type: string
example: Apartment 123
city:
type: string
example: Rochester
state:
type: string
example: New York
zip:
type: string
example: '14445'
country:
type: string
example: United States
comments:
type: string
example: A great customer
custom_fields:
type: object
minProperties: 0
maxProperties: 10
additionalProperties:
type: string
example:
secondary phone number: '555-555-5555'
NewCustomer:
type: object
allOf:
- $ref: '#/components/schemas/Person'
properties:
company_name:
type: string
example: PHP Point Of Sale
tier_id:
type: integer
format: uuid
example: 0
account_number:
type: string
example: '3333'
taxable:
type: boolean
example: false
tax_certificate:
type: string
example: '1234'
override_default_tax:
type: boolean
example: false
tax_class_id:
type: integer
format: uuid
example: 0
balance:
type: number
format: float
example: 22.99
credit_limit:
example: null
points:
type: integer
format: int32
example: 333
disable_loyalty:
type: boolean
example: true
amount_to_spend_for_next_point:
type: number
format: float
readOnly: true
example: 10.00
remaining_sales_before_discount:
type: integer
format: int32
readOnly: true
example: 0
xml:
name: Customer
Json sent:
{"first_name":"John","last_name":"Doe","email":"john#example.com","phone_number":"555-555-5555","address_1":"123 Nowhere Street","address_2":"Apartment 123","city":"Rochester","state":"New York","zip":"14445","country":"United States","comments":"A great customer","custom_fields":{"secondary phone number":"555-555-5555"},"company_name":"PHP Point Of Sale","tier_id":0,"account_number":"3333","taxable":false,"tax_certificate":"1234","override_default_tax":false,"tax_class_id":0,"balance":22.99,"credit_limit":null,"points":333,"disable_loyalty":true,"Image":"string"}
Swagger Hub File:
https://app.swaggerhub.com/apis/PHP-Point-Of-Sale/PHP-Point-Of-Sale/1.0#/customers/addCustomer
1) Swagger UI (which SwaggerHub uses) does not yet support form data and file upload for OpenAPI 3.0 definitions. Follow this issue for status updates: https://github.com/swagger-api/swagger-ui/issues/3641 UPD: Swagger UI now supports file uploads for OAS3.
2) If "request that includes an image along with other JSON data" means a multipart request like this –
POST /foo HTTP/1.1
Content-Type: multipart/form-data; boundary=ABCDEF123
--ABCDEF123
Content-Disposition: form-data; name="Customer"
Content-Type: application/json
{
"foo": "bar"
}
--ABCDEF123
Content-Disposition: form-data; name="Image"; filename="image1.png"
Content-Type: application/octet-steam
{…image content…}
--ABCDEF123--
then the request body definition should look like this:
requestBody:
content:
...
multipart/form-data:
schema:
type: object
properties:
Customer: # <--- name of the part in a multipart request
$ref: '#/components/schemas/NewCustomer'
Image: # <--- name of the part in a multipart request
type: string
format: binary
required:
- Customer
- Image
More information:
How to describe a multipart response using OpenAPI (Swagger)? here on SO
Special Considerations for multipart Content in the OpenAPI 3.0 Specification
Multipart Requests on swagger.io
File upload is supported in Open API v 3.0.3
Here's what my swagger.json looks like:
"/media/upload": {
"post": {
"tags": ["Media"],
"name": "Upload Media",
"description": "Uploads a Media file to the server.",
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"media": {
"type": "string",
"format": "base64"
}
}
}
}
}
}
}
}
Here's how it show up in swagger:

How to define a json output using swagger

I have a json payload and I need some help on how to represent it in swagger-YAML
keywords can be an empty array [] or and array like ['a','b','c']
Here is my object
{
"keywords": [],
"distance": "25",
"jobType": "all",
"sectorId": "0",
"location": {
"lat": "33.7489954",
"lng": "-84.3879824",
"name": "Atlanta, GA"
}
}
This is what I have so far but I am getting the error
SyntaxError: Data does not match any schemas from "oneOf"
Data path: "/paths/~1users~1{userId}~1jobdeckSearch/get/responses/200"
Schema path: "/properties/paths/patternProperties/^~1/properties/get/properties/responses/patternProperties/^([0-9]{3})$|^(default)$/oneOf"
responses:
200:
schema:
type: object
properties:
distance:
type: string
keywords:
type: array
items:
type: string
jobType:
type: string
sectorId:
type: string
location:
properties:
lat:
type: string
lng:
type: string
name:
type: string
Response codes require a description:
responses:
200:
description: OK
schema:
...
Also, remove the extra space character before lat and lng - they need to be indented at the same level as name.
Other than that, you definition is fine.

Swagger POST body list

I've been searching fruitlessly on how to swagger spec a POST body with a list inside of it. How do I do it? Here's what I have:
/groups:
post:
summary: Creates a group
parameters:
- name: body
in: body
schema:
properties:
name:
type: string
description:
type: string
groupType:
type: string
enum: [ "open", "closed", "secret" ]
users:
type: string list # <--------- a list of strings
responses:
201:
description: Group created
default:
description: Group creation failed
For property being an array of string, please refer to https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L660 as an example:
photoUrls:
type: array
items:
type: string

Resources