Skip to content

Latest commit

 

History

History

openapi-proxy

OpenAPI Configuration Example

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

Running the Example

Make sure to use Membrane version 5 or newer.

  1. Go to the examples/openapi/openapi-proxy directory and start Membrane:
./service-proxy.sh

or:

service.proxy.bat
  1. 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.

API Overview

  1. Click on Fruit Shop API on the left to open the Swagger UI of the API.

Swagger UI

  1. Click on POST /products to expand it.

POST /products

  1. Next click on Try it out and then on Execute. The call should succeed and create a new product.

  2. 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"
      }
    ]
  }
}
  1. Try also a value for name that is longer than 30 characters.

  2. Have a look at the configuration in the proxies.xmlfile:

<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: