Skip to content

Commit d2ed729

Browse files
authored
Fix pattern property naming (#21)
1 parent 39d2404 commit d2ed729

File tree

91 files changed

+796
-522
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+796
-522
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
],
1515
"require": {
1616
"ext-json": "*",
17-
"swaggest/json-schema": "^0.12.13",
17+
"swaggest/json-schema": "^0.12.17",
1818
"swaggest/code-builder": "^0.3.1",
1919
"php": ">=5.5.0"
2020
},

composer.lock

+17-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JsonSchema/SchemaBuilder.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,17 @@ private function processObject()
210210
if ($this->schema->patternProperties !== null) {
211211
foreach ($this->schema->patternProperties as $pattern => $property) {
212212
if ($property instanceof Schema) {
213+
$varName = '$' . PhpCode::makePhpName($pattern);
213214
$patternExp = var_export($pattern, true);
214215
$this->result->addSnippet(
215216
$this->copyTo(new SchemaBuilder(
216217
$property,
217-
"\$patternProperty",
218+
$varName,
218219
$this->path . "->patternProperties->{{$pattern}}",
219220
$this->phpBuilder
220221
))->build()
221222
);
222-
$this->result->addSnippet("{$this->varName}->setPatternProperty({$patternExp}, \$patternProperty);\n");
223+
$this->result->addSnippet("{$this->varName}->setPatternProperty({$patternExp}, $varName);\n");
223224
}
224225
}
225226
}

src/PhpCode.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ public static function makePhpName($rawName, $lowerFirst = true)
6868
$phpName = preg_replace("/([^a-zA-Z0-9_]+)/", "_", $rawName);
6969
$phpName = self::toCamelCase($phpName, $lowerFirst);
7070
if (!$phpName) {
71-
$phpName = 'property' . substr(md5($rawName), 0, 6);
71+
$phpName = 'Property' . substr(md5($rawName), 0, 6);
7272
} elseif (is_numeric($phpName[0])) {
73-
$phpName = 'property' . $phpName;
73+
$phpName = 'Property' . $phpName;
74+
}
75+
if ($lowerFirst) {
76+
$phpName[0] = strtolower($phpName[0]);
7477
}
7578
return $phpName;
7679
}

tests/resources/openapi3-schema.json

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
"additionalProperties": false,
5353
"definitions": {
5454
"Reference": {
55+
"not": {
56+
"description": "References are removed from validation because of proactive dereferencing"
57+
},
5558
"type": "object",
5659
"required": [
5760
"$ref"
+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Swagger Petstore",
6+
"license": {
7+
"name": "MIT"
8+
}
9+
},
10+
"servers": [
11+
{
12+
"url": "http://petstore.swagger.io/v1"
13+
}
14+
],
15+
"paths": {
16+
"/pets": {
17+
"get": {
18+
"summary": "List all pets",
19+
"operationId": "listPets",
20+
"tags": [
21+
"pets"
22+
],
23+
"parameters": [
24+
{
25+
"name": "limit",
26+
"in": "query",
27+
"description": "How many items to return at one time (max 100)",
28+
"required": false,
29+
"schema": {
30+
"type": "integer",
31+
"format": "int32"
32+
}
33+
}
34+
],
35+
"responses": {
36+
"200": {
37+
"description": "A paged array of pets",
38+
"headers": {
39+
"x-next": {
40+
"description": "A link to the next page of responses",
41+
"schema": {
42+
"type": "string"
43+
}
44+
}
45+
},
46+
"content": {
47+
"application/json": {
48+
"schema": {
49+
"$ref": "#/components/schemas/Pets"
50+
}
51+
}
52+
}
53+
},
54+
"default": {
55+
"description": "unexpected error",
56+
"content": {
57+
"application/json": {
58+
"schema": {
59+
"$ref": "#/components/schemas/Error"
60+
}
61+
}
62+
}
63+
}
64+
}
65+
},
66+
"post": {
67+
"summary": "Create a pet",
68+
"operationId": "createPets",
69+
"tags": [
70+
"pets"
71+
],
72+
"responses": {
73+
"201": {
74+
"description": "Null response"
75+
},
76+
"default": {
77+
"description": "unexpected error",
78+
"content": {
79+
"application/json": {
80+
"schema": {
81+
"$ref": "#/components/schemas/Error"
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
},
89+
"/pets/{petId}": {
90+
"get": {
91+
"summary": "Info for a specific pet",
92+
"operationId": "showPetById",
93+
"tags": [
94+
"pets"
95+
],
96+
"parameters": [
97+
{
98+
"name": "petId",
99+
"in": "path",
100+
"required": true,
101+
"description": "The id of the pet to retrieve",
102+
"schema": {
103+
"type": "string"
104+
}
105+
}
106+
],
107+
"responses": {
108+
"200": {
109+
"description": "Expected response to a valid request",
110+
"content": {
111+
"application/json": {
112+
"schema": {
113+
"$ref": "#/components/schemas/Pet"
114+
}
115+
}
116+
}
117+
},
118+
"default": {
119+
"description": "unexpected error",
120+
"content": {
121+
"application/json": {
122+
"schema": {
123+
"$ref": "#/components/schemas/Error"
124+
}
125+
}
126+
}
127+
}
128+
}
129+
}
130+
}
131+
},
132+
"components": {
133+
"schemas": {
134+
"Pet": {
135+
"type": "object",
136+
"required": [
137+
"id",
138+
"name"
139+
],
140+
"properties": {
141+
"id": {
142+
"type": "integer",
143+
"format": "int64"
144+
},
145+
"name": {
146+
"type": "string"
147+
},
148+
"tag": {
149+
"type": "string"
150+
}
151+
}
152+
},
153+
"Pets": {
154+
"type": "array",
155+
"items": {
156+
"$ref": "#/components/schemas/Pet"
157+
}
158+
},
159+
"Error": {
160+
"type": "object",
161+
"required": [
162+
"code",
163+
"message"
164+
],
165+
"properties": {
166+
"code": {
167+
"type": "integer",
168+
"format": "int32"
169+
},
170+
"message": {
171+
"type": "string"
172+
}
173+
}
174+
}
175+
}
176+
}
177+
}

tests/src/PHPUnit/JsonSchema/AdvancedTest.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,18 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
437437
{
438438
$ownerSchema->type = 'object';
439439
$ownerSchema->additionalProperties = false;
440-
$patternProperty = new Swaggest\JsonSchema\Schema();
441-
$patternProperty->oneOf[0] = Swaggest\JsonSchema\Schema::null();
442-
$patternProperty->oneOf[1] = Swaggest\JsonSchema\Schema::number();
443-
$patternProperty->oneOf[2] = Swaggest\JsonSchema\Schema::boolean();
444-
$patternProperty->oneOf[3] = Swaggest\JsonSchema\Schema::string();
445-
$patternProperty->oneOf[4] = Swaggest\JsonSchema\Schema::object();
446-
$patternProperty->oneOf[5] = Swaggest\JsonSchema\Schema::arr();
447-
$patternProperty->setFromRef('#/definitions/x');
448-
$ownerSchema->setPatternProperty('^x-', $patternProperty);
449-
$patternProperty = Swaggest\JsonSchema\Schema::string();
450-
$patternProperty->setFromRef('#/definitions/z');
451-
$ownerSchema->setPatternProperty('^zed-', $patternProperty);
440+
$x = new Swaggest\JsonSchema\Schema();
441+
$x->oneOf[0] = Swaggest\JsonSchema\Schema::null();
442+
$x->oneOf[1] = Swaggest\JsonSchema\Schema::number();
443+
$x->oneOf[2] = Swaggest\JsonSchema\Schema::boolean();
444+
$x->oneOf[3] = Swaggest\JsonSchema\Schema::string();
445+
$x->oneOf[4] = Swaggest\JsonSchema\Schema::object();
446+
$x->oneOf[5] = Swaggest\JsonSchema\Schema::arr();
447+
$x->setFromRef('#/definitions/x');
448+
$ownerSchema->setPatternProperty('^x-', $x);
449+
$zed = Swaggest\JsonSchema\Schema::string();
450+
$zed->setFromRef('#/definitions/z');
451+
$ownerSchema->setPatternProperty('^zed-', $zed);
452452
}
453453
454454
/**

0 commit comments

Comments
 (0)