|
| 1 | +# IMPORT REST CLIENT FROM OPENAPI |
| 2 | + |
| 3 | +Import a consumed REST service from an OpenAPI 3.0 JSON specification in one step. The spec is parsed automatically and stored in the service document — exactly as Mendix Studio Pro does when you use its built-in OpenAPI import. |
| 4 | + |
| 5 | +**Requires Mendix 10.1+.** |
| 6 | + |
| 7 | +## Syntax |
| 8 | + |
| 9 | +```sql |
| 10 | +IMPORT REST CLIENT Module.Name FROM OPENAPI '/path/to/openapi.json'; |
| 11 | + |
| 12 | +-- Idempotent: replace an existing service with the same name |
| 13 | +IMPORT OR REPLACE REST CLIENT Module.Name FROM OPENAPI '/path/to/openapi.json'; |
| 14 | + |
| 15 | +-- Override base URL (use when spec has no servers array) |
| 16 | +IMPORT REST CLIENT Module.Name FROM OPENAPI '/path/to/openapi.json' |
| 17 | + SET BaseUrl: 'https://api.example.com'; |
| 18 | + |
| 19 | +-- Override base URL and authentication |
| 20 | +IMPORT OR REPLACE REST CLIENT Module.Name FROM OPENAPI '/path/to/openapi.json' |
| 21 | + SET BaseUrl: 'https://api.example.com', |
| 22 | + Authentication: BASIC (Username: '$Module.ApiUser', Password: '$Module.ApiPass'); |
| 23 | +``` |
| 24 | + |
| 25 | +## Parameters |
| 26 | + |
| 27 | +| Parameter | Description | |
| 28 | +|-----------|-------------| |
| 29 | +| `Module.Name` | Qualified name for the new REST client. Module must already exist. | |
| 30 | +| `'/path/to/openapi.json'` | Absolute or relative path to the OpenAPI 3.0 JSON spec file. | |
| 31 | +| `OR REPLACE` | If a REST client with the same name already exists, replace it. Without this clause the command fails if the service already exists. | |
| 32 | +| `SET BaseUrl` | Override the base URL from the spec's `servers[0].url`. Required when the spec has no `servers` array. | |
| 33 | +| `SET Authentication` | Override the authentication. Supported values: `NONE`, `BASIC (Username: '...', Password: '...')`. | |
| 34 | + |
| 35 | +## What Gets Imported |
| 36 | + |
| 37 | +| OpenAPI Field | REST Client Field | |
| 38 | +|---------------|-------------------| |
| 39 | +| `servers[0].url` | `BaseUrl` (trailing slash stripped) | |
| 40 | +| `paths[path][method].operationId` | Operation name (sanitized to valid identifier) | |
| 41 | +| `paths[path]` | `Path` (same `{param}` placeholder format as Mendix) | |
| 42 | +| `paths[path][method]` | `Method` (uppercased: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`) | |
| 43 | +| `parameters[*].in = "path"` | `Parameters` | |
| 44 | +| `parameters[*].in = "query"` | `Query` | |
| 45 | +| `requestBody` (non-DELETE/HEAD) | `Body: JSON` | |
| 46 | +| `responses["200"/"201"/"2XX"]` | `Response: JSON` | |
| 47 | +| `responses["204"]` / no 2xx | `Response: NONE` | |
| 48 | +| Raw spec bytes | `OpenApiFile.Content` (stored as-is, enables "View OpenAPI" in Studio Pro) | |
| 49 | + |
| 50 | +**Note:** duplicate parameter names within a single operation (a spec authoring error) are preserved as-is, matching Studio Pro behaviour. Studio Pro will surface these as warnings when you open the project. |
| 51 | + |
| 52 | +### Type Mapping |
| 53 | + |
| 54 | +| OpenAPI Type | Format | Mendix Type | |
| 55 | +|--------------|--------|-------------| |
| 56 | +| `string` | — | `String` | |
| 57 | +| `integer` | `int32` (or none) | `Integer` | |
| 58 | +| `integer` | `int64` | `Long` | |
| 59 | +| `number` | — | `Decimal` | |
| 60 | +| `boolean` | — | `Boolean` | |
| 61 | + |
| 62 | +## Examples |
| 63 | + |
| 64 | +### Basic import |
| 65 | + |
| 66 | +```sql |
| 67 | +-- Create the module first |
| 68 | +CREATE MODULE PetStoreIntegration; |
| 69 | + |
| 70 | +-- Import all endpoints from the spec |
| 71 | +IMPORT REST CLIENT PetStoreIntegration.PetStoreAPI FROM OPENAPI '/specs/petstore.json'; |
| 72 | +``` |
| 73 | + |
| 74 | +### Spec without servers array |
| 75 | + |
| 76 | +```sql |
| 77 | +IMPORT REST CLIENT MyModule.YourAPI FROM OPENAPI '/specs/openapi.json' |
| 78 | + SET BaseUrl: 'https://api.example.com/v1'; |
| 79 | +``` |
| 80 | + |
| 81 | +### Idempotent re-import after spec update |
| 82 | + |
| 83 | +```sql |
| 84 | +IMPORT OR REPLACE REST CLIENT MyModule.YourAPI FROM OPENAPI '/specs/openapi.json'; |
| 85 | +``` |
| 86 | + |
| 87 | +## DESCRIBE OPENAPI FILE |
| 88 | + |
| 89 | +Preview what `IMPORT REST CLIENT` would generate without modifying the project. No project connection is needed. |
| 90 | + |
| 91 | +```sql |
| 92 | +DESCRIBE OPENAPI FILE '/path/to/openapi.json'; |
| 93 | +``` |
| 94 | + |
| 95 | +Output is a complete `CREATE REST CLIENT` statement — re-executable MDL showing all operations that would be created. |
| 96 | + |
| 97 | +## Studio Pro Compatibility |
| 98 | + |
| 99 | +The raw spec is stored in the service document's `OpenApiFile.Content` field. When you open the project in Studio Pro, the REST client shows a **View OpenAPI** button and validates operations against the spec, exactly as if you had used Studio Pro's built-in import. |
| 100 | + |
| 101 | +## Related |
| 102 | + |
| 103 | +- [REST Client Statements](README.md) — overview of all REST client commands |
| 104 | +- [SEND REST REQUEST](../../language/microflow/send-rest-request.md) — call an operation from a microflow |
| 105 | +- `SHOW REST CLIENTS` / `DESCRIBE REST CLIENT Module.Name` — list and inspect imported services |
0 commit comments