BoxLang 🚀 A New JVM Dynamic Language Learn More...
This is an extension for Preside that generates a Swagger specification and provides an API dashboard for the in-built REST architecture. For details on the REST framework implementation see here: Preside REST Framework Documentation
The extension provides a system configuration screen that allows you to configure:
Make sure that the hostname and whether to use HTTPS or not is set correctly. Otherwise the Swagger UI will not work.
Two new handlers are available that are publicly accessible:
In addition a new admin sidebar menu item named Swagger is available with quicklinks and an integrated UI Dashboard.
The Specification is generated based on component metadata from RESTful handlers. Have a look here how to create REST API Endpoints in Preside.
The following attributes (Standard CF as well as custom ones) are evaluated:
Swagger Responses in more detail:
You are able to define multiple responses for a single endpoint by using the custom cffunction attribute swagger_responses like this:
// using a semicolon as the delimiter
RESPONSE_1;RESPONSE_2;RESPONSE_3
A single response has the following format:
// use a colon as the delimiter
CODE:DESCRIPTION: HEADERS
The following are all valid examples that could be used:
200:pet created:X-NEW-ID;400:pet already existing
200:pet collection
200;404:pet not found
It is automatically determined whether a given argument is part of the request uri - a token. For all other arguments the evaluation follows some rules:
The following CFML > Swagger Mappings are used:
In case no custom swagger format is defined for some types specific default formats are used:
The following valid swagger types are supported (via swagger_type annotation on cfargument):
The following valid swagger formats can be used in combination with a valid swagger type (via swagger_format annotation on cfargument):
The following shows a full example of how a RESTful API Handler could look like:
/**
* @restUri /pets/{id}/
*/
component {
property name="petDao" inject="presidecms:object: pet";
/**
* @hint This endpoint returns a single pet
* @swagger_tags pets
* @swagger_summary return a pet
* @swagger_responses 200:single pet;404:pet not found
* @id.hint the object id of the pet object
*/
private void function get(required uuid id) {
var result = petDao.selectData(id=arguments.id);
if (result.recordCount eq 0) {
restResponse.noData().setStatus(404);
return;
}
restResponse.setData(queryRowData(result, 1));
}
/**
* @hint This endpoint updates a pet
* @swagger_tags pets
* @swagger_summary update a pet
* @swagger_responses 200:pet updated
* @id.hint the object id of the pet object
* @name.hint the name of the pet
*/
private void function put(required uuid id, required string name) {
petDao.updateData(
id=arguments.id,
data={
name=arguments.name
}
);
restResponse.noData();
}
/**
* @hint This endpoint is used to delete a pet
* @swagger_tags pets
* @swagger_summary delete a pet
* @swagger_responses 200:pet deleted
* @id.hint the object id of the pet object
*/
private void function delete(required uuid id) {
petDao.deleteData(id=arguments.id);
restResponse.noData();
}
}
Install the extension to your application via either of the methods detailed below (Git submodule / CommandBox) and then enable the extension by opening up the Preside developer console and entering:
extension enable preside-ext-rest-swagger
reload all
From the root of your application, type the following command:
git submodule add https://bitbucket.org/hwsdev/preside-ext-rest-swagger.git application/extensions/preside-ext-rest-swagger
From the root of your application, type the following command:
box install preside-ext-rest-swagger
This Extensions makes use of the Swagger UI code which can be found here: https://github.com/swagger-api/swagger-ui
$
box install preside-ext-rest-swagger