You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We want to verify if the implementation of the API complies with the contract established with the OpenAPI document.
In an OpenAPI document you can specify more than one response code with a different body in each case. It would be nice if we could test each one by making multiple requests for the same endpoint, one for each status code.
The problem that arises from this idea is how to trigger a different response code for the same endpoint.
We have thought about defining an additional file, with the intention of decoupling it from the original document. In this file, the final user specifies how to trigger a response code through a request. The format of this document will also follow the OpenAPI specification, using the “examples” field. This way, it will be easy to process the information in the workflow generation and we can use the Swagger validator to check if the document is well-formed.
Use example
The examples file could be specified by using a flag or as a second parameter of stepci generate:
The following OpenAPI document is the specification of the API that will be referred to in the subsequent examples.
openapi: 3.0.0info:
title: Example APIversion: 1.0.0servers:
- url: http://localhost:1234paths:
/example-headers:
get:
responses:
'200':
description: This is the response for the HTTP status code 200.content:
application/json:
schema:
$ref: '#/components/schemas/SuccessResponse''500':
description: This is the response for the HTTP status code 500.content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'/example-query:
get:
parameters:
- name: fooin: queryschema:
type: stringresponses:
'200':
description: This is the response for the HTTP status code 200.content:
application/json:
schema:
$ref: '#/components/schemas/SuccessResponse''500':
description: This is the response for the HTTP status code 500.content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'/example-request-body:
post:
requestBody:
content:
application/json:
schema:
type: objectproperties:
property1:
type: stringproperty2:
type: stringresponses:
'200':
description: This is the response for the HTTP status code 200.content:
application/json:
schema:
$ref: '#/components/schemas/SuccessResponse''500':
description: This is the response for the HTTP status code 500.content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'components:
schemas:
SuccessResponse:
type: objectproperties:
data:
type: stringErrorResponse:
type: objectproperties:
error:
type: integer
Examples document structure
Several options are provided for the user to specify how to trigger a specific HTTP response code through an HTTP request. All of them share the use of examples field.
Object Examples
The object key represents an HTTP response code, and the value is an object with the following properties:
summary(optional): Explanatory text.
value(required): Payload value to be sent. It must follow the same format as the associated schema, according to the OpenAPI specification:
*“In all cases, the example value is expected to be compatible with the type schema of its associated value. Tooling implementations MAY choose to validate compatibility automatically, and reject the example value(s) if incompatible.”*
header
The user specifies the header to be added to the request, with the goal of having the API interpret it and return the expected response code. The header name, its value, and interpretation are on the user's side.
In this example, Prefer: statusCode=200 and Prefer: statusCode=500 headers are used.
get:
parameters:
- name: Prefer in: headerschema:
type: stringexamples:
'200':
summary: Expects a 200 status code from the APIvalue: 'statusCode=200''500':
summary: Expects a 500 status code from the APIvalue: 'statusCode=500'
query
get:
parameters:
- name: foo in: queryschema:
type: stringexamples:
'200':
summary: This is a correct parameter examplevalue: bar'500':
summary: This is a wrong parameter examplevalue: xxx
post:
requestBody:
content:
application/json:
examples:
'200':
summary: This is a correct parameter examplevalue:
property1: 'foo'property2: 'bar''500':
summary: This is a wrong parameter examplevalue: 'xxx'
Complete example
openapi: 3.0.0info:
title: Example APIversion: 1.0.0paths:
/example-headers:
get:
parameters:
- name: Preferin: headerschema:
type: stringexamples:
'200':
summary: Expects a 200 status code from the APIvalue: 'statusCode=200''500':
summary: Expects a 500 status code from the APIvalue: 'statusCode=500'responses:
'200':
description: This is the response for the HTTP status code 200.'500':
description: This is the response for the HTTP status code 500./example-query:
get:
parameters:
- name: fooin: queryschema:
type: stringexamples:
'200':
summary: This is a correct parameter examplevalue: bar'500':
summary: This is a wrong parameter examplevalue: xxxresponses:
'200':
description: This is the response for the HTTP status code 200.'500':
description: This is the response for the HTTP status code 500./example-request-body:
post:
requestBody:
content:
'application/json':
examples:
'200':
summary: This is a correct parameter examplevalue:
property1: 'foo'property2: 'bar''500':
summary: This is a wrong parameter examplevalue: 'xxx'responses:
'200':
description: This is the response for the HTTP status code 200.'500':
description: This is the response for the HTTP status code 500.
The text was updated successfully, but these errors were encountered:
Proposal
Objective
We want to verify if the implementation of the API complies with the contract established with the OpenAPI document.
In an OpenAPI document you can specify more than one response code with a different body in each case. It would be nice if we could test each one by making multiple requests for the same endpoint, one for each status code.
The problem that arises from this idea is how to trigger a different response code for the same endpoint.
We have thought about defining an additional file, with the intention of decoupling it from the original document. In this file, the final user specifies how to trigger a response code through a request. The format of this document will also follow the OpenAPI specification, using the “examples” field. This way, it will be easy to process the information in the workflow generation and we can use the Swagger validator to check if the document is well-formed.
Use example
The examples file could be specified by using a flag or as a second parameter of
stepci generate
:OpenAPI contract (starter point)
The following OpenAPI document is the specification of the API that will be referred to in the subsequent examples.
Examples document structure
Several options are provided for the user to specify how to trigger a specific HTTP response code through an HTTP request. All of them share the use of examples field.
Object Examples
The object key represents an HTTP response code, and the value is an object with the following properties:
summary (optional): Explanatory text.
value (required): Payload value to be sent. It must follow the same format as the associated schema, according to the OpenAPI specification:
https://spec.openapis.org/oas/v3.1.0#example-object
Placement
parameters (https://spec.openapis.org/oas/v3.1.0#parameter-object)
header
The user specifies the header to be added to the request, with the goal of having the API interpret it and return the expected response code. The header name, its value, and interpretation are on the user's side.
In this example,
Prefer: statusCode=200
andPrefer: statusCode=500
headers are used.query
requestBody (https://spec.openapis.org/oas/v3.1.0#request-body-object)
Complete example
The text was updated successfully, but these errors were encountered: