Custom API Documentation

From RAD Studio
Jump to: navigation, search

Go Up to Extending the EMS Server‎


RAD Studio allows you to create API documentation for new EMS Resource modules in YAML and JSON format. The new implementation is based in the Swagger RESTful API Documentation Specification.

EMS.ResourceType implements the new attributes that can be used to generate API documentation for the EndPoints of a Resource.

EndPointRequestSummary

Description of a method:

[EndPointRequestSummary('ATags', 'ASummary', 'ADescription', 'AProduces', 'AConsume')]
  • Tags: Defines a tag.
  • Summary: A method title.
  • Description: A method description.
  • Produces: A MIME type that the API can produce. This is global to all APIs but can be overridden on specific API calls. The value must be as described under Mime Types.
  • Consume: A MIME type that the API can consume. This is global to all APIs but can be overridden on specific API calls. The value must be as described under Mime Types.

Example of description declaration of a GET method of an EndPoint:

[EndPointRequestSummary('Api Doc', 'Get API EndPoints', 'Used to retrieve all the API EndPoints', 'application/json', '')]
procedure Get(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);

EndPointRequestParameter

Description of the parameters used in a request.

A unique parameter is defined by a combination of a name and location.

There are five possible parameter types: Path, Query, Header, Body, and Form.

[EndPointRequestParameter('AIn', 'AName', 'ADescription', 'ARequired', 'AType', 'AFormat', 'AItemType', 'AJSONSchema', 'AReference')]
  • ParamIn: The location of the parameter: Path, Query, Header, Body or Form.
  • Name: The name of the parameter. Parameter names are case sensitive.
    • When the location of the parameter is 'Body', the name must be 'body'.
    • When the location of the parameter is 'Path', the name must correspond to the associated path segment from the path field in the Paths object.
    • For the rest of cases, the name corresponds with ParamIn.
  • Description: A brief description of the parameter. This could contain examples of use. GFM syntax can be used for rich text representation.
  • Required: Determines if this parameter is mandatory. If the parameter is in 'Path', this property is required and its value must be True, otherwise the property may be included and its default value is False.
  • ParamType: The type of the parameter. For other value than 'Body', the value must be one of the following values: 'spArray', 'spBoolean', 'spInteger', 'spNumber', 'spNull', 'spObject', 'spString', 'spFile'. If ParamType is 'spFile', the consume MIME type must be either "multipart/form-data" or "application/x-www-form-urlencoded", and the parameter must be in "form-data". For the value 'Body', JSONSchema and Reference are required.
  • ItemFormat: The extending format for the ParamType: 'None', 'Int32', 'Int64', 'Float', 'Double', 'Byte', 'Date', 'DateTime', 'Password'.
  • ItemType: Required if ParamType is 'array'. It describes the type of items in the array.
  • JSONSchema: The Schema Definition of the Primitive sent to the server. A definition of the body request structure. If ParamType is 'Array' or 'Object', a schema can be defined.
    • For example:
      In JSON format: {"type": "object","additionalProperties": {"type": "string"}}.
      In YAML format:
      type: object
      additionalProperties:
      type: string
  • Reference: The Schema Definition of the Primitive sent to the server. A definition of the body request structure. If Type is 'Array' or 'Object', a schema can be defined.
    • For example:
      '#/definitions/pet'
[EndPointRequestParameter(TAPIDocParameter.TParameterIn.Path, 'item', 'Path Parameter item Description', true, TAPIDoc.TPrimitiveType.spString, TAPIDoc.TPrimitiveFormat.None, TAPIDoc.TPrimitiveType.spString, '', '')]

EndPointResponseDetails

Description of the request responses:

[EndPointResponseDetails('ACode', 'ADescription', 'AType', 'AFormat', 'ASchema', 'AReference')]
  • Code: The response code.
  • Description: Description of the response code.
  • PrimitiveType: The Type of the Primitive returned. Primitive data types in the Swagger Specification are based on the types supported by the JSON-Schema Draft 4. JSON Schema defines seven primitive types for JSON values: 'spArray', 'spBoolean', 'spInteger', 'spNumber', 'spNull', 'spObject', 'spString'. See JSON Schema primitive types. An additional primitive data type, 'spFile', is used by the Parameter Object and the Response Object to set the parameter type or the response as being a file.
  • PrimitiveFormat: The Format of the Primitive returned, for example: 'None', 'Int32', 'Int64', 'Float', 'Double', 'Byte', 'Date', 'DateTime', 'Password'.
  • Schema:
    • For example:
      In JSON format: {"type": "object","additionalProperties": {"type": "string"}}.
      in YAML format:
      type: object
      additionalProperties:
      type: string
  • Reference: The Schema Definition of the Primitive returned. A definition of the response structure. If PrimitiveType is 'spArray' or 'spObject', a Schema definition can be defined:
    • For example:
      '#/definitions/pet'
