This sample demonstrates:
- How to use OpenAPI as a source of API Gateway configuration
- How to validate requests and responses against OpenAPI definitions
- How to publish a Swagger UI for deployed APIs
Make sure to use Membrane version 5 or newer.
- Go to the examples/openapi/openapi-proxy directory and start Membrane:
./service-proxy.sh
or:
service.proxy.bat
- You can curl now for a JSON description of deployed APIs from OpenAPI.
curl localhost:2000/api-docs
{
"fruitshop-v1-1" : {
"openapi": "3.0.2",
"title" : "Fruit Shop API",
"version": "1.1",
"openapi_link": "/api-docs/fruitshop-v1-1",
"ui_link": "/api-docs/ui/fruitshop-v1-1"
}
}
Open the same URL http://localhost:2000/api-doc in the browser, and you get a UI.
- Click on Fruit Shop API on the left to open the Swagger UI of the API.
- Click on POST /products to expand it.
-
Next click on Try it out and then on Execute. The call should succeed and create a new product.
-
Change the price to a negative number like -2.7 and execute the call again.
{
"name": "Figs",
"price": -2.7
}
The validator checks the request against the OpenAPI definition. Cause the value for price is invalid a 400 Bad Request status code and an error message is returned.
{
"method": "POST",
"uriTemplate": "/products",
"path": "/shop/v2/products",
"validationErrors": {
"REQUEST/BODY#/price": [
{
"message": "-2.79 is smaller than the minimum of 0",
"complexType": "Product",
"schemaType": "number"
}
]
}
}
-
Try also a value for name that is longer than 30 characters.
-
Have a look at the configuration in the
proxies.xml
file:
<api port="2000">
<openapi location="fruitshop-api.yml"
validateRequests="yes"
validateResponses="yes"
validationDetails="yes"/>
</api>
For more about the OpenAPI Validation features of Membrane have a look at:
See:
- openapi reference