Skip to content

Commit 3413a61

Browse files
authored
Merge pull request #73 from php-api-clients/handle-empty-body-responses-better
Handle empty response bodies better
2 parents 447b9fd + 25b41e2 commit 3413a61

File tree

11 files changed

+503
-869
lines changed

11 files changed

+503
-869
lines changed

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"require": {
2222
"php": "^8.2",
2323
"cebe/php-openapi": "dev-openapi-31 as 1.6.0",
24-
"jawira/case-converter": "^3.4",
25-
"twig/twig": "^3.0",
26-
"nikic/php-parser": "^4.8",
24+
"jawira/case-converter": "^3.5",
25+
"twig/twig": "^3.5",
26+
"nikic/php-parser": "^4.15",
2727
"psr/http-message": "^1.0",
2828
"ringcentral/psr7": "^1.3",
29-
"symfony/yaml": "^5.2",
29+
"symfony/yaml": "^5.4",
3030
"wyrihaximus/composer-update-bin-autoload-path": "^1 || ^1.0.1",
3131
"wyrihaximus/hydrator": "dev-master",
3232
"league/openapi-psr7-validator": "^0.16",
@@ -39,8 +39,9 @@
3939
"ApiClients\\Tools\\OpenApiClientGenerator\\": "src/"
4040
},
4141
"files": [
42-
"external_files/cebe/SpecBaseObject.php",
43-
"external_files/cebe/Schema.php",
42+
"external_files/cebe/JsonReference.php",
43+
"external_files/cebe/Paths.php",
44+
"external_files/cebe/Responses.php",
4445
"external_files/thephpleague/SchemaValidator.php",
4546
"external_files/thephpleague/Type.php"
4647
]

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
5+
* @license https://github.com/cebe/php-openapi/blob/master/LICENSE
6+
*/
7+
8+
namespace cebe\openapi\json;
9+
10+
use JsonSerializable;
11+
12+
/**
13+
* Represents a JSON Reference (IETF draft-pbryan-zyp-json-ref-03)
14+
*
15+
* Includes the URI to another JSON document and the JSON Pointer as
16+
* the fragment section of the URI.
17+
*
18+
* @link https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03
19+
* @see JsonPointer
20+
*/
21+
final class JsonReference implements JsonSerializable
22+
{
23+
/**
24+
* @var string
25+
*/
26+
private $_uri = '';
27+
/**
28+
* @var JsonPointer
29+
*/
30+
private $_pointer;
31+
32+
/**
33+
* Create a JSON Reference instance from a JSON document.
34+
* @param string $json the JSON object, e.g. `{ "$ref": "http://example.com/example.json#/foo/bar" }`.
35+
* @return JsonReference
36+
* @throws MalformedJsonReferenceObjectException
37+
* @throws InvalidJsonPointerSyntaxException if an invalid JSON pointer string is passed as part of the fragment section.
38+
*/
39+
public static function createFromJson(string $json): JsonReference
40+
{
41+
$refObject = json_decode($json, true);
42+
if (!isset($refObject['$ref'])) {
43+
throw new MalformedJsonReferenceObjectException('JSON Reference Object must contain the "$ref" member.');
44+
}
45+
return static::createFromReference($refObject['$ref']);
46+
}
47+
48+
/**
49+
* Create a JSON Reference instance from an URI and a JSON Pointer.
50+
* If no JSON Pointer is given this will be interpreted as an empty string JSON pointer, which
51+
* references the whole document.
52+
* @param string $uri the URI to the document without a fragment part.
53+
* @param JsonPointer $jsonPointer
54+
* @return JsonReference
55+
*/
56+
public static function createFromUri(string $uri, ?JsonPointer $jsonPointer = null): JsonReference
57+
{
58+
$jsonReference = static::createFromReference($uri);
59+
$jsonReference->_pointer = $jsonPointer ?: new JsonPointer('');
60+
return $jsonReference;
61+
}
62+
63+
/**
64+
* Create a JSON Reference instance from a reference URI.
65+
* @param string $referenceURI the JSON Reference URI, e.g. `"http://example.com/example.json#/foo/bar"`.
66+
* @return JsonReference
67+
* @throws InvalidJsonPointerSyntaxException if an invalid JSON pointer string is passed as part of the fragment section.
68+
*/
69+
public static function createFromReference(string $referenceURI): JsonReference
70+
{
71+
$jsonReference = new JsonReference();
72+
if (strpos($referenceURI, '#') !== false) {
73+
list($uri, $fragment) = explode('#', $referenceURI, 2);
74+
$jsonReference->_uri = $uri;
75+
$jsonReference->_pointer = new JsonPointer(rawurldecode($fragment));
76+
} else {
77+
$jsonReference->_uri = $referenceURI;
78+
$jsonReference->_pointer = new JsonPointer('');
79+
}
80+
return $jsonReference;
81+
}
82+
83+
private function __construct()
84+
{
85+
}
86+
87+
public function __clone()
88+
{
89+
$this->_pointer = clone $this->_pointer;
90+
}
91+
92+
93+
public function getJsonPointer(): JsonPointer
94+
{
95+
return $this->_pointer;
96+
}
97+
98+
/**
99+
* @return string returns the URI of the referenced JSON document without the fragment (JSON Pointer) part.
100+
*/
101+
public function getDocumentUri(): string
102+
{
103+
return $this->_uri;
104+
}
105+
106+
/**
107+
* @return string returns the JSON Pointer in URI format.
108+
*/
109+
public function getReference(): string
110+
{
111+
// https://tools.ietf.org/html/rfc6901#section-6
112+
// A JSON Pointer can be represented in a URI fragment identifier by
113+
// encoding it into octets using UTF-8 [RFC3629], while percent-encoding
114+
// those characters not allowed by the fragment rule in [RFC3986].
115+
// https://tools.ietf.org/html/rfc3986#page-25
116+
// The characters slash ("/") and question mark ("?") are allowed to
117+
// represent data within the fragment identifier.
118+
// https://tools.ietf.org/html/rfc3986#section-2.4
119+
// the "%7E" can be replaced by "~" without changing its interpretation.
120+
return $this->_uri . '#' . strtr(rawurlencode($this->_pointer->getPointer()), ['%2F' => '/', '%3F' => '?', '%7E' => '~']);
121+
}
122+
123+
/**
124+
* Specify data which should be serialized to JSON
125+
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
126+
* @return mixed data which can be serialized by <b>json_encode</b>,
127+
* which is a value of any type other than a resource.
128+
*/
129+
#[\ReturnTypeWillChange]
130+
public function jsonSerialize()
131+
{
132+
return (object)['$ref' => $this->getReference()];
133+
}
134+
}

external_files/cebe/OpenApi.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)