[EndPointResponseDetails(200, 'Ok', TAPIDoc.TPrimitiveType.spObject, TAPIDoc.TPrimitiveFormat.None, '', '#/definitions/EmployeeTable')]
[EndPointResponseDetails(404, 'Not Found', TAPIDoc.TPrimitiveType.spNull, TAPIDoc.TPrimitiveFormat.None, '', '')]

EndPointObjectsYAMLDefinitions

Definition of objects for the API YAML version.

  [EndPointObjectsYAMLDefinitions(Objects)]
  • Objects: YAML Objects definitions.
type
  [ResourceName('SampleAttributes')]
  [EndPointObjectsYAMLDefinitions(cYamlDefinitions)]

// YAML Object definition:
  cYamlDefinitions =
  '# ' +  sLineBreak +
  ' PutObject:' +  sLineBreak +
  '   properties:' +  sLineBreak +
  '    EMP_NO:' +  sLineBreak +
  '     type: integer' +  sLineBreak +
  '    FIRST_NAME:' +  sLineBreak +
  '     type: string' +  sLineBreak +
  '    LAST_NAME:' +  sLineBreak +
  '     type: string' +  sLineBreak +
  '    PHONE_EXT:' +  sLineBreak +
  '     type: string' +  sLineBreak +
  '    HIRE_DATE:' +  sLineBreak +
  '     type: string' +  sLineBreak +
  '     format: date-time' +  sLineBreak +
  '    DEPT_NO:' +  sLineBreak +
  '     type: string' +  sLineBreak +
  '    JOB_CODE:' +  sLineBreak +
  '     type: string' +  sLineBreak +
  '    JOB_GRADE:' +  sLineBreak +
  '     type: integer' +  sLineBreak +
  '    JOB_COUNTRY:' +  sLineBreak +
  '     type: string' +  sLineBreak +
  '    SALARY:' +  sLineBreak +
  '     type: integer' +  sLineBreak +
  '    FULL_NAME:' +  sLineBreak +
  '     type: string' +  sLineBreak +
  '# '

EndPointObjectsJSONDefinitions

Definition of objects for the API JSON version.

    [EndPointObjectsJSONDefinitions(Objects)]
  • Objects: JSON Objects definitions.
type
  [ResourceName('SampleAttributes')]
  [EndPointObjectsJSONDefinitions(cJSONDefinitions)]

// JSON Object definition:

cJSONDefinitions =
 '{' +  sLineBreak +
  '   "PutObject": {' +  sLineBreak +
  '       "properties": {' +  sLineBreak +
  '           "EMP_NO": {' +  sLineBreak +
  '               "type": "integer"' +  sLineBreak +
  '           },' +  sLineBreak +
  '           "FIRST_NAME": {' +  sLineBreak +
  '               "type": "string"' +  sLineBreak +
  '           },' +  sLineBreak +
  '           "LAST_NAME": {' +  sLineBreak +
  '               "type": "string"' +  sLineBreak +
  '           },' +  sLineBreak +
  '           "PHONE_EXT": {' +  sLineBreak +
  '               "type": "string"' +  sLineBreak +
  '           },' +  sLineBreak +
  '           "HIRE_DATE": {' +  sLineBreak +
  '               "type": "string",' +  sLineBreak +
  '               "format": "date-time"' +  sLineBreak +
  '           },' +  sLineBreak +
  '           "DEPT_NO": {' +  sLineBreak +
  '               "type": "string"' +  sLineBreak +
  '           },' +  sLineBreak +
  '           "JOB_CODE": {' +  sLineBreak +
  '               "type": "string"' +  sLineBreak +
  '           },' +  sLineBreak +
  '           "JOB_GRADE": {' +  sLineBreak +
  '               "type": "integer"' +  sLineBreak +
  '           },' +  sLineBreak +
  '            "JOB_COUNTRY": {' +  sLineBreak +
  '                "type": "string"' +  sLineBreak +
  '            },' +  sLineBreak +
  '            "SALARY": {' +  sLineBreak +
  '                "type": "integer"' +  sLineBreak +
  '            },' +  sLineBreak +
  '            "FULL_NAME": {' +  sLineBreak +
  '                "type": "string"' +  sLineBreak +
  '            }' +  sLineBreak +
  '        }' +  sLineBreak +
  '    }'

Topics

See Also