Skip to main content

Annotations

This document provides a comprehensive guide to all annotations and options supported by Gleece for controllers and schemas.

Table of Contents

Overview

  • Gleece uses annotations to automatically generate routes & OpenAPI documentation. The format for the annotations is:

    // {{ annotation }} {{ ( {{theBasicRequiredParam}}, { json5 attributes } ) }} {{ description }}
    • Annotation: This is always required and specifies the type of annotation, such as @Tag, @Route, @Method, etc.
    • theBasicRequiredParam: This parameter is required depending on the type of annotation. For example, @Route requires a route path, @Method requires an HTTP method, etc.
    • json5 attributes: These are optional attributes in JSON5 format that provide additional information for the annotation. For example, in @Header(origin, { name: x-origin }), { name: x-origin } is a JSON5 attribute specifying the name of the header in the http request, while the origin is the name of the parameter in the given function.
    • Description: This is an optional description that provides further details about the annotation. It helps in generating more descriptive documentation.

Paths Annotations

Class-Level Annotations - Controller

These annotations are used at the controller class level.

AnnotationPurposeParameterJSON5 Options RefRequiredcomment
@TagDefines the OpenAPI tag for grouping routestagName-No
@RouteDefines the base route path for all methodspath-Yes
@SecuritySet the default security for controller's routessecuritySchemeNameSecurity OptionsNoSupports multiple annotation and will be count as logical OR between them
@DescriptionProvides a detailed description of the controller--No
@DeprecatedMarks the entire controller as deprecated--No

Function-Level Annotations - Route

AnnotationPurposeParameterJSON5 Options RefRequiredComment
@MethodDefines the HTTP method for the routehttpVerb-Yes
@RouteDefines the route path for the methodpath-YesRoute var supports OpenAPI v3 Specification - Path Templating (e.g. /users/{userId})
@QueryDefines a query parameter for the endpointparamNameQuery OptionsNoSupport description
@HeaderDefines an header parameter for the endpointparamNameHeader OptionsNoSupport description
@PathDefines a path parameter for the endpointparamNamePath OptionsNoSupport description
@BodyDefines a body parameter for the endpointparamNameBody OptionsNoSupport description, only one body parameter is allowed per route. Cannot be used with @Form
@FormFieldDefines URL-encoded form parameter for the endpointparamNameForm Field OptionsNoSupport description. Used as x-www-form-urlencoded request body. Cannot be used with @Body
@SecurityDefines a security for the routesecuritySchemeNameSecurity OptionsNoSupports multiple annotation and will be count as logical OR between them
@ResponseSpecifies the success response codestatusCode-NoDescription can include HTML formatting
@ErrorResponseDefines possible error responsestatusCode-NoDescription can include HTML formatting
@DescriptionProvides a detailed description of the endpoint--No
@DeprecatedMarks the endpoint as deprecated--No
@HiddenHides the endpoint from API documentation--No
@TemplateContextDefines custom context data for templatescontextKeyAny JSON5 objectNoSupports multiple annotations with unique contextKey values. See Custom Context for more details

Schema Annotations

Struct Annotations - Schema

AnnotationPurposeParameterJSON5 Options RefRequiredComment
@DescriptionProvides a detailed description of the schema--No
@DeprecatedMarks the schema as deprecated--No

Field Annotations - Property

AnnotationPurposeParameterJSON5 Options RefRequiredComment
@DescriptionProvides a detailed description of the property--No
@DeprecatedMarks the property as deprecated--No

A struct's property supported the standard json and validate in the field tag e.g. json:"fieldName" validate:"required"

Advanced Options

Query Options

Field NameDescriptionDefault
nameThe name of the query in the HTTP requestFunction parameter name
validateStandard go-playground validation string-

Path Options

Field NameDescriptionDefault
nameThe name of the path element in the HTTP requestFunction parameter name
validateStandard go-playground validation string-

Header Options

Field NameDescriptionDefault
nameThe name of the header in the HTTP requestFunction parameter name
validateStandard go-playground validation string-

Body Options

Field NameDescriptionDefault
validateStandard go-playground validation string-

Form Field Options

Field NameDescriptionDefault
nameThe name of the form field in the requestFunction parameter name
validateStandard go-playground validation string-

Security Options

Field NameDescriptionDefault
scopesThe scopes e.g. [read:users, write:users]-