File tree 3 files changed +67
-1
lines changed
3 files changed +67
-1
lines changed Original file line number Diff line number Diff line change @@ -525,4 +525,37 @@ public function getExtensions(): array
525
525
}
526
526
return $ extensions ;
527
527
}
528
+
529
+ /**
530
+ * Returns extension property with `x-` prefix.
531
+ * @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions
532
+ * @param string $key The property key, e.g. 'x-prop' or 'prop' (the 'x-' prefix is added automatically if needed).
533
+ * @return mixed|null The property value, if present.
534
+ * @since 1.8.0
535
+ */
536
+ public function getExtension (string $ key )
537
+ {
538
+ if (strpos ($ key , 'x- ' ) !== 0 ) {
539
+ $ key = 'x- ' . $ key ;
540
+ }
541
+
542
+ return $ this ->_properties [$ key ] ?? null ;
543
+ }
544
+
545
+ /**
546
+ * Set extension property with `x-` prefix.
547
+ * @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions
548
+ * @param string $key The property key, e.g. 'x-prop' or 'prop' (the 'x-' prefix is added automatically if needed).
549
+ * @param mixed $value The property value.
550
+ * @return void
551
+ * @since 1.8.0
552
+ */
553
+ public function setExtension (string $ key , $ value ): void
554
+ {
555
+ if (strpos ($ key , 'x- ' ) !== 0 ) {
556
+ $ key = 'x- ' . $ key ;
557
+ }
558
+
559
+ $ this ->_properties [$ key ] = $ value ;
560
+ }
528
561
}
Original file line number Diff line number Diff line change @@ -37,6 +37,33 @@ public function testWriteJson()
37
37
);
38
38
}
39
39
40
+ public function testWriteJsonExtensions ()
41
+ {
42
+ $ openapi = $ this ->createOpenAPI ();
43
+ $ openapi ->{'x-extra-var ' } = 'foo ' ;
44
+ $ openapi ->setExtension ('x-another-var ' , 'bar ' );
45
+ $ openapi ->setExtension ('short-var ' , 'baz ' );
46
+
47
+ $ json = \cebe \openapi \Writer::writeToJson ($ openapi );
48
+
49
+ $ this ->assertEquals (preg_replace ('~\R~ ' , "\n" , <<<JSON
50
+ {
51
+ "openapi": "3.0.0",
52
+ "info": {
53
+ "title": "Test API",
54
+ "version": "1.0.0"
55
+ },
56
+ "paths": {},
57
+ "x-extra-var": "foo",
58
+ "x-another-var": "bar",
59
+ "x-short-var": "baz"
60
+ }
61
+ JSON
62
+ ),
63
+ $ json
64
+ );
65
+ }
66
+
40
67
public function testWriteJsonMofify ()
41
68
{
42
69
$ openapi = $ this ->createOpenAPI ();
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ public function testRead()
27
27
name: Apache 2.0
28
28
url: https://www.apache.org/licenses/LICENSE-2.0.html
29
29
version: 1.0.1
30
+ x-extra-var: foo
30
31
YAML
31
32
, Info::class);
32
33
@@ -46,6 +47,11 @@ public function testRead()
46
47
$ this ->assertInstanceOf (License::class, $ info ->license );
47
48
$ this ->assertEquals ('Apache 2.0 ' , $ info ->license ->name );
48
49
$ this ->assertEquals ('https://www.apache.org/licenses/LICENSE-2.0.html ' , $ info ->license ->url );
50
+
51
+ $ this ->assertEquals ('foo ' , $ info ->getExtensions ()['x-extra-var ' ]);
52
+ $ this ->assertEquals ('foo ' , $ info ->getExtension ('x-extra-var ' ));
53
+ $ this ->assertEquals ('foo ' , $ info ->getExtension ('extra-var ' ));
54
+ $ this ->assertNull ($ info ->getExtension ('another-var ' ));
49
55
}
50
56
51
57
public function testReadInvalid ()
@@ -117,4 +123,4 @@ public function testReadInvalidLicense()
117
123
$ this ->assertNull ($ info ->contact );
118
124
119
125
}
120
- }
126
+ }
You can’t perform that action at this time.
0 commit comments