Skip to content

Commit 309bd65

Browse files
committed
refactor: Use convenience methods
1 parent 995b996 commit 309bd65

29 files changed

Lines changed: 99 additions & 88 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ $openApi = OpenApi::create()
5555
->operationId('users.show')
5656
->tags('Users')
5757
->responses(
58-
Response::ok()->content(MediaType::json(Reference::to('#/components/schemas/User'))),
58+
Response::ok()->content(MediaType::json(Reference::schema('User'))),
5959
Response::notFound(),
6060
),
6161
),

docs/openapi/components.mdx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ $components = Components::create()
6161
));
6262
```
6363

64-
Reference schemas anywhere with `Reference::to()`:
64+
Reference schemas anywhere with typed `Reference` shortcuts:
6565

6666
```php
6767
use Cortex\OpenApi\Objects\Reference;
6868

6969
// In a MediaType
70-
MediaType::json(Reference::to('#/components/schemas/User'))
70+
MediaType::json(Reference::schema('User'))
7171

7272
// In a Parameter schema slot
73-
Parameter::query('filter', Reference::to('#/components/schemas/Filter'))
73+
Parameter::query('filter', Reference::schema('Filter'))
7474

7575
// In a composed schema (allOf/oneOf/anyOf)
7676
Schema::object()->allOf(
77-
Reference::to('#/components/schemas/BaseEntity'),
77+
Reference::schema('BaseEntity'),
7878
Schema::object()->properties(Schema::string('title')),
7979
)
8080
```
@@ -112,15 +112,15 @@ use Cortex\OpenApi\Objects\PathItem;
112112

113113
PathItem::create('/users/{userId}/articles/{slug}')
114114
->parameters(
115-
Reference::to('#/components/parameters/UserId'),
116-
Reference::to('#/components/parameters/ArticleSlug'),
115+
Reference::parameter('UserId'),
116+
Reference::parameter('ArticleSlug'),
117117
)
118118
->operations(
119119
Operation::get()
120120
->operationId('users.articles.show')
121121
->parameters(
122-
Reference::to('#/components/parameters/PageSize'),
123-
Reference::to('#/components/parameters/PageNumber'),
122+
Reference::parameter('PageSize'),
123+
Reference::parameter('PageNumber'),
124124
),
125125
);
126126
```
@@ -133,7 +133,7 @@ Centralize common error responses to avoid repeating them on every operation:
133133
use Cortex\OpenApi\Objects\Response;
134134
use Cortex\OpenApi\Objects\MediaType;
135135

136-
$errorSchema = Reference::to('#/components/schemas/Error');
136+
$errorSchema = Reference::schema('Error');
137137

