8i | 9i | 10g | 11g | 12c | 13c | 18c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Misc » Here

Oracle REST Data Services (ORDS) : Open API 2.0 (Swagger) Support

The Open API Initiative (OAI) provides an open specification, originally know as the Swagger specification, for describing and documenting REST APIs. From version 17.4 onward, Oracle REST Data Services (ORDS) exposes the metadata of specific web services in Open API 2.0 format, making it easy to document and generate calling code for REST APIs using Swagger.

Related articles.

Setup

The examples in this article are based the following.

Metadata Catalog

Using ORDS 17.4 onward the top-level metadata-catalog output includes both the original metadata-catalog and open-api-catalog URLs of all relevant objects in an ORDS enabled schema. The metadata-catalog output for a specific object is presented in the original format, while the open-api-catalog output is in Open API (Swagger) 2.0 format, which makes it really simple to generate documentation and an example of the calling code in several programming languages.

An overview of the contents of an ORDS enabled schema can be displayed in a browser (GET HTTP method) using the following type of URL. The output is a JSON document.

Both URL types listed.
Format  : http://server:port/ords/<connection>/<schema-alias>/metadata-catalog/
Example : http://ol7-122.localdomain:8080/ords/pdb1/testuser1/metadata-catalog/

Open API URLs listed
Format  : http://server:port/ords/<connection>/<schema-alias>/open-api-catalog/
Example : http://ol7-122.localdomain:8080/ords/pdb1/testuser1/open-api-catalog/

The open-api-catalog output for a specific service is displayed in the Open API 2.0 format.

Open API Format
Format  : http://server:port/ords/<connection>/<schema-alias>/open-api-catalog/<object-alias>/
Example : http://ol7-122.localdomain:8080/ords/pdb1/testuser1/open-api-catalog/testmodule3/

Example 1 : Manually Created RESTful Service

We can use a browser, REST client or the curl command to check the metadata for the testmodule3 module using the object alias associated with the module, also testmodule3 in this case. Here is an example using the curl command.

$ curl "http://ol7-122.localdomain:8080/ords/pdb1/testuser1/open-api-catalog/testmodule3/"

{"swagger":"2.0","info":{"title":"ORDS generated API for testmodule3","version":"1.0.0"},"host":"ol7-122.localdomain:8080",
"basePath":"/ords/pdb1/testuser1/testmodule3","schemes":["http"],"produces":["application/json"],
"paths":{"/emp/":{"get":{"produces":["application/json"],"responses":{"200":{"description":"output of the endpoint",
"schema":{"type":"object","properties":{}}}}}},"/emp/{empno}":{"get":{"produces":["application/json"],
"responses":{"200":{"description":"output of the endpoint","schema":{"type":"object","properties":{}}}},
"parameters":[{"name":"empno","in":"path","required":true,"type":"string","description":"implicit","pattern":"^[^/]+$"}]}}}}

If we go to the online Swagger Editor, we can paste in the output JSON text, which converts to YAML and displays the available endpoints.

Swagger Editor - Description 1

The "Generate Client" menu at the top of the screen allows us to create client code to call this service from a number of different languages.

Swagger Editor - Client Code

The sample code is downloaded as a zip file.

Example 2 : AutoREST Service

We can display the metadata for the table EMP, which is AutoREST enabled, using the following curl command.

$ curl "http://ol7-122.localdomain:8080/ords/pdb1/testuser1/open-api-catalog/employees/"

{"swagger":"2.0","info":{"title":"ORDS generated API for EMP","version":"1.0.0"},
"host":"ol7-122.localdomain:8080","basePath":"/ords/pdb1/testuser1/employees",
"schemes":["http"],"produces":["application/json"],"paths":{"/":{"get":{"produces":["application/json"],
"responses":{"200":{"description":"output of the endpoint","schema":{"type":"object","properties":{}}}}},
"post":{"produces":["application/json"],"responses":{"200":{"description":"output of the endpoint",
"schema":{"type":"object","properties":{}}}},"parameters":[{"name":"payload","in":"body","required":true,
"schema":{"$ref":"#/definitions/PAYLOAD1"}}]}},"/{id}":{"get":{"produces":["application/json"],
"responses":{"200":{"description":"output of the endpoint","schema":{"type":"object","properties":{}}}},
"parameters":[{"name":"id","in":"path","required":true,"type":"string","description":"implicit",
"pattern":"^[^/]+$"}]},"put":{"produces":["application/json"],"responses":{"200":{"description":"output of the endpoint",
"schema":{"type":"object","properties":{}}}},"parameters":[{"name":"id","in":"path","required":true,"type":"string",
"description":"implicit","pattern":"^[^/]+$"},{"name":"payload","in":"body","required":true,
"schema":{"$ref":"#/definitions/PAYLOAD1"}}]},"delete":{"produces":["application/json"],
"responses":{"200":{"description":"output of the endpoint","schema":{"type":"object","properties":{}}}},
"parameters":[{"name":"id","in":"path","required":true,"type":"string","description":"implicit",
"pattern":"^[^/]+$"}]}}},"definitions":{"DATE":{"type":"string",
"pattern":"^\\d{4}-[01]\\d-[0123]\\dT[012]\\d:[0-5]\\d:[0-5]\\d(.\\d+)?(Z|([-+][012]\\d:[0-5]\\d))$"},
"NUMBER":{"type":"number"},"VARCHAR2":{"type":"string"},"PAYLOAD1":{"properties":{"EMPNO":{"$ref":"#/definitions/NUMBER"},
"ENAME":{"$ref":"#/definitions/VARCHAR2"},"JOB":{"$ref":"#/definitions/VARCHAR2"},"MGR":{"$ref":"#/definitions/NUMBER"},
"HIREDATE":{"$ref":"#/definitions/DATE"},"SAL":{"$ref":"#/definitions/NUMBER"},"COMM":{"$ref":"#/definitions/NUMBER"},
"DEPTNO":{"$ref":"#/definitions/NUMBER"}}}}}

After pasting the output into the Swagger Editor we see a larger number of endpoints, as well as a payload definition for those endpoints that require it.

Swagger Editor - Description 2

As before, sample calling code for this service can be generated from the "Generate Client" menu.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.