Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
db238f2
add in faq
natsuki-engr Feb 4, 2025
10d4922
OAT to OA
natsuki-engr Feb 11, 2025
c0d15f4
x-tagGroups
natsuki-engr Feb 17, 2025
92e2948
response examples
natsuki-engr Feb 17, 2025
5279f0e
external docs
natsuki-engr Feb 18, 2025
3dac190
union types
natsuki-engr Feb 19, 2025
014f798
security schemes
natsuki-engr Feb 22, 2025
6cf387f
secure endpoint
natsuki-engr Feb 22, 2025
e9c79c4
multiple security schemes
natsuki-engr Mar 3, 2025
7ac5462
file upload
natsuki-engr Mar 28, 2025
5a713f2
add success response description in attribute example
natsuki-engr Apr 15, 2025
9da2add
xml content response
natsuki-engr Apr 16, 2025
f222208
upload multipart/form-data
natsuki-engr Apr 16, 2025
6894ed5
nested structure with @OA\Property
natsuki-engr Apr 25, 2025
7821474
expression of single or list by oneOf
natsuki-engr Apr 25, 2025
b341a18
reusing responses
natsuki-engr May 7, 2025
80f6ff9
callbacks
natsuki-engr May 7, 2025
8c166af
virtual model
natsuki-engr May 7, 2025
b902bb8
class name as type
natsuki-engr May 7, 2025
bf2837a
enums
natsuki-engr May 7, 2025
a918967
multi value query parameter
natsuki-engr May 7, 2025
61b4348
custom response classes
natsuki-engr May 7, 2025
5d37e10
class constants
natsuki-engr May 7, 2025
10b6f3e
fix wrong id
natsuki-engr May 7, 2025
72d01d3
rename wrong file name
natsuki-engr May 7, 2025
f5c6f64
context awareness
natsuki-engr May 7, 2025
7762311
response MediaType and JsonContent
natsuki-engr May 7, 2025
3b3596b
using $ref
natsuki-engr May 7, 2025
40c9a98
custom property
natsuki-engr May 7, 2025
e052848
enum as values
natsuki-engr May 7, 2025
ae962ba
backed enum names as schema
natsuki-engr May 7, 2025
71fd7d0
backed enum values as schema
natsuki-engr May 7, 2025
eb50568
replace singlequote to double quote
natsuki-engr May 7, 2025
d57f1a7
enum as schema
natsuki-engr May 7, 2025
7f824e4
Merge remote-tracking branch 'origin/master' into doc-add-attribute-e…
natsuki-engr May 8, 2025
341c15c
fix errors
natsuki-engr May 9, 2025
64ba8bb
fix syntax error
natsuki-engr May 12, 2025
cac0c6b
fix syntax error
natsuki-engr May 12, 2025
c36bc1c
remove tabs of custom response example
natsuki-engr May 12, 2025
447f406
fix syntax error
natsuki-engr May 12, 2025
db91eea
move example file into inline
natsuki-engr May 12, 2025
e04d06b
add referred schema
natsuki-engr May 12, 2025
d543878
add use statement
natsuki-engr May 13, 2025
c6390d5
Merge remote-tracking branch 'origin/master' into doc-add-attribute-e…
natsuki-engr May 13, 2025
75d06b7
add docs/snippets to CS
natsuki-engr May 13, 2025
66d7e95
apply style of code_sniffer to docs/snippets
natsuki-engr May 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
->path('src')->name('*.php')
->path('tools')->name('*.php')
->path('docs/examples')->name('*.php')
->path('docs/snippets')->name('*.php')
->path('tests')->name('*.php')
->filter(function (\SplFileInfo $file) {
return
Expand Down
276 changes: 139 additions & 137 deletions docs/guide/common-techniques.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,19 @@ a structural element (`class`, `method`, `parameter` or `enum`)
This means in a lot of cases it is not necessary to explicitly document all details.

**Example**
```php
<?php

/**
* @OA\Schema()
*/
class Product {
<codeblock id="context-awareness">
<template v-slot:at>

/**
* The product name,
* @var string
* @OA\Property()
*/
public $name;
}
```
<<< @/snippets/guide/common-techniques/context_awareness_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/context_awareness_an.php

</template>
</codeblock>

**Results in**
```yaml
Expand All @@ -52,48 +49,50 @@ components:

**As if you'd written**

```php
/**
* The product name
* @var string
*
* @OA\Property(
* property="name",
* type="string",
* description="The product name"
* )
*/
public $name;
```
<codeblock id="explicit-context">
<template v-slot:at>

<<< @/snippets/guide/common-techniques/explicit_context_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/explicit_context_an.php

</template>
</codeblock>

## Response media type

The `@OA\MediaType` is used to describe the content:

```php
/**
* @OA\Response(
* response=200,
* description="successful operation",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/User"),
* )
* ),
*/
```
<codeblock id="response-media-type">
<template v-slot:at>

<<< @/snippets/guide/common-techniques/response_media_type_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/response_media_type_an.php

</template>
</codeblock>

But because most API requests and responses are JSON, the `@OA\JsonContent` allows you to simplify this by writing:

```php
/**
* @OA\Response(
* response=200,
* description="successful operation",
* @OA\JsonContent(ref="#/components/schemas/User"),
* )
*/
```
<codeblock id="response-json-content">
<template v-slot:at>

<<< @/snippets/guide/common-techniques/response_json_content_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/response_json_content_an.php

</template>
</codeblock>

During processing the `@OA\JsonContent` unfolds to `@OA\MediaType( mediaType="application/json", @OA\Schema(...)`
and will generate the same output.
Expand All @@ -103,6 +102,21 @@ and will generate the same output.
It's quite common that endpoints have some overlap in either their request or response data.
To keep things DRY (Don't Repeat Yourself) the specification allows reusing components using `$ref`'s

<codeblock id="using-references">
<template v-slot:at>

```php
#[OA\Schema(
schema: 'product_id',
type: 'integer',
format: 'int64',
description: 'The unique identifier of a product in our catalog',
)]
```

</template>
<template v-slot:an>

```php
/**
* @OA\Schema(
Expand All @@ -114,6 +128,9 @@ To keep things DRY (Don't Repeat Yourself) the specification allows reusing comp
*/
```

</template>
</codeblock>

**Results in:**

```yaml
Expand All @@ -128,13 +145,27 @@ components:

This doesn't do anything by itself, but now you can reference this fragment by its path in the document tree `#/components/schemas/product_id`

<codeblock id="refer-to-$ref">
<template v-slot:at>

```php
#[OA\Property(ref: "#/components/schemas/product_id")]
public $id;
```

</template>
<template v-slot:an>

```php
/**
* @OA\Property(ref="#/components/schemas/product_id")
*/
public $id;
```

</template>
</codeblock>

::: info Examples
There are more uses cases on how to use refs in the [using-refs example](https://github.com/zircote/swagger-php/tree/master/examples/specs/using-refs).
:::
Expand All @@ -153,24 +184,18 @@ The specification allows for [custom properties](http://swagger.io/specification
as long as they start with "x-". Therefore, all swagger-php annotations have an `x` property which accepts an array (map)
and will unfold into "x-" properties.

```php
/**
* @OA\Info(
* title="Example",
* version="1.0.0",
* x={
* "some-name": "a-value",
* "another": 2,
* "complex-type": {
* "supported":{
* {"version": "1.0", "level": "baseapi"},
* {"version": "2.1", "level": "fullapi"},
* }
* }
* }
* )
*/
```
<codeblock id="custom-property">
<template v-slot:at>

<<< @/snippets/guide/common-techniques/custom_property_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/custom_property_an.php

</template>
</codeblock>

**Results in:**

Expand Down Expand Up @@ -199,23 +224,18 @@ Enum cases can be used as value in an `enum` list just like a `string`, `integer

**Basic enum:**

```php
use OpenApi\Attributes as OAT;

enum Suit
{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

class Model
{
#[OAT\Property(enum: [Suit::Hearts, Suit::Diamonds])]
protected array $someSuits;
}
```
<codeblock id="enum-as-values">
<template v-slot:at>

<<< @/snippets/guide/common-techniques/enum_as_values_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/enum_as_values_an.php

</template>
</codeblock>

**Results in:**

Expand All @@ -242,24 +262,18 @@ If the enum is a backed enum, the case backing value is used instead of the name

The simples way of using enums is to annotate them as `Schema`. This allows you to reference them like any other schema in your spec.

```php
use OpenApi\Attributes as OAT;

#[OAT\Schema()]
enum Colour
{
case GREEN;
case BLUE;
case RED;
}

#[OAT\Schema()]
class Product
{
#[OAT\Property()]
public Colour $colour;
}
```
<codeblock id="enum-as-schema">
<template v-slot:at>

<<< @/snippets/guide/common-techniques/enum_as_schema_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/enum_as_schema_an.php

</template>
</codeblock>

**Results in:**

Expand Down Expand Up @@ -288,24 +302,18 @@ For backed enums there exist two rules that determine whether the name or backin

**Using the name of a backed enum:**

```php
use OpenApi\Attributes as OAT;

#[OAT\Schema()]
enum Colour: int
{
case GREEN = 1;
case BLUE = 2;
case RED = 3;
}

#[OAT\Schema()]
class Product
{
#[OAT\Property()]
public Colour $colour;
}
```
<codeblock id="backed-enum-names-as-schema">
<template v-slot:at>

<<< @/snippets/guide/common-techniques/backed_enum_names_as_schema_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/backed_enum_names_as_schema_an.php

</template>
</codeblock>

**Results in:**

Expand All @@ -328,24 +336,18 @@ components:

**Using the backing value:**

```php
use OpenApi\Attributes as OAT;

#[OAT\Schema(type: 'integer')]
enum Colour: int
{
case GREEN = 1;
case BLUE = 2;
case RED = 3;
}

#[OAT\Schema()]
class Product
{
#[OAT\Property()]
public Colour $colour;
}
```
<codeblock id="backed-enum-values-as-schema">
<template v-slot:at>

<<< @/snippets/guide/common-techniques/backed_enum_values_as_schema_at.php

</template>
<template v-slot:an>

<<< @/snippets/guide/common-techniques/backed_enum_values_as_schema_an.php

</template>
</codeblock>

**Results in:**

Expand Down
Loading