138138
$components = Components::create()
139139
->response('BadRequest',
@@ -158,9 +158,9 @@ Reference them from operations using `Response::ref()`:
158158
```php
159159
Operation::get()
160160
->responses(
161-
Response::ok()->content(MediaType::json(Reference::to('#/components/schemas/User'))),
162-
Response::ref('#/components/responses/NotFound'),
163-
Response::ref('#/components/responses/Unauthorized'),
161+
Response::ok()->content(MediaType::json(Reference::schema('User'))),
162+
Response::ref('NotFound'),
163+
Response::ref('Unauthorized'),
164164
);
165165
```
166166

@@ -175,17 +175,17 @@ $components = Components::create()
175175
->requestBody('CreateUser',
176176
RequestBody::create()
177177
->required(true)
178-
->content(MediaType::json(Reference::to('#/components/schemas/UserInput'))),
178+
->content(MediaType::json(Reference::schema('UserInput'))),
179179
)
180180
->requestBody('UpdateUser',
181181
RequestBody::create()
182182
->required(true)
183-
->content(MediaType::json(Reference::to('#/components/schemas/UserPatch'))),
183+
->content(MediaType::json(Reference::schema('UserPatch'))),
184184
);
185185

186186
// Reference
187-
Operation::post()->requestBody(RequestBody::ref('#/components/requestBodies/CreateUser'));
188-
Operation::patch()->requestBody(RequestBody::ref('#/components/requestBodies/UpdateUser'));
187+
Operation::post()->requestBody(RequestBody::ref('CreateUser'));
188+
Operation::patch()->requestBody(RequestBody::ref('UpdateUser'));
189189
```
190190

191191
## Security Schemes
@@ -249,8 +249,8 @@ Reference headers in a response's `->headers()` array:
249249

250250
```php
251251
Response::ok()->headers([
252-
'X-Request-Id' => Reference::to('#/components/headers/X-Request-Id'),
253-
'X-RateLimit-Remaining' => Reference::to('#/components/headers/X-RateLimit-Remaining'),
252+
'X-Request-Id' => Reference::header('X-Request-Id'),
253+
'X-RateLimit-Remaining' => Reference::header('X-RateLimit-Remaining'),
254254
])
255255
```
256256

@@ -283,7 +283,7 @@ $components = Components::create()
283283
->operationId('webhook.user.event')
284284
->requestBody(
285285
RequestBody::create()
286-
->content(MediaType::json(Reference::to('#/components/schemas/UserEvent'))),
286+
->content(MediaType::json(Reference::schema('UserEvent'))),
287287
)
288288
->responses(Response::ok()),
289289
),
@@ -328,12 +328,12 @@ $components = Components::create()
328328
// Shared responses
329329
->response('NotFound',
330330
Response::notFound()->content(
331-
MediaType::json(Reference::to('#/components/schemas/Error')),
331+
MediaType::json(Reference::schema('Error')),
332332
),
333333
)
334334
->response('Unauthorized',
335335
Response::unauthorized()->content(
336-
MediaType::json(Reference::to('#/components/schemas/Error')),
336+
MediaType::json(Reference::schema('Error')),
337337
),
338338
)
339339

docs/openapi/installation.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ icon: 'terminal'
77
## Requirements
88

99
- PHP 8.3+
10-
- Composer for dependency management
1110

1211
## Installation
1312

docs/openapi/quickstart.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ icon: 'rocket'
6060
</Step>
6161

6262
<Step title="Define Paths and Operations">
63-
Describe your API endpoints with `PathItem` and `Operation`. Use `Reference::to()` to point at component schemas rather than embedding them repeatedly.
63+
Describe your API endpoints with `PathItem` and `Operation`. Use typed `Reference` shortcuts to point at component schemas rather than embedding them repeatedly.
6464

6565
```php
6666
use Cortex\OpenApi\Objects\PathItem;
@@ -83,10 +83,10 @@ icon: 'rocket'
8383
->responses(
8484
Response::ok()
8585
->description('A paged array of pets')
86-
->content(MediaType::json(Reference::to('#/components/schemas/Pet'))),
86+
->content(MediaType::json(Reference::schema('Pet'))),
8787
Response::default()
8888
->description('Unexpected error')
89-
->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
89+
->content(MediaType::json(Reference::schema('Error'))),
9090
),
9191

9292
Operation::post()
@@ -95,23 +95,23 @@ icon: 'rocket'
9595
->requestBody(
9696
RequestBody::create()
9797
->required(true)
98-
->content(MediaType::json(Reference::to('#/components/schemas/Pet'))),
98+
->content(MediaType::json(Reference::schema('Pet'))),
9999
)
100100
->responses(
101101
Response::created()
102-
->content(MediaType::json(Reference::to('#/components/schemas/Pet'))),
102+
->content(MediaType::json(Reference::schema('Pet'))),
103103
),
104104
),
105105

106106
PathItem::create('/pets/{petId}')
107-
->parameters(Reference::to('#/components/parameters/PetId'))
107+
->parameters(Reference::parameter('PetId'))
108108
->operations(
109109
Operation::get()
110110
->operationId('showPetById')
111111
->tags('pets')
112112
->responses(
113-
Response::ok()->content(MediaType::json(Reference::to('#/components/schemas/Pet'))),
114-
Response::notFound()->content(MediaType::json(Reference::to('#/components/schemas/Error'))),
113+
Response::ok()->content(MediaType::json(Reference::schema('Pet'))),
114+
Response::notFound()->content(MediaType::json(Reference::schema('Error'))),
115115
),
116116
),
117117
];
@@ -260,7 +260,7 @@ Most objects provide named constructors so you rarely need `new`. Here's a quick
260260

261261
## Working with References
262262

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.
263+
Typed named constructors on `Reference` build the correct `$ref` pointer automatically — no need to write out the full path. Use `Reference::to()` as an escape hatch when you need to reference a path outside the standard component buckets.
264264

265265
```php
266266
use Cortex\OpenApi\Objects\Reference;
@@ -280,9 +280,9 @@ Reference::pathItem('LegacyPets') // #/components/pathItems/LegacyPets
280280
Reference::to('#/components/schemas/Pet')
281281

282282
// Shortcut on the target class (equivalent, and communicates intent)
283-
Response::ref('#/components/responses/NotFound')
284-
Parameter::ref('#/components/parameters/PetId')
285-
RequestBody::ref('#/components/requestBodies/CreateUser')
283+
Response::ref('NotFound')
284+
Parameter::ref('PetId')
285+
RequestBody::ref('CreateUser')
286286
```
287287

288288
## Vendor Extensions

docs/openapi/request-bodies.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use Cortex\OpenApi\Objects\Reference;
7474
MediaType::json(Schema::object()->properties(...))
7575

7676
// $ref to a registered component
77-
MediaType::json(Reference::to('#/components/schemas/User'))
77+
MediaType::json(Reference::schema('User'))
7878

7979
// Raw PHP array — escape hatch for hand-crafted schemas
8080
MediaType::json(['type' => 'string', 'format' => 'binary'])
@@ -92,8 +92,8 @@ A single `RequestBody` can declare multiple accepted formats. The client sends t
9292
RequestBody::create()
9393
->required(true)
9494
->content(
95-
MediaType::json(Reference::to('#/components/schemas/Article')),
96-
MediaType::xml(Reference::to('#/components/schemas/Article')),
95+
MediaType::json(Reference::schema('Article')),
96+
MediaType::xml(Reference::schema('Article')),
9797
)
9898
```
9999

@@ -223,7 +223,7 @@ $components = Components::create()
223223
// Reference from an operation
224224
Operation::post()
225225
->operationId('articles.create')
226-
->requestBody(RequestBody::ref('#/components/requestBodies/CreateArticle'));
226+
->requestBody(RequestBody::ref('CreateArticle'));
227227
```
228228

229229
## Complete Example

docs/openapi/responses.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Header::create()
154154
use Cortex\OpenApi\Objects\Link;
155155

156156
Response::created()
157-
->content(MediaType::json(Reference::to('#/components/schemas/Article')))
157+
->content(MediaType::json(Reference::schema('Article')))
158158
->links([
159159
'GetArticleById' => Link::create()
160160
->operationId('articles.show')
@@ -199,8 +199,8 @@ $components = Components::create()
199199
Operation::get()
200200
->responses(
201201
Response::ok()->json($schema),
202-
Response::ref('#/components/responses/NotFound'),
203-
Response::ref('#/components/responses/Unauthorized'),
202+
Response::ref('NotFound'),
203+
Response::ref('Unauthorized'),
204204
);
205205
```
206206

docs/openapi/webhooks-and-callbacks.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ $components = Components::create()
128128
->schema('EventEnvelope', $eventEnvelope)
129129
->schema('UserCreatedPayload', Schema::object()
130130
->allOf(
131-
Reference::to('#/components/schemas/EventEnvelope'),
131+
Reference::schema('EventEnvelope'),
132132
Schema::object()->properties(
133133
Schema::object('data')->properties(
134134
Schema::integer('id')->required(),
@@ -138,7 +138,7 @@ $components = Components::create()
138138
))
139139
->schema('OrderPlacedPayload', Schema::object()
140140
->allOf(
141-
Reference::to('#/components/schemas/EventEnvelope'),
141+
Reference::schema('EventEnvelope'),
142142
Schema::object()->properties(
143143
Schema::object('data')->properties(
144144
Schema::integer('orderId')->required(),
@@ -153,7 +153,7 @@ $doc->components($components)->webhooks([
153153
->operationId('webhook.user.created')
154154
->requestBody(
155155
RequestBody::create()->content(
156-
MediaType::json(Reference::to('#/components/schemas/UserCreatedPayload')),
156+
MediaType::json(Reference::schema('UserCreatedPayload')),
157157
),
158158
)
159159
->responses(Response::ok()),
@@ -232,7 +232,7 @@ $components = Components::create()
232232
Operation::post()
233233
->requestBody(
234234
RequestBody::create()
235-
->content(MediaType::json(Reference::to('#/components/schemas/Event'))),
235+
->content(MediaType::json(Reference::schema('Event'))),
236236
)
237237
->responses(Response::ok()),
238238
),
@@ -243,7 +243,7 @@ $components = Components::create()
243243
Operation::post()
244244
->operationId('hooks.subscribe')
245245
->callbacks([
246-
'onEvent' => Callback::ref('#/components/callbacks/EventWebhook'),
246+
'onEvent' => Callback::ref('EventWebhook'),
247247
]);
248248
```
249249

@@ -283,7 +283,7 @@ $doc = OpenApi::create()
283283
Operation::post()
284284
->requestBody(
285285
RequestBody::create()->content(
286-
MediaType::json(Reference::to('#/components/schemas/Event')),
286+
MediaType::json(Reference::schema('Event')),
287287
),
288288
)
289289
->responses(Response::ok()),
@@ -299,7 +299,7 @@ $doc = OpenApi::create()
299299
->summary('Payment Succeeded')
300300
->requestBody(
301301
RequestBody::create()->content(
302-
MediaType::json(Reference::to('#/components/schemas/Event')),
302+
MediaType::json(Reference::schema('Event')),
303303
),
304304
)
305305
->responses(Response::ok()),
@@ -325,7 +325,7 @@ $doc = OpenApi::create()
325325
)
326326
->responses(Response::created())
327327
->callbacks([
328-
'onEvent' => Callback::ref('#/components/callbacks/GenericWebhook'),
328+
'onEvent' => Callback::ref('GenericWebhook'),
329329
]),
330330
),
331331
);

src/Objects/Callback.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static function create(): self
2222
return new self();
2323
}
2424

25-
public static function ref(string $pointer): Reference
25+
public static function ref(string $name, ?string $summary = null, ?string $description = null): Reference
2626
{
27-
return Reference::to($pointer);
27+
return Reference::callback($name, $summary, $description);
2828
}
2929

3030
public function expression(string $runtimeExpression, PathItem $pathItem): self

src/Objects/Example.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static function create(): self
2929
return new self();
3030
}
3131

32-
public static function ref(string $pointer): Reference
32+
public static function ref(string $name, ?string $summary = null, ?string $description = null): Reference
3333
{
34-
return Reference::to($pointer);
34+
return Reference::example($name, $summary, $description);
3535
}
3636

3737
public function summary(?string $summary): self

src/Objects/Header.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public static function create(): self
5454
return new self();
5555
}
5656

57-
public static function ref(string $pointer): Reference
57+
public static function ref(string $name, ?string $summary = null, ?string $description = null): Reference
5858
{
59-
return Reference::to($pointer);
59+
return Reference::header($name, $summary, $description);
6060
}
6161

6262
public function description(?string $description): self

0 commit comments

Comments
 (0)