Skip to content

Commit c8ff08a

Browse files
committed
docs: update all docs for Info::create() signature, json() shortcuts, path(), Reference typed constructors, Style enum, and errors()
Made-with: Cursor
1 parent b435b9a commit c8ff08a

8 files changed

Lines changed: 116 additions & 69 deletions

docs/openapi/info-and-metadata.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use Cortex\OpenApi\Objects\Info;
1515
use Cortex\OpenApi\Objects\Contact;
1616
use Cortex\OpenApi\Objects\License;
1717

18-
$info = Info::create()
19-
->title('Payments API')
20-
->version('2.1.0')
18+
$info = Info::create('Payments API', '2.1.0')
2119
->summary('Handles payment processing and refunds')
2220
->description('Full description with **Markdown** support.')
2321
->termsOfService('https://example.com/terms')
@@ -208,9 +206,7 @@ use Cortex\OpenApi\Objects\ExternalDocs;
208206

209207
$doc = OpenApi::create()
210208
->info(
211-
Info::create()
212-
->title('Acme Platform API')
213-
->version('3.0.0')
209+
Info::create('Acme Platform API', '3.0.0')
214210
->summary('Powers the Acme product suite')
215211
->description(<<<'MD'
216212
The Acme Platform API provides programmatic access to all Acme services.

docs/openapi/introduction.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ $userSchema = Schema::object()->properties(
9393
);
9494

9595
$doc = OpenApi::create()
96-
->info(Info::create()->title('Users API')->version('1.0.0'))
96+
->info(Info::create('Users API', '1.0.0'))
9797
->servers(Server::create('https://api.example.com'))
9898
->paths(
9999
PathItem::create('/users')->operations(

docs/openapi/paths-and-operations.mdx

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,23 @@ PathItem::create('/articles')
1919
);
2020
```
2121

22-
The path is registered as the key in the document's `paths` map when you pass it to `OpenApi::paths()`.
22+
Pass path items to `OpenApi::paths()` (auto-keyed from the path string) or add them one at a time with `OpenApi::path()`:
23+
24+
```php
25+
use Cortex\OpenApi\OpenApi;
26+
use Cortex\OpenApi\Objects\Reference;
27+
28+
// Variadics — auto-keys from PathItem::getPath()
29+
OpenApi::create()->paths(
30+
PathItem::create('/users'),
31+
PathItem::create('/users/{id}'),
32+
);
33+
34+
// Fluent single-add — also accepts a Reference to a path item component
35+
OpenApi::create()
36+
->path('/users', PathItem::create('/users'))
37+
->path('/legacy', Reference::pathItem('LegacyUsers'));
38+
```
2339

2440
### Path-Level Metadata
2541

@@ -174,31 +190,33 @@ Parameter::query('filter', Schema::string())
174190

175191
### Serialization Style
176192

177-
Control how arrays and objects are serialized in query strings using `->style()` and `->explode()`:
193+
Control how arrays and objects are serialized in query strings using `->style()` and `->explode()`. Pass the `Style` enum or a plain string.
178194

179195
<Tabs>
180196
<Tab title="Form (default for query)">
181197
```php
198+
use Cortex\OpenApi\Enums\Style;
199+
182200
// Default: ?color=blue&color=red (explode=true)
183201
Parameter::query('color', Schema::array()->items(Schema::string()))
184-
->style('form')
202+
->style(Style::Form)
185203
->explode()
186204

187205
// ?color=blue,red (explode=false)
188206
Parameter::query('color', Schema::array()->items(Schema::string()))
189-
->style('form')
207+
->style(Style::Form)
190208
->explode(false)
191209
```
192210
</Tab>
193211
<Tab title="Space / Pipe Delimited">
194212
```php
195213
// ?color=blue%20red
196214
Parameter::query('color', Schema::array()->items(Schema::string()))
197-
->style('spaceDelimited')
215+
->style(Style::SpaceDelimited)
198216

199217
// ?color=blue|red
200218
Parameter::query('color', Schema::array()->items(Schema::string()))
201-
->style('pipeDelimited')
219+
->style(Style::PipeDelimited)
202220
```
203221
</Tab>
204222
<Tab title="Deep Object">
@@ -207,16 +225,16 @@ Control how arrays and objects are serialized in query strings using `->style()`
207225
Parameter::query('filter', Schema::object()->properties(
208226
Schema::string('status'),
209227
Schema::string('type'),
210-
))->style('deepObject')
228+
))->style(Style::DeepObject)
211229
```
212230
</Tab>
213231
<Tab title="Path Matrix / Label">
214232
```php
215233
// ;color=blue (matrix)
216-
Parameter::path('color', Schema::string())->style('matrix')
234+
Parameter::path('color', Schema::string())->style(Style::Matrix)
217235

218236
// .blue (label)
219-
Parameter::path('color', Schema::string())->style('label')
237+
Parameter::path('color', Schema::string())->style(Style::Label)
220238
```
221239
</Tab>
222240
</Tabs>
@@ -247,9 +265,8 @@ use Cortex\JsonSchema\Schema;
247265
use Cortex\OpenApi\Objects\PathItem;
248266
use Cortex\OpenApi\Objects\Operation;
249267
use Cortex\OpenApi\Objects\Parameter;
250-
use Cortex\OpenApi\Objects\Response;
251-
use Cortex\OpenApi\Objects\MediaType;
252268
use Cortex\OpenApi\Objects\RequestBody;
269+
use Cortex\OpenApi\Objects\Response;
253270
use Cortex\OpenApi\Objects\Reference;
254271
use Cortex\OpenApi\Objects\ExternalDocs;
255272

@@ -266,8 +283,8 @@ PathItem::create('/articles/{slug}')
266283
->summary('Get an article')
267284
->externalDocs(ExternalDocs::create('https://docs.example.com/articles'))
268285
->responses(
269-
Response::ok()->content(MediaType::json(Reference::to('#/components/schemas/Article'))),
270-
Response::notFound()->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
286+
Response::ok()->json(Reference::schema('Article')),
287+
Response::notFound()->json(Reference::schema('Error')),
271288
),
272289

273290
Operation::patch()
@@ -276,13 +293,13 @@ PathItem::create('/articles/{slug}')
276293
->summary('Update an article')
277294
->requestBody(
278295
RequestBody::create()
279-
->required(true)
280-
->content(MediaType::json(Reference::to('#/components/schemas/ArticleUpdate'))),
296+
->required()
297+
->json(Reference::schema('ArticleUpdate')),
281298
)
282299
->responses(
283-
Response::ok()->content(MediaType::json(Reference::to('#/components/schemas/Article'))),
284-
Response::badRequest()->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
285-
Response::notFound()->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
300+
Response::ok()->json(Reference::schema('Article')),
301+
Response::badRequest()->json(Reference::schema('Error')),
302+
Response::notFound()->json(Reference::schema('Error')),
286303
),
287304

288305
Operation::delete()
@@ -291,7 +308,7 @@ PathItem::create('/articles/{slug}')
291308
->summary('Delete an article')
292309
->responses(
293310
Response::noContent(),
294-
Response::notFound()->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
311+
Response::notFound()->json(Reference::schema('Error')),
295312
),
296313
);
297314
```

docs/openapi/quickstart.mdx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ icon: 'rocket'
130130

131131
$doc = OpenApi::create()
132132
->info(
133-
Info::create()
134-
->title('Petstore API')
135-
->version('1.0.0')
133+
Info::create('Petstore API', '1.0.0')
136134
->description('A sample Petstore API demonstrating cortexphp/openapi.'),
137135
)
138136
->servers(Server::create('https://petstore.example.com/v1'))
@@ -157,7 +155,8 @@ icon: 'rocket'
157155
try {
158156
$doc->validate();
159157
} catch (ValidationException $e) {
160-
echo $e->getMessage();
158+
echo $e->getMessage(); // human-readable summary
159+
print_r($e->errors()); // structured array of errors keyed by JSON pointer
161160
exit(1);
162161
}
163162

@@ -261,15 +260,24 @@ Most objects provide named constructors so you rarely need `new`. Here's a quick
261260

262261
## Working with References
263262

264-
`Reference::to()` creates a `$ref` to any component registered in `Components`. On the target class itself, `ref()` is a type-safe shortcut.
263+
`Reference::to()` creates a `$ref` to any component registered in `Components`. Typed named constructors on `Reference` build the correct pointer automatically — no need to write out the full path.
265264

266265
```php
267266
use Cortex\OpenApi\Objects\Reference;
268267

269-
// Generic reference — works for anything
268+
// Typed shortcuts — recommended, construct the $ref path automatically
269+
Reference::schema('Pet') // #/components/schemas/Pet
270+
Reference::response('NotFound') // #/components/responses/NotFound
271+
Reference::parameter('PetId') // #/components/parameters/PetId
272+
Reference::requestBody('CreateUser') // #/components/requestBodies/CreateUser
273+
Reference::header('RateLimit') // #/components/headers/RateLimit
274+
Reference::securityScheme('OAuth2') // #/components/securitySchemes/OAuth2
275+
Reference::link('GetPet') // #/components/links/GetPet
276+
Reference::callback('EventWebhook') // #/components/callbacks/EventWebhook
277+
Reference::pathItem('LegacyPets') // #/components/pathItems/LegacyPets
278+
279+
// Generic reference — use when the bucket isn't covered above
270280
Reference::to('#/components/schemas/Pet')
271-
Reference::to('#/components/parameters/PetId')
272-
Reference::to('#/components/responses/NotFound')
273281

274282
// Shortcut on the target class (equivalent, and communicates intent)
275283
Response::ref('#/components/responses/NotFound')
@@ -283,7 +291,7 @@ All objects support OpenAPI specification extensions via `->x()`. Keys are norma
283291

284292
```php
285293
$doc = OpenApi::create()
286-
->info(Info::create()->title('My API')->version('1.0.0'))
294+
->info(Info::create('My API', '1.0.0'))
287295
->x('x-api-id', 'my-service-v1') // prefix already present — used as-is
288296
->x('internal-owner', 'platform'); // prefix added automatically → x-internal-owner
289297

docs/openapi/request-bodies.mdx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,39 @@ icon: 'file-up'
88

99
`RequestBody` describes the payload an operation expects from the client. Attach it to an operation with `->requestBody()`.
1010

11+
The `->json()` shortcut handles the most common case — a required JSON body — in one call:
12+
1113
```php
1214
use Cortex\OpenApi\Objects\RequestBody;
13-
use Cortex\OpenApi\Objects\MediaType;
15+
use Cortex\OpenApi\Objects\Reference;
1416
use Cortex\OpenApi\Objects\Operation;
1517
use Cortex\JsonSchema\Schema;
1618

19+
// Shortcut — sets application/json content in one step
1720
Operation::post()
1821
->operationId('users.create')
1922
->requestBody(
2023
RequestBody::create()
21-
->required(true)
24+
->required()
2225
->description('The new user payload')
23-
->content(
24-
MediaType::json(
25-
Schema::object()->properties(
26-
Schema::string('name')->required(),
27-
Schema::string('email')->format('email')->required(),
28-
),
26+
->json(
27+
Schema::object()->properties(
28+
Schema::string('name')->required(),
29+
Schema::string('email')->format('email')->required(),
2930
),
3031
),
3132
);
33+
34+
// With a component reference
35+
Operation::put()
36+
->operationId('users.update')
37+
->requestBody(
38+
RequestBody::create()->required()->json(Reference::schema('UserInput')),
39+
);
3240
```
3341

42+
For multiple content types or non-JSON bodies, use `->content()` with explicit `MediaType` objects (see [MediaType](#mediatype) below).
43+
3444
### required
3545

3646
Mark the body required when clients must always send it. Optional bodies (the default) are used for partial updates or endpoints where the payload is situational.
@@ -206,8 +216,8 @@ use Cortex\OpenApi\Objects\Reference;
206216
$components = Components::create()
207217
->requestBody('CreateArticle',
208218
RequestBody::create()
209-
->required(true)
210-
->content(MediaType::json(Reference::to('#/components/schemas/ArticleInput'))),
219+
->required()
220+
->json(Reference::schema('ArticleInput')),
211221
);
212222

213223
// Reference from an operation

docs/openapi/responses.mdx

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ icon: 'file-down'
1010

1111
```php
1212
use Cortex\OpenApi\Objects\Response;
13-
use Cortex\OpenApi\Objects\MediaType;
1413
use Cortex\OpenApi\Objects\Reference;
1514

1615
Operation::get()
1716
->operationId('users.show')
1817
->responses(
19-
Response::ok()->content(MediaType::json(Reference::to('#/components/schemas/User'))),
20-
Response::notFound()->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
18+
Response::ok()->json(Reference::schema('User')),
19+
Response::notFound()->json(Reference::schema('Error')),
2120
);
2221
```
2322

@@ -55,10 +54,10 @@ Response::status(418) // 418 I'm a Teapot
5554
```php
5655
Operation::get()
5756
->responses(
58-
Response::ok()->content(MediaType::json($successSchema)),
57+
Response::ok()->json($successSchema),
5958
Response::default()
6059
->description('An unexpected error occurred')
61-
->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
60+
->json(Reference::schema('Error')),
6261
);
6362
```
6463

@@ -68,16 +67,27 @@ Use `Response::default()` to cover all error codes with a single error schema ra
6867

6968
### Response Content
7069

71-
Attach one or more `MediaType` objects to document the response body:
70+
Attach one or more `MediaType` objects to document the response body. The `->json()` shortcut handles the common case of a single JSON content type in one call:
71+
72+
```php
73+
use Cortex\OpenApi\Objects\Reference;
74+
75+
// Shortcut — sets application/json content directly
76+
Response::ok()->json(Reference::schema('Article'))
77+
Response::ok()->json(Schema::object()->properties(Schema::string('id')))
78+
Response::ok()->json() // no schema — emits an empty application/json entry
79+
```
80+
81+
For multiple content types or non-JSON responses, use `->content()` with `MediaType`:
7282

7383
```php
7484
use Cortex\OpenApi\Objects\MediaType;
7585

7686
Response::ok()
7787
->description('The requested resource')
7888
->content(
79-
MediaType::json(Reference::to('#/components/schemas/Article')),
80-
MediaType::xml(Reference::to('#/components/schemas/Article')),
89+
MediaType::json(Reference::schema('Article')),
90+
MediaType::xml(Reference::schema('Article')),
8191
)
8292
```
8393

@@ -179,20 +189,16 @@ use Cortex\OpenApi\Objects\Components;
179189

180190
$components = Components::create()
181191
->response('NotFound',
182-
Response::notFound()->content(
183-
MediaType::json(Reference::to('#/components/schemas/Error')),
184-
),
192+
Response::notFound()->json(Reference::schema('Error')),
185193
)
186194
->response('Unauthorized',
187-
Response::unauthorized()->content(
188-
MediaType::json(Reference::to('#/components/schemas/Error')),
189-
),
195+
Response::unauthorized()->json(Reference::schema('Error')),
190196
);
191197

192198
// Reference from an operation
193199
Operation::get()
194200
->responses(
195-
Response::ok()->content(MediaType::json($schema)),
201+
Response::ok()->json($schema),
196202
Response::ref('#/components/responses/NotFound'),
197203
Response::ref('#/components/responses/Unauthorized'),
198204
);
@@ -231,7 +237,6 @@ Response::ok()
231237
```php
232238
use Cortex\JsonSchema\Schema;
233239
use Cortex\OpenApi\Objects\Response;
234-
use Cortex\OpenApi\Objects\MediaType;
235240
use Cortex\OpenApi\Objects\Header;
236241
use Cortex\OpenApi\Objects\Link;
237242
use Cortex\OpenApi\Objects\Reference;
@@ -253,17 +258,15 @@ Operation::post()
253258
->schema(Schema::string()->format('uri'))
254259
->description('URL of the newly created article'),
255260
])
256-
->content(MediaType::json($articleSchema))
261+
->json($articleSchema)
257262
->links([
258263
'GetArticle' => Link::create()
259264
->operationId('articles.show')
260265
->parameters(['slug' => '$response.body#/slug']),
261266
]),
262267

263-
Response::badRequest()
264-
->content(MediaType::json(Reference::to('#/components/schemas/ValidationError'))),
268+
Response::badRequest()->json(Reference::schema('ValidationError')),
265269

266-
Response::unauthorized()
267-
->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
270+
Response::unauthorized()->json(Reference::schema('Error')),
268271
);
269272
```

0 commit comments

Comments
 (0)