Skip to content

Commit 68a7f35

Browse files
committed
Note on schema extensions
1 parent 207afd8 commit 68a7f35

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,46 @@ bin/cake bake controller {Name} --theme SwaggerBake
445445
- SwaggerBake has been developed primarily for application/json and application/x-www-form-urlencoded, but does have
446446
some support for application/xml and *should* work with application/vnd.api+json.
447447

448+
SwaggerBake does not document schema associations. If your application includes associations on things like
449+
GET requests, you can easily add them into your swagger documentation through the OpenAPI `allOf` property. Since
450+
SwaggerBake works in conjunction with OpenAPI YAML you can easily add a new schema with this association. Below is an
451+
example of extending an existing City schema to include a Country association.
452+
453+
```yaml
454+
# in your swagger.yml
455+
components:
456+
schemas:
457+
CityExtended:
458+
description: 'City with extended information including Country'
459+
type: object
460+
allOf:
461+
- $ref: '#/components/schemas/City'
462+
- type: object
463+
properties:
464+
country:
465+
$ref: '#/components/schemas/Country'
466+
467+
```
468+
469+
Then in your controller action you'd specify the Schema:
470+
471+
```php
472+
/**
473+
* View method
474+
* @Swag\SwagResponseSchema(refEntity="#/components/schemas/CityExtended")
475+
*/
476+
public function view($id)
477+
{
478+
$this->request->allowMethod('get');
479+
$city = $this->Cities->get($id, ['contain' => ['Countries']]);
480+
$this->set(compact('cities'));
481+
$this->viewBuilder()->setOption('serialize', 'cities');
482+
}
483+
```
484+
485+
The demo application includes this and many other examples of usage. Read more about `oneOf`, `anyOf`, `allOf`, and
486+
`not` in the [OpenAPI 3 documentation](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/).
487+
448488
## Supported Versions
449489

450490
This is built for CakePHP 4.x only. A cake-3.8 option is available, but not supported.

0 commit comments

Comments
 (0)