@bazo/openapi-client-generator 3.0.13 → 3.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -5
- package/bin.js +1043 -34
- package/package.json +1 -1
- package/skills/generate-client/SKILL.md +19 -8
- package/skills/integrate-php-client/SKILL.md +200 -0
package/package.json
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
name: generate-client
|
|
3
3
|
description: >
|
|
4
4
|
Run the @bazo/openapi-client-generator CLI to produce a typed API client
|
|
5
|
-
from an OpenAPI JSON spec. Covers CLI flags (-n/--name, -m/--mode
|
|
6
|
-
positional arguments (spec path, output
|
|
7
|
-
(full, playwright, swift, kotlin
|
|
8
|
-
integration. Load when generating,
|
|
9
|
-
openapi-client-generator CLI.
|
|
5
|
+
from an OpenAPI JSON spec. Covers CLI flags (-n/--name, -m/--mode,
|
|
6
|
+
--namespace, --php-version), positional arguments (spec path, output
|
|
7
|
+
directory), generation modes (full, playwright, swift, kotlin, php),
|
|
8
|
+
output file naming, and npm script integration. Load when generating,
|
|
9
|
+
regenerating, or configuring the openapi-client-generator CLI.
|
|
10
10
|
type: core
|
|
11
11
|
library: openapi-client-generator
|
|
12
12
|
library_version: "3.0.0"
|
|
@@ -36,7 +36,9 @@ Arguments and flags:
|
|
|
36
36
|
| `<spec.json>` | yes | Path to a local OpenAPI JSON file |
|
|
37
37
|
| `<output-dir>` | yes | Directory for the generated file |
|
|
38
38
|
| `-n, --name` | yes | Client class name (must be a valid TypeScript identifier) |
|
|
39
|
-
| `-m, --mode` | no | `full` (default), `playwright`, `swift`, or `
|
|
39
|
+
| `-m, --mode` | no | `full` (default), `playwright`, `swift`, `kotlin`, or `php` |
|
|
40
|
+
| `--namespace` | no | Root PHP namespace (php mode only, default: `--name`) |
|
|
41
|
+
| `--php-version` | no | Target PHP version for php mode: `8.0`–`8.5` (default: `8.1`) |
|
|
40
42
|
|
|
41
43
|
Save as an npm script:
|
|
42
44
|
|
|
@@ -82,6 +84,14 @@ npx openapi-client openapi.json src/main/kotlin/api/ -n ApiClient -m kotlin
|
|
|
82
84
|
|
|
83
85
|
Produces `src/main/kotlin/api/ApiClient.kt` with `@Serializable` data classes and Ktor suspend functions.
|
|
84
86
|
|
|
87
|
+
### Generate a PHP client
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
npx openapi-client openapi.json src/Api/ -n ApiClient -m php --namespace "App\\Api" --php-version 8.2
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Unlike the other modes, php outputs a directory: `src/Api/ApiClient/` with `ApiClient.php`, `Model/*.php`, `ApiResponse.php`, `ApiException.php`, and `SerializerFactory.php` (Symfony HttpClient + Serializer). `--php-version` gates emitted language features (enums/readonly 8.1+, readonly classes 8.2+, `#[\NoDiscard]` 8.5).
|
|
94
|
+
|
|
85
95
|
## Common Mistakes
|
|
86
96
|
|
|
87
97
|
### CRITICAL Hallucinating non-existent CLI flags
|
|
@@ -98,7 +108,7 @@ Correct:
|
|
|
98
108
|
npx openapi-client api.json out/ -n MyClient -m full
|
|
99
109
|
```
|
|
100
110
|
|
|
101
|
-
The CLI uses `parseArgs` with `strict: true`. Only `-n/--name`, `-m/--mode`, `-v/--version`, and `-h/--help` exist. Any other flag causes a hard error.
|
|
111
|
+
The CLI uses `parseArgs` with `strict: true`. Only `-n/--name`, `-m/--mode`, `--namespace`, `--php-version`, `-v/--version`, and `-h/--help` exist. Any other flag causes a hard error.
|
|
102
112
|
|
|
103
113
|
Source: src/bin.ts
|
|
104
114
|
|
|
@@ -170,7 +180,7 @@ Correct:
|
|
|
170
180
|
npx openapi-client api.json out/ -n MyClient -m full
|
|
171
181
|
```
|
|
172
182
|
|
|
173
|
-
Mode must be exactly `full`, `playwright`, `swift`, or `
|
|
183
|
+
Mode must be exactly `full`, `playwright`, `swift`, `kotlin`, or `php`. Aliases like `react-query`, `ts`, `ios`, `android`, or `symfony` are not recognized.
|
|
174
184
|
|
|
175
185
|
Source: src/bin.ts
|
|
176
186
|
|
|
@@ -178,3 +188,4 @@ See also: integrate-full-client/SKILL.md — peer deps and client instantiation
|
|
|
178
188
|
See also: integrate-playwright-client/SKILL.md — Playwright fixture setup for playwright mode
|
|
179
189
|
See also: integrate-swift-client/SKILL.md — Xcode integration for swift mode
|
|
180
190
|
See also: integrate-kotlin-client/SKILL.md — Ktor setup for kotlin mode
|
|
191
|
+
See also: integrate-php-client/SKILL.md — Composer setup and Symfony HttpClient integration for php mode
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: integrate-php-client
|
|
3
|
+
description: >
|
|
4
|
+
Use the generated PHP client in a PHP 8 application. Covers Symfony
|
|
5
|
+
HttpClient and Serializer dependencies via Composer, PSR-4 autoloading of
|
|
6
|
+
the generated directory, instantiation with HttpClientInterface and
|
|
7
|
+
baseUrl, typed model classes and backed enums, ApiResponse, ApiException
|
|
8
|
+
handling, the generated SerializerFactory, and the --php-version /
|
|
9
|
+
--namespace generation flags. Load when importing or using a generated
|
|
10
|
+
php-mode client directory.
|
|
11
|
+
type: framework
|
|
12
|
+
library: openapi-client-generator
|
|
13
|
+
library_version: "3.0.0"
|
|
14
|
+
requires:
|
|
15
|
+
- generate-client
|
|
16
|
+
sources:
|
|
17
|
+
- "bazo/openapi-client-generator:src/modes/php/index.ts"
|
|
18
|
+
- "bazo/openapi-client-generator:src/modes/php/helpers.ts"
|
|
19
|
+
- "bazo/openapi-client-generator:src/modes/php/runtime.ts"
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
This skill builds on generate-client. Read it first for CLI usage.
|
|
23
|
+
|
|
24
|
+
# Integrate PHP Client
|
|
25
|
+
|
|
26
|
+
## Setup
|
|
27
|
+
|
|
28
|
+
Generate the client (unlike other modes, php outputs a directory of PSR-4 files):
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
openapi-client-generator api.json src/Api -n PetstoreClient -m php \
|
|
32
|
+
--namespace "App\\Api\\Petstore" --php-version 8.2
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
This writes `src/Api/PetstoreClient/` containing `PetstoreClient.php`, `ApiResponse.php`, `ApiException.php`, `SerializerFactory.php`, and `Model/*.php`. `--namespace` defaults to the client name; `--php-version` (8.0–8.5) defaults to 8.1 and gates emitted features (backed enums and readonly need 8.1+, `readonly class` 8.2+, `#[\Deprecated]` 8.4+, `#[\NoDiscard]` and `Uri\Rfc3986` URL building 8.5+).
|
|
36
|
+
|
|
37
|
+
Add Composer dependencies:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
composer require symfony/http-client symfony/serializer \
|
|
41
|
+
symfony/property-info symfony/property-access phpdocumentor/reflection-docblock
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`phpdocumentor/reflection-docblock` is required — without it, `Pet[]` collection properties silently degrade to arrays of plain arrays instead of hydrated model objects.
|
|
45
|
+
|
|
46
|
+
Map the namespace in `composer.json` (adjust to where you generated):
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"autoload": {
|
|
51
|
+
"psr-4": { "App\\Api\\Petstore\\": "src/Api/PetstoreClient/" }
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Instantiate the client:
|
|
57
|
+
|
|
58
|
+
```php
|
|
59
|
+
use App\Api\Petstore\PetstoreClient;
|
|
60
|
+
use Symfony\Component\HttpClient\HttpClient;
|
|
61
|
+
|
|
62
|
+
$api = new PetstoreClient(HttpClient::create(), 'https://api.example.com');
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The constructor signature is:
|
|
66
|
+
|
|
67
|
+
```php
|
|
68
|
+
public function __construct(
|
|
69
|
+
private readonly HttpClientInterface $client,
|
|
70
|
+
private readonly string $baseUrl = self::DEFAULT_BASE_URL, // default only when the spec declares servers
|
|
71
|
+
?SerializerInterface $serializer = null, // defaults to SerializerFactory::create()
|
|
72
|
+
)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
In a Symfony app, inject `HttpClientInterface` and register the client as a service instead of calling `HttpClient::create()`.
|
|
76
|
+
|
|
77
|
+
## Core Patterns
|
|
78
|
+
|
|
79
|
+
### Call a query operation
|
|
80
|
+
|
|
81
|
+
```php
|
|
82
|
+
$result = $api->listTasks(page: 1, limit: 10);
|
|
83
|
+
$result->data; // TaskList — typed, hydrated model
|
|
84
|
+
$result->status; // 200
|
|
85
|
+
$result->headers; // response headers (keys lower-cased by Symfony HttpClient)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Optional parameters default to `null`; use named arguments to skip earlier ones.
|
|
89
|
+
|
|
90
|
+
### Call a mutation operation
|
|
91
|
+
|
|
92
|
+
```php
|
|
93
|
+
use App\Api\Petstore\Model\CreateTaskRequest;
|
|
94
|
+
use App\Api\Petstore\Model\TaskStatus;
|
|
95
|
+
|
|
96
|
+
$result = $api->createTask(new CreateTaskRequest(
|
|
97
|
+
title: 'Build feature',
|
|
98
|
+
status: TaskStatus::Todo,
|
|
99
|
+
));
|
|
100
|
+
$result->data->id; // created task ID
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Bodies are typed model classes; enum-typed fields take backed enum cases (PHP 8.1+ targets). Unset optional properties are omitted from the request JSON.
|
|
104
|
+
|
|
105
|
+
### Handle API errors
|
|
106
|
+
|
|
107
|
+
```php
|
|
108
|
+
use App\Api\Petstore\ApiException;
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
$result = $api->getTask('nonexistent');
|
|
112
|
+
} catch (ApiException $e) {
|
|
113
|
+
$e->statusCode; // 404
|
|
114
|
+
$e->body; // raw response body
|
|
115
|
+
$e->responseHeaders;
|
|
116
|
+
$e->url;
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Every non-success status throws `ApiException`; Symfony's own `HttpExceptionInterface` auto-throw is disabled by the generated code (`getContent(false)`).
|
|
121
|
+
|
|
122
|
+
### Immutable model updates (PHP 8.5 target)
|
|
123
|
+
|
|
124
|
+
Models are readonly; on a PHP 8.5 target use clone-with instead of mutation:
|
|
125
|
+
|
|
126
|
+
```php
|
|
127
|
+
$renamed = clone($task, ['title' => 'New title']);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Common Mistakes
|
|
131
|
+
|
|
132
|
+
### CRITICAL Missing serializer support packages
|
|
133
|
+
|
|
134
|
+
Wrong:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
composer require symfony/http-client symfony/serializer
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Correct:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
composer require symfony/http-client symfony/serializer \
|
|
144
|
+
symfony/property-info symfony/property-access phpdocumentor/reflection-docblock
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
The generated `SerializerFactory` wires `PhpDocExtractor` so constructor `@param Pet[]` docblocks drive collection hydration. Without `phpdocumentor/reflection-docblock`, nested arrays of models arrive as raw arrays and property reads fatal on type mismatch.
|
|
148
|
+
|
|
149
|
+
Source: src/modes/php/runtime.ts SerializerFactory
|
|
150
|
+
|
|
151
|
+
### HIGH Wrong serializer version for a PHP 8.0 target
|
|
152
|
+
|
|
153
|
+
Wrong:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
# --php-version 8.0 output with serializer 7.x
|
|
157
|
+
composer require symfony/serializer:^7.0
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Correct:
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
# 8.0-target output uses AnnotationLoader, removed in serializer 7
|
|
164
|
+
composer require symfony/serializer:"^5.4|^6.0"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Code generated with `--php-version 8.0` imports `AnnotationLoader` and `Serializer\Annotation\SerializedName`, which serializer 7 removed. Targets 8.1+ use `AttributeLoader` and require serializer >= 6.4. Prefer `--php-version 8.1` or newer unless you are stuck on PHP 8.0.
|
|
168
|
+
|
|
169
|
+
Source: src/modes/php/runtime.ts serializerFactoryUses
|
|
170
|
+
|
|
171
|
+
### HIGH Editing the generated files directly
|
|
172
|
+
|
|
173
|
+
Wrong:
|
|
174
|
+
|
|
175
|
+
```php
|
|
176
|
+
// Adding a custom method inside the generated PetstoreClient.php
|
|
177
|
+
final class PetstoreClient
|
|
178
|
+
{
|
|
179
|
+
// ...generated code...
|
|
180
|
+
public function customMethod(): void { /* hand-written */ }
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Correct:
|
|
185
|
+
|
|
186
|
+
```php
|
|
187
|
+
// Wrap or compose in your own class
|
|
188
|
+
final class PetstoreGateway
|
|
189
|
+
{
|
|
190
|
+
public function __construct(private readonly PetstoreClient $api) {}
|
|
191
|
+
|
|
192
|
+
public function customMethod(): void { /* hand-written */ }
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
The generated directory is regenerated from the spec; manual edits are overwritten. The generated client is `final` — compose, don't extend.
|
|
197
|
+
|
|
198
|
+
Source: maintainer interview
|
|
199
|
+
|
|
200
|
+
See also: generate-client/SKILL.md — CLI usage for regenerating the client
|