9.0 OpenAPI
The host serves an OpenAPI 3.1 document of the application's endpoints at /openapi/v1.json. Its schemas
are those of the procedures' declared types, as the program's binding has them, so a client made from it sends and receives the JSON the endpoints
convert (§8.0). It is an RD-VBA addition with no MS-VBAL counterpart.
The document is made at its first request. With it, the host serves a Swagger UI page at /swagger: the
document's operations, grouped by module with their descriptions, each one called from the page with the values typed there. The host serves both
in the Development environment, in which dotnet run starts it, and RDWeb:OpenApi turns them on or off in any environment
(§7.2).
9.1 The Document
| Part | What it holds |
|---|---|
info.title |
The composition file's name, without its extension. |
tags |
A tag for each controller module, in the order of its first endpoint, its description the module's '@ModuleDescription. |
paths |
A path for each route, each route parameter written {name}, without its constraint or default (§3.3). |
| An operation | One for each endpoint, under its HTTP method. Its operationId is Module_Procedure, followed by _ and its method in lower case (_get, _put) when another operation has the name, and it is tagged with its module. Its summary is the procedure's '@Description, as a C# action's is its xmldoc summary. |
parameters |
Each route parameter, in: path. Each other parameter the request binds, in: query, required unless it is Optional (§4.0). A RequestContext parameter is not one (§7.3). A kept machine's key that names no parameter is a string. |
requestBody |
The body parameter of a POST, a PUT, a PATCH, or a DELETE with '@FromBody (§2.3), as application/json (§4.3). It is a query parameter too: a request without a body binds it from the query string, so neither is required. |
components.schemas |
The schemas the operations refer to: one for each class of the workspace they reach, and RDWeb.Problem, the problem details of an error. |
Responses
| Response | Of |
|---|---|
200 |
A Function: its value, as application/json (§5.1). |
204 |
A Sub, and a kept machine's endpoint: a program that ends there ends its machine (§5.3). |
default |
A Function declared As RDWeb.Result, when the request is dispatched (§7.3): the result's own status and body. |
400, 415 |
An endpoint with a body parameter: a body that is not JSON, or one sent as another content type. |
404 |
A kept machine's endpoint, for a key with no machine (§6.2). |
409 |
A '@NewMachine endpoint, for a key that has a machine already. |
500 |
Every endpoint: a run-time error, or a run that did not complete (§5.2). |
503 |
Every endpoint: no machine can run the request now. |
Each error response is problem details, application/problem+json, of the RDWeb.Problem schema: type, title, status, detail, and the
errorNumber and errorCode of a run-time error.
9.2 Schemas
A parameter, a body and a value have the JSON Schema of their declared type: the JSON its value converts to, and the JSON to give it.
| Declared type | Schema |
|---|---|
Boolean |
boolean. |
Byte, Integer, Long, LongLong, LongPtr, an Enum |
integer, from the type's least value to its greatest (an Enum is a Long, and a LongPtr a LongLong in a 64-bit environment and a Long in a 32-bit one), of format int32, or int64 for a 64-bit type. |
Single, Double |
number, of format float or double. |
Currency, Decimal |
number. |
Date |
A string of the pattern ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$. Its format is not date-time, which has an offset. |
String, a fixed-length String |
string. |
Variant |
Any JSON value. |
Object, Scripting.Dictionary |
An object, or null. |
Collection |
An array of any values, or null. |
| A class of the workspace | Its schema in components.schemas, or null. |
| Another class | null, which is Nothing. |
| A dynamic array | An array of its elements' schema. |
| A UDT, a fixed-size array, an array of UDTs | None: no JSON converts to it. |
The schema of a class is an object of the members its JSON has and of those a key gives a value (§8.4), named as
the class declares them. A member only written, a Property Get with no Let or Set, is readOnly; a member only given a value is writeOnly. A
member whose type has none is left out. Its name is the class's, each character other than an ASCII letter, a digit, ., - and _ written _, and
followed by _2 when another class has it.
9.3 A TypeScript Client
openapi-typescript writes the document's types, and openapi-fetch is a fetch
client typed by them. With the host running:
npm install --save-dev openapi-typescript@7.13.0
npm install openapi-fetch@0.17.0
npx openapi-typescript http://localhost:5000/openapi/v1.json --output src/api.d.ts
Each call names its path and method, and its parameters, body and value have the types of the procedure's:
import createClient from "openapi-fetch";
import type { paths } from "./api";
const api = createClient<paths>({ baseUrl: "/" });
const { data, error } = await api.GET("/api/orders/{id}", { params: { path: { id: 42 } } });
error is the problem details of an error response.
Implementation
| Type or member | Role |
|---|---|
RDWebOpenApi |
Makes the document of the endpoints, with the program's schemas. |
RDWebHostApplication.OpenApiKey |
The RDWeb:OpenApi key, which turns the document and its page on or off. |
RDWebHostApplication.SwaggerPath |
The path of the Swagger UI page. |
RDWebEndpoint.ReturnsResult |
Whether a Function is declared As RDWeb.Result. |
RDWebEndpoint.Description, ControllerDescription |
The procedure's and its module's descriptions. |
RDCore.SDK IComposedProgram.JsonSchemasAsync |
The schemas of the program's procedures, as its binding has them (ProgramJsonSchemas). |
RDCore.SDK JsonSchemaSet |
The schema of a declared type, and the schemas of the classes it reaches. |
Tests
| Rule | Test |
|---|---|
| Each endpoint is an operation of its route's path, named by its module and procedure, and tagged with its module | RDWebOpenApiTests.EachEndpoint_IsAnOperationOfItsRoutesPath_NamedByItsModuleAndProcedure_AndTaggedWithItsModule |
| An operation is summarized by its procedure's description, and each module's tag described by the module's | RDWebOpenApiTests.AnOperation_IsSummarizedByItsProceduresDescription_AndEachModulesTag_DescribedByTheModules |
| An endpoint has its procedure's and its module's descriptions | RDWebEndpointReaderTests.AnEndpoint_IsDescribedByItsProceduresDescription_AndByItsModulesDescription |
| A procedure served several times has an operation name for each | RDWebOpenApiTests.AProcedureServedSeveralTimes_HasAnOperationNameForEach |
| Route parameters are path parameters, the others query parameters, and the request's context is none | RDWebOpenApiTests.TheRoutesParametersArePathParameters_TheOthersQueryParameters_AndTheRequestsContextIsNone |
A POST's body parameter is its request body and a query parameter, required in neither |
RDWebOpenApiTests.APostsBodyParameter_IsItsRequestBody_AndAQueryParameter_RequiredInNeither |
Each parameter, the body and the value have their schemas; an Optional parameter is not required |
RDWebOpenApiTests.WithTheProgramsSchemas_EachParameterTheBodyAndTheValueHaveTheirSchemas_AndAnOptionalParameterIsNotRequired |
| The components are the schemas the operations refer to | RDWebOpenApiTests.TheComponents_AreTheSchemasTheOperationsReferTo_NotTheRequestsContexts |
The responses are those the host answers with: 400 and 415 for an endpoint with a body parameter |
RDWebOpenApiTests.TheResponses_AreThoseTheHostAnswersWith |
| A composed project's document has an operation for each verb of its actions, and its Swagger UI page is served | RDWebHostedControllerTests.TheOpenApiDocument_HasAnOperationForEachVerbOfTheProject_AndItsSwaggerPageIsServed |
| A kept machine's key that names no parameter is a string path parameter | RDWebOpenApiTests.AKeptMachinesKey_ThatNamesNoParameter_IsAStringPathParameter |
A Function declared As RDWeb.Result returns a result, unless the program has a module named Result |
RDWebEndpointReaderTests.AFunctionDeclaredAsTheLibrarysResult_ReturnsAResult_UnlessTheProgramHasAModuleOfThatName |
| The host serves the document, with the program's schemas | RDWebHostApplicationTests.TheOpenApiDocument_IsServed_WithTheProgramsSchemas |
| The host serves the Swagger UI page, which reads the document | RDWebHostApplicationTests.TheSwaggerPage_IsServed_WithTheDocument |
The document and its page are served in Development, and as RDWeb:OpenApi says when it is given |
RDWebHostApplicationTests.TheDocumentAndItsSwaggerPage_AreServedInDevelopment_OrAsOpenApiSays |