Skip to content

Commit 1efaec6

Browse files
committed
refactor: stricter constructors for required properties
1 parent 1cd3709 commit 1efaec6

17 files changed

+302
-117
lines changed

README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,25 @@ Basic example:
4949

5050
use erasys\OpenApi\Spec\v3 as OASv3;
5151

52-
$spec = new OASv3\Document(
53-
new OASv3\Info('My API', '1.0.0', 'My API description', 'My License')
52+
$doc = new OASv3\Document(
53+
new OASv3\Info('My API', '1.0.0', 'My API description'),
54+
[
55+
'/foo/bar' => new OASv3\PathItem(
56+
[
57+
'get' => new OASv3\Operation(
58+
[
59+
'200' => new OASv3\Response('Successful response.'),
60+
'default' => new OASv3\Response('Default error response.'),
61+
]
62+
),
63+
]
64+
),
65+
]
5466
);
5567

56-
$rootPath = new OASv3\PathItem();
57-
$rootPath->get = new OASv3\Operation();
58-
$rootPath->get->responses = [
59-
'200' => new OASv3\Response(['description' => 'successful response'])
60-
];
61-
62-
$spec->paths = ['/' => $rootPath];
63-
64-
$yaml = $spec->toYaml();
65-
$json = $spec->toJson();
66-
$arr = $spec->toArray();
68+
$yaml = $doc->toYaml();
69+
$json = $doc->toJson();
70+
$arr = $doc->toArray();
6771

6872
```
6973

src/Spec/v3/Discriminator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
class Discriminator extends AbstractObject
1616
{
17-
1817
/**
1918
* REQUIRED. The name of the property in the payload that will hold the discriminator value.
2019
*
@@ -28,4 +27,15 @@ class Discriminator extends AbstractObject
2827
* @var string[] array<string, string>
2928
*/
3029
public $mapping;
30+
31+
/**
32+
* @param string $propertyName
33+
* @param string[] $mapping
34+
*/
35+
public function __construct(string $propertyName, array $mapping = null)
36+
{
37+
parent::__construct([]);
38+
$this->propertyName = $propertyName;
39+
$this->mapping = $mapping;
40+
}
3141
}

src/Spec/v3/Document.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ class Document extends AbstractObject implements ExtensibleInterface
6565
*/
6666
public $info;
6767

68-
/**
69-
* An array of Server Objects, which provide connectivity information to a target server.
70-
* If the servers property is not provided, or is an empty array,
71-
* the default value would be a Server Object with a url value of /.
72-
*
73-
* @var Server[]
74-
*/
75-
public $servers;
76-
7768
/**
7869
* REQUIRED. The available paths and operations for the API.
7970
* A map between a path and its definition.
@@ -94,6 +85,15 @@ class Document extends AbstractObject implements ExtensibleInterface
9485
*/
9586
public $paths;
9687

88+
/**
89+
* An array of Server Objects, which provide connectivity information to a target server.
90+
* If the servers property is not provided, or is an empty array,
91+
* the default value would be a Server Object with a url value of /.
92+
*
93+
* @var Server[]
94+
*/
95+
public $servers;
96+
9797
/**
9898
* An element to hold various schemas for the specification.
9999
*
@@ -133,13 +133,18 @@ class Document extends AbstractObject implements ExtensibleInterface
133133
public $externalDocs;
134134

135135
/**
136-
* Schema constructor.
137-
*
138-
* @param Info $info
136+
* @param Info $info
137+
* @param PathItem[] $paths
138+
* @param string $openapi
139+
* @param array $additionalProperties
139140
*/
140-
public function __construct(Info $info)
141+
public function __construct(Info $info, array $paths, string $openapi = '3.0.1', array $additionalProperties = [])
141142
{
142-
$this->info = $info;
143+
$this->info = $info;
144+
$this->paths = $paths;
145+
$this->openapi = $openapi;
146+
147+
parent::__construct($additionalProperties);
143148
}
144149

145150
/**

src/Spec/v3/ExternalDocumentation.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
*
88
* @see https://swagger.io/specification/#externalDocumentationObject
99
*/
10-
class ExternalDocumentation extends AbstractObject
10+
class ExternalDocumentation extends AbstractObject implements ExtensibleInterface
1111
{
12+
/**
13+
* REQUIRED. The URL for the target documentation. Value MUST be in the format of a URL.
14+
*
15+
* @var string
16+
*/
17+
public $url;
1218

1319
/**
1420
* A short description of the target documentation. CommonMark syntax MAY be used for rich text representation.
@@ -18,9 +24,14 @@ class ExternalDocumentation extends AbstractObject
1824
public $description;
1925

2026
/**
21-
* REQUIRED. The URL for the target documentation. Value MUST be in the format of a URL.
22-
*
23-
* @var string
27+
* @param string $url
28+
* @param string|null $description
29+
* @param array $additionalProperties
2430
*/
25-
public $url;
31+
public function __construct(string $url, string $description = null, array $additionalProperties = [])
32+
{
33+
parent::__construct($additionalProperties);
34+
$this->url = $url;
35+
$this->description = $description;
36+
}
2637
}

src/Spec/v3/Header.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
*/
1414
class Header extends AbstractParameter
1515
{
16-
17-
// This class has no additional properties
16+
/**
17+
* @param string|null $description
18+
* @param array $additionalProperties
19+
*/
20+
public function __construct(string $description = null, array $additionalProperties = [])
21+
{
22+
parent::__construct($additionalProperties);
23+
$this->description = $description;
24+
}
1825
}

src/Spec/v3/Info.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,58 @@ class Info extends AbstractObject implements ExtensibleInterface
1818
public $title;
1919

2020
/**
21-
* (Optional) A short description of the application. CommonMark syntax MAY be used for rich text representation.
21+
* REQUIRED. The version of this OpenAPI document
22+
* (which is distinct from the OpenAPI Specification version or the API implementation version).
23+
*
24+
*
25+
* @var string
26+
*/
27+
public $version;
28+
29+
/**
30+
* A short description of the application. CommonMark syntax MAY be used for rich text representation.
2231
*
2332
* @var string
2433
*/
2534
public $description;
2635

2736
/**
28-
* (Optional) A URL to the Terms of Service for the API. MUST be in the format of a URL.
37+
* A URL to the Terms of Service for the API. MUST be in the format of a URL.
2938
*
3039
* @var string
3140
*/
3241
public $termsOfService;
3342

3443
/**
35-
* (Optional) The contact information for the exposed API.
44+
* The contact information for the exposed API.
3645
*
3746
* @var Contact
3847
*/
3948
public $contact;
4049

4150
/**
42-
* (Optional) The license information for the exposed API.
51+
* The license information for the exposed API.
4352
*
4453
* @var License
4554
*/
4655
public $license;
4756

48-
/**
49-
* REQUIRED. The version of this OpenAPI document
50-
* (which is distinct from the OpenAPI Specification version or the API implementation version).
51-
*
52-
*
53-
* @var string
54-
*/
55-
public $version;
56-
5757
/**
5858
* @param string $title
5959
* @param string $version
6060
* @param string|null $description
61-
* @param string $licenseName
61+
* @param array $additionalProperties
6262
*/
6363
public function __construct(
6464
string $title,
6565
string $version,
6666
string $description = null,
67-
string $licenseName = 'Proprietary License'
67+
array $additionalProperties = []
6868
) {
69+
parent::__construct($additionalProperties);
70+
6971
$this->title = $title;
7072
$this->version = $version;
7173
$this->description = $description;
72-
$this->license = new License(
73-
[
74-
'name' => $licenseName,
75-
]
76-
);
7774
}
7875
}

src/Spec/v3/License.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ class License extends AbstractObject implements ExtensibleInterface
2323
* @var string
2424
*/
2525
public $url;
26+
27+
/**
28+
* @param string $name
29+
* @param string|null $url
30+
* @param array $additionalProperties
31+
*/
32+
public function __construct(string $name, string $url = null, array $additionalProperties = [])
33+
{
34+
parent::__construct($additionalProperties);
35+
$this->name = $name;
36+
$this->url = $url;
37+
}
2638
}

src/Spec/v3/OauthFlow.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,39 @@ class OauthFlow extends AbstractObject implements ExtensibleInterface
2828
public $tokenUrl;
2929

3030
/**
31-
* The URL to be used for obtaining refresh tokens. This MUST be in the form of a URL.
31+
* REQUIRED. The available scopes for the OAuth2 security scheme. A map between the scope name and a short
32+
* description for it.
3233
*
3334
* Applies To: oauth2
3435
*
35-
* @var string
36+
* @var string[] array<string,string>
3637
*/
37-
public $refreshUrl;
38+
public $scopes;
3839

3940
/**
40-
* REQUIRED. The available scopes for the OAuth2 security scheme. A map between the scope name and a short
41-
* description for it.
41+
* The URL to be used for obtaining refresh tokens. This MUST be in the form of a URL.
4242
*
4343
* Applies To: oauth2
4444
*
45-
* @var string[] array<string,string>
45+
* @var string
4646
*/
47-
public $scopes;
47+
public $refreshUrl;
48+
49+
/**
50+
* @param string $authorizationUrl
51+
* @param string $tokenUrl
52+
* @param string[] $scopes
53+
* @param array $additionalProperties
54+
*/
55+
public function __construct(
56+
string $authorizationUrl,
57+
string $tokenUrl,
58+
array $scopes,
59+
array $additionalProperties = []
60+
) {
61+
parent::__construct($additionalProperties);
62+
$this->authorizationUrl = $authorizationUrl;
63+
$this->tokenUrl = $tokenUrl;
64+
$this->scopes = $scopes;
65+
}
4866
}

0 commit comments

Comments
 (0)