2.0 Annotations
An RDCore.Web application declares its endpoints with comment annotations. '@Controller makes a module a controller; '@HttpGet, '@HttpPost and
the annotations of the other HTTP methods make a procedure of a controller an endpoint, and '@FromBody names the parameter a request's body binds
to. '@KeptMachine keeps a machine for each key of a controller, and '@NewMachine makes it. '@Service declares a service,
'@ControllerFactory names the function that makes a controller, and '@Authorize and '@AllowAnonymous say who may reach it. They follow the
comment annotation syntax of
RD-VBAL §3.0.1.1 Comment Annotations Syntax:
to VBA they are comments, so an annotated module compiles as it is.
RDWebEndpointReader reads a module's endpoints from the annotations of its parse, or those of every
module of a program.
The RDWeb extension declares these annotations in its manifest, with a description of each one and of its argument in English and French: the platform knows them, and where each one is legal, without starting the extension.
| Annotation | Kind | Written | Argument |
|---|---|---|---|
'@Controller |
Module annotation | In the module's declarations section, or above the header of a block (Module Name, Class Name) in a file of blocks. |
The controller's route (§3.1). Optional. |
'@HttpGet |
Member annotation | Immediately above the procedure. | The endpoint's route (§3.2). Optional. |
'@HttpPost |
Member annotation | Immediately above the procedure. | The endpoint's route. Optional. |
'@HttpPut, '@HttpPatch, '@HttpDelete, '@HttpHead, '@HttpOptions |
Member annotation | Immediately above the procedure. | The endpoint's route. Optional (§2.3). |
'@FromBody |
Member annotation | Immediately above the procedure, with its HTTP method annotations. | The body parameter's name (§2.4). |
'@KeptMachine |
Module annotation | With '@Controller. |
The route parameter whose value is the machine's key (§2.5). |
'@NewMachine |
Member annotation | Immediately above the procedure, with its HTTP method annotations. | None (§2.6). |
'@Service |
Module annotation | In the declarations section of a class module. | The service's name, then its lifetime. Both optional (§2.7). |
'@ControllerFactory |
Module annotation | In the declarations section of a controller class module with a default instance. | The factory's name (§2.8). |
'@Authorize |
Module or member annotation | With '@Controller, or immediately above the procedure. |
A policy's name. Optional (§2.9). |
'@AllowAnonymous |
Module or member annotation | With '@Controller, or immediately above the procedure. |
None (§2.9). |
Annotation names are compared without regard to case, as RD-VBA names are: '@httpget is '@HttpGet. A '@Controller below the first procedure of a
module is not in its declarations section, and does not make it a controller.
Only a Public Sub or Function serves an endpoint, or one with no access modifier, which is Public. A Private or Friend procedure serves none.
'@Controller("api/orders")
Option Explicit
'@HttpGet("{id}")
Public Function Order(ByVal id As Variant) As Variant
' ...
End Function
'@HttpPost
Public Sub Add(ByVal order As Variant)
' ...
End Sub
Order serves GET /api/orders/{id}, and Add serves POST /api/orders.
2.1 '@Controller
Syntax
'@Controller
'@Controller("route")
The argument is a string literal: the route that prefixes the routes of the module's endpoints.
Static Semantics
| Case | Controller route |
|---|---|
'@Controller("api/orders") |
api/orders. |
'@Controller with no argument |
The conventional route: the module's name less a Controller suffix (§3.1). |
A class module with a default instance (Attribute VB_PredeclaredId = True,
MS-VBAL §5.2.4.1.2 Default Instance Variables Static Semantics)
is a controller as any class module is. Its default instance runs only its factory, Create or the public Function that '@ControllerFactory names
(§2.8), and each request's procedure runs on the object the factory returns (§7.3).
A controller is a standard module or a class module, with a default instance or without.
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.Controller |
The annotation's name. |
RDWebEndpointReader.Read |
Reads the endpoints of a controller module, with a default instance or without. |
RDWebEndpoint.FromAnnotation |
Takes the argument as controllerRoute; null when the annotation writes none. |
2.2 '@HttpGet
Syntax
'@HttpGet
'@HttpGet("route")
The argument is a string literal: the endpoint's route, under the controller's.
Static Semantics
The annotation declares an endpoint for the HTTP GET method, served by the procedure immediately below it. Its route is made as
§3.2 describes. A GET endpoint has no body parameter: every parameter is bound from the route or the query
string.
Runtime Semantics
A GET request whose path matches the endpoint's route runs the procedure. A request of another method on that route answers 405 Method Not Allowed
and runs nothing.
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.HttpGet |
The annotation's name. |
RDWebAnnotations.HttpMethodOf |
GET for HttpGet, without regard to case. |
2.3 '@HttpPost
Syntax
'@HttpPost
'@HttpPost("route")
The argument is a string literal: the endpoint's route, under the controller's.
Static Semantics
The annotation declares an endpoint for the HTTP POST method, served by the procedure immediately below it. Its route is made as
§3.2 describes. The body parameter, which the request's JSON body binds to, is the one '@FromBody names
(§2.4), else the first parameter of the procedure that the route does not bind. A POST whose route binds every parameter, and that has
no '@FromBody, has no body parameter.
Runtime Semantics
A POST request whose path matches the endpoint's route runs the procedure. A request of another method on that route answers
405 Method Not Allowed and runs nothing.
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.HttpPost |
The annotation's name. |
RDWebAnnotations.HttpMethodOf |
POST for HttpPost, without regard to case. |
RDWebEndpoint.BodyParameter |
The parameter the body binds to. |
Other HTTP Methods
'@HttpPut("{id}")
'@HttpPatch("{id}")
'@HttpDelete("{id}")
'@HttpHead("{id}")
'@HttpOptions
Each annotation declares an endpoint for its HTTP method, as '@HttpPost does for POST: its argument is the optional route, a procedure may have
several, and a request of another method on the route answers 405 Method Not Allowed. They differ in the body they bind.
| Annotation | Method | Body parameter |
|---|---|---|
'@HttpPut |
PUT |
As a POST's: the one '@FromBody names, else the first parameter the route does not bind. |
'@HttpPatch |
PATCH |
As a POST's. |
'@HttpDelete |
DELETE |
The one '@FromBody names; none without it. |
'@HttpHead |
HEAD |
None. The response has the status and headers of the procedure's, and no body. |
'@HttpOptions |
OPTIONS |
None. |
| Type or member | Role |
|---|---|
RDWebAnnotations.HttpPut, HttpPatch, HttpDelete, HttpHead, HttpOptions |
The annotations' names. |
RDWebAnnotations.BindsBody |
Whether a method binds a body: POST, PUT, PATCH and DELETE. |
RDWebAnnotations.BindsBodyImplicitly |
Whether it binds one without '@FromBody: POST, PUT and PATCH. |
2.4 '@FromBody
Syntax
'@HttpPost("{id}/lines")
'@FromBody("line")
Public Function AddLine(ByVal id As Variant, ByVal notify As Variant, ByVal line As Variant) As Variant
' ...
End Function
The argument is a string literal: the name of a parameter of the procedure. The annotation names a parameter because a parameter list holds no comment.
Static Semantics
| Case | Effect |
|---|---|
A POST, PUT, PATCH or DELETE endpoint |
The body binds to the parameter named, matched without regard to case and written as the procedure spells it. The other parameters the route does not bind are bound from the query string. |
A GET, HEAD or OPTIONS endpoint |
None: it binds no body, whatever '@FromBody names. |
| A name that is not a parameter of the procedure | An error: FromAnnotation throws RDWebEndpointException, whose message names the parameter, the module and the procedure. |
| A name that is a parameter the route binds | The same error. |
A POST, a PUT or a PATCH with no '@FromBody still binds its body, to the first parameter the route does not bind; a DELETE binds none. Diagnostic
RDW00001 reports it, with a quick fix that adds the annotation, and RDW00002 reports each
further parameter that neither the route nor the body binds.
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.FromBody |
The annotation's name. |
RDWebEndpoint.FromAnnotation |
Takes the argument as bodyParameter; null when the procedure has none. |
2.5 '@KeptMachine
Syntax
'@Controller("api/games/{game}")
'@KeptMachine("game")
Option Explicit
The argument is a string literal: the name of a parameter of the controller's routes.
Static Semantics
| Case | Effect |
|---|---|
| Every endpoint of the controller | Keyed by the value of the route parameter named. A request runs on its key's machine (§6.2). |
| The procedure has a parameter of that name | The route value binds to it, as any route value does. |
| The procedure has none | The route value binds nothing: the key need not be a parameter of the procedure. |
| An endpoint whose route has no parameter of that name | An error: FromAnnotation throws RDWebEndpointException, whose message names the key, the module and the procedure. |
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.KeptMachine |
The annotation's name. |
RDWebEndpoint.KeptMachineKey |
The route parameter named; null for an endpoint whose requests run on a machine of the pool. |
2.6 '@NewMachine
Syntax
'@HttpPost("new")
'@NewMachine
Public Sub NewGame()
' ...
End Sub
The annotation takes no argument.
Static Semantics
A request to the endpoint makes its key's machine, then runs the procedure on it. A request to any other endpoint of the controller runs on the machine its
key has. A '@NewMachine in a controller without '@KeptMachine is an error: FromAnnotation throws RDWebEndpointException.
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.NewMachine |
The annotation's name. |
RDWebEndpoint.MakesMachine |
Whether a request makes its key's machine. |
2.7 '@Service
Syntax
'@Service
'@Service("IOrderRepository")
'@Service("IClock", "Singleton")
The arguments are string literals: the service's name, then its lifetime.
Static Semantics
A class module with '@Service is a service: a parameter of a factory, a filter or another service whose declared type is the service's name is given
an object of the class (§7.3).
| Argument | Meaning |
|---|---|
| Name | The type a parameter declares to be given the service. The class is that type, or names it in an Implements statement (MS-VBAL §5.2.4.2 Implements Directive). Without it, the class's own name. |
| Lifetime | Scoped (one object per request), Transient (one for each parameter that needs it) or Singleton (one per machine), in any case. Without it, Scoped. |
The object is a New one, or, for a class with a default instance, the one its Create function returns, run on the default instance. Names are compared
without regard to case (MS-VBAL §3.3.5 Identifier Tokens).
Read throws RDWebServiceException for a '@Service that is not in a class module, a lifetime that is none of these, or a class that is not the type
its name is and does not implement it. Two classes that declare one name are RDW00005. A registration that Program.Run
makes of the same name wins over the class (§7.3).
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.Service |
The annotation's name. |
RDWebServiceReader |
Reads the service a class declares, and the services of a program, each name once. |
RDWebService |
A service: its name, its lifetime and its class. |
2.8 '@ControllerFactory
Syntax
'@Controller("api/orders")
'@ControllerFactory("Create")
Option Explicit
The argument is a string literal: the name of a public Function of the class.
Static Semantics
The factory of a controller class with a default instance makes the object that runs each request's procedure. It runs on the default instance, and each
of its parameters is given a service (§7.3). Without '@ControllerFactory, the factory is the class's
Create function. A factory that the class does not have is a startup error. A factory parameter that is not of an object type is
RDW00006. The annotation does nothing on a class without a default instance, whose object is a New one.
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.ControllerFactory |
The annotation's name. |
RDWebServiceReader.DefaultFactory |
Create: the factory when no annotation names another. |
2.9 '@Authorize and '@AllowAnonymous
Syntax
'@Authorize
'@Authorize("Admins")
'@AllowAnonymous
'@Authorize takes an optional string literal, the name of a policy that Program.Run adds. '@AllowAnonymous takes no argument.
Static Semantics
'@Authorize lets only a signed-in user reach the controller or the action, and its policy, when it names one, is one the user must meet.
'@AllowAnonymous lets any request reach the controller or the action, even when the controller has '@Authorize.
Note
Not implemented. RDCore.Web signs no user in. A program with '@Authorize, or whose Program.Run adds an authentication scheme or a policy, is a
startup error, and the host serves nothing of it (§7.3).
Implementation
| Type or member | Role |
|---|---|
RDWebAnnotations.Authorize |
The annotation's name. |
RDWebAnnotations.AllowAnonymous |
The annotation's name. |
Other annotations
An annotation of any other name declares no endpoint: FromAnnotation returns null for it, and a procedure with only such annotations is not served.
Implementation
| Type or member | Role |
|---|---|
RDWebEndpointReader.Read |
Reads a module's endpoints from its parse: one for each HTTP method annotation of each public procedure, in the order they are written. |
Tests
| Rule | Test |
|---|---|
'@HttpGet serves GET, on the controller's route and its own |
RDWebEndpointTests.AnHttpGetAnnotation_ServesTheProcedureForGet_OnTheControllersRouteAndItsOwn |
'@HttpPost serves POST |
RDWebEndpointTests.AnHttpPostAnnotation_ServesTheProcedureForPost |
'@HttpPut, '@HttpPatch, '@HttpDelete, '@HttpHead and '@HttpOptions serve their methods |
RDWebEndpointTests.EachRestVerbsAnnotation_ServesTheProcedureForItsMethod_OnItsRoute |
| A request of each method runs the controller's action, with the body its method binds, and its value is the response | RDWebHostDispatchTests.ARequestOfEachVerb_RunsTheControllersActionOfItsAnnotation_WithTheBodyItBinds_AndItsValueIsTheResponse, and in RD-VBA, from a composed project: RDWebHostedControllerTests.EachVerbsAction_RunsInRDVBA_WithTheBodyItsVerbBinds_AndItsValueIsTheResponse, RDWebHostedControllerTests.AHeadOfAnAction_RunsIt_AndAnswersItsStatusWithNoBody |
| Names are compared without regard to case | RDWebEndpointTests.AnAnnotationName_IsComparedWithoutRegardToCase |
| Another annotation declares no endpoint | RDWebEndpointTests.AnAnnotationOfNoHttpMethod_DeclaresNoEndpoint |
The body parameter of a POST, a PUT and a PATCH; none for a GET, a HEAD, an OPTIONS, or a DELETE without '@FromBody |
RDWebEndpointTests.ThePostBody_BindsTheFirstParameterTheRouteDoesNotBind, RDWebEndpointTests.APutOrPatchBody_BindsTheFirstParameterTheRouteDoesNotBind_AsAPostsDoes, RDWebEndpointTests.APostWhoseRouteBindsEveryParameter_HasNoBodyParameter, RDWebEndpointTests.AGetDeleteHeadOrOptions_WithNoFromBody_HasNoBodyParameter |
Another method answers 405 and runs nothing |
RDWebEndpointsHostingTests.AnEndpoint_ServesOnlyItsOwnMethod |
'@FromBody names the body parameter of a POST, a PUT, a PATCH and a DELETE; a GET, a HEAD and an OPTIONS bind none |
RDWebEndpointTests.TheBody_BindsTheParameterFromBodyNames_InTheSpellingOfTheProcedure, RDWebEndpointTests.AGetHeadOrOptions_BindsNoBody_WhateverFromBodyNames, RDWebEndpointReaderTests.AFromBodyAnnotationOfAProcedure_NamesTheBodyParameterOfItsPost_AndItsGetBindsNone |
A '@FromBody the request cannot bind is an error |
RDWebEndpointTests.AFromBodyParameterTheRequestCannotBind_IsAnEndpointException_NamingIt |
The body binds the parameter '@FromBody names; the query string binds the others |
RDWebEndpointsHostingTests.APostedJsonBody_BindsTheParameterFromBodyNames_AndTheQueryStringStillBindsTheOthers |
| A module is read: a controller's procedures are its endpoints | RDWebEndpointReaderTests.AnHttpGetProcedureOfAController_IsAnEndpoint_OnTheControllersRouteAndItsOwn, RDWebEndpointReaderTests.AModuleThatIsNoController_DeclaresNoEndpoint |
| A controller serves its endpoints with a default instance or without; a predeclared class that is not a controller declares none | RDWebEndpointReaderTests.AController_ServesItsEndpoints_WhetherOrNotItHasADefaultInstance, RDWebEndpointReaderTests.AClassWithADefaultInstanceThatIsNoController_DeclaresNoEndpoint_AndIsNoError |
'@Controller only in the declarations section, or above a block's header |
RDWebEndpointReaderTests.AControllerAnnotationAfterTheFirstProcedure_DoesNotMakeTheModuleAController, RDWebEndpointReaderTests.InAFileOfBlocks_AControllerAnnotationAboveABlocksHeader_MakesThatBlocksModuleAController |
Only a public Sub or Function serves an endpoint |
RDWebEndpointReaderTests.AProcedureThatIsNotPublic_ServesNoEndpoint, RDWebEndpointReaderTests.AProcedureWithAnImplicitAccessModifier_IsPublic_AndASubServesAnEndpointToo |
| One endpoint per HTTP method annotation, in the order written | RDWebEndpointReaderTests.EachHttpMethodAnnotationOfAProcedure_IsAnEndpoint_InTheOrderWritten_AndOtherAnnotationsAreNone |
'@KeptMachine keys each endpoint of its controller; '@NewMachine marks only its own |
RDWebEndpointReaderTests.AKeptMachineAnnotationOfAController_KeysEachOfItsEndpoints_AndNewMachineMakesTheMachineOnlyWhereItIsWritten, RDWebEndpointReaderTests.AControllerWithoutKeptMachine_KeysNoEndpoint, RDWebEndpointTests.AnEndpointThatMakesTheMachine_SaysSo |
| The key need not be a parameter of the procedure, and binds one of its name | RDWebEndpointTests.TheMachinesKey_NeedNotBeAParameterOfTheProcedure_AndThePostBodyBindsTheFirstParameterAfterIt, RDWebEndpointTests.TheMachinesKey_BindsTheProcedureParameterOfItsName_WhenTheProcedureHasOne, RDWebEndpointTests.AnotherRouteParameterOfAKeptMachinesEndpoint_StillNamesAParameterOfTheProcedure |
A key the route lacks, or '@NewMachine without '@KeptMachine, is an error |
RDWebEndpointTests.AMachinesKeyThatIsNoParameterOfTheRoute_IsAnEndpointException_NamingIt, RDWebEndpointTests.AnEndpointThatMakesAMachine_WithNoKeyToMakeItFor_IsAnEndpointException, RDWebEndpointReaderTests.NewMachineInAControllerWithoutKeptMachine_IsAnEndpointException |
A class with '@Service is a scoped service of its own name; the first argument is its name, the second its lifetime, in any case |
RDWebServiceReaderTests.AClassWithAServiceAnnotation_IsAScopedService_OfItsOwnName, RDWebServiceReaderTests.AServicesName_IsItsFirstArgument_AnInterfaceTheClassImplements, RDWebServiceReaderTests.AServicesLifetime_IsItsSecondArgument_InAnyCase, RDWebServiceReaderTests.AServicesName_IsComparedWithoutRegardToCase |
A lifetime that is none of the three, a type the class does not implement, or a '@Service in a standard module is an error |
RDWebServiceReaderTests.AnUnknownLifetime_IsAServiceException_ThatNamesTheClassAndTheLifetime, RDWebServiceReaderTests.AServiceNamedForATypeTheClassDoesNotImplement_IsAServiceException_ThatNamesTheClassAndTheType, RDWebServiceReaderTests.AServiceAnnotationInAStandardModule_IsAServiceException_ThatNamesTheModule |
| A service of a class with a default instance is made by its factory | RDWebServiceReaderTests.AServiceIsMadeByItsFactory_OnlyWhenItsClassHasADefaultInstance |
| Two classes of one service name are an error; distinct names are kept in order | RDWebServiceReaderTests.TwoClassesOfOneServiceName_InAnyCase_AreAServiceException_ThatNamesBothClassesAndTheName, RDWebServiceReaderTests.ServicesOfDistinctNames_AreKept_InTheOrderTheyAreGiven |