Skip to content

Commit 39d2404

Browse files
authored
Merge pull request #20 from swaggest/names-from-descriptions
Add option to use title/description as class name part
2 parents a618336 + 4b008df commit 39d2404

14 files changed

+60
-27
lines changed

src/JsonSchema/PhpBuilder.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
use Swaggest\PhpCodeBuilder\Property\Setter;
2727
use Swaggest\PhpCodeBuilder\Types\TypeOf;
2828

29-
/**
30-
* @todo properly process $ref, $schema property names
31-
*/
3229
class PhpBuilder
3330
{
3431
const IMPORT_METHOD_PHPDOC_ID = '::import';
@@ -51,6 +48,12 @@ public function __construct()
5148
public $makeEnumConstants = false;
5249
public $skipSchemaDescriptions = false;
5350

51+
/**
52+
* Use title/description where available instead of keyword in names
53+
* @var bool
54+
*/
55+
public $namesFromDescriptions = false;
56+
5457
/**
5558
* Squish multiple $ref, a PHP class for each $ref will be created if false
5659
* @var bool

src/JsonSchema/SchemaBuilder.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ private function copyTo(SchemaBuilder $schemaBuilder)
309309
return $schemaBuilder;
310310
}
311311

312+
/**
313+
* @throws \Swaggest\JsonSchema\InvalidValue
314+
*/
312315
private function processOther()
313316
{
314317
static $skip = null, $emptySchema = null, $names = null;
@@ -384,6 +387,10 @@ private function processOther()
384387
}
385388
}
386389

390+
/**
391+
* @throws Exception
392+
* @throws \Exception
393+
*/
387394
private function processLogic()
388395
{
389396
$names = Schema::names();
@@ -392,8 +399,11 @@ private function processLogic()
392399
if ($this->schema->$keyword !== null) {
393400
$schema = $this->schema->$keyword;
394401
$path = $this->path . '->' . $keyword;
395-
if ($schema instanceof Schema && !empty($schema->getFromRefs())) {
396-
$path = $schema->getFromRefs()[0];
402+
if ($schema instanceof Schema) {
403+
$path = $this->pathFromDescription($path, $schema);
404+
if (!empty($schema->getFromRefs())) {
405+
$path = $schema->getFromRefs()[0];
406+
}
397407
}
398408
$this->result->addSnippet(
399409
$this->copyTo(new SchemaBuilder(
@@ -411,6 +421,7 @@ private function processLogic()
411421
if ($this->schema->$keyword !== null) {
412422
foreach ($this->schema->$keyword as $index => $schema) {
413423
$path = $this->path . "->{$keyword}[{$index}]";
424+
$path = $this->pathFromDescription($path, $schema);
414425
if ($schema instanceof Schema && !empty($schema->getFromRefs())) {
415426
$path = $schema->getFromRefs()[0];
416427
}
@@ -442,9 +453,27 @@ private function processLogic()
442453
}
443454
}
444455

456+
/**
457+
* @param string $path
458+
* @param Schema $schema
459+
* @return string
460+
*/
461+
private function pathFromDescription($path, $schema)
462+
{
463+
if ($this->phpBuilder->namesFromDescriptions) {
464+
if ($schema->title && strlen($schema->title) < 30) {
465+
$path = $this->path . "->{$schema->title}";
466+
} elseif ($schema->description && strlen($schema->description) < 30) {
467+
$path = $this->path . "->{$schema->description}";
468+
}
469+
}
470+
return $path;
471+
}
472+
445473
/**
446474
* @return PhpCode
447475
* @throws Exception
476+
* @throws \Swaggest\JsonSchema\InvalidValue
448477
*/
449478
public function build()
450479
{

tests/src/PHPUnit/OpenAPI3/GenTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testGenerateSwagger()
3939
$builder->buildSetters = true;
4040
$builder->makeEnumConstants = true;
4141
$builder->minimizeRefs = $this->minimizeRefs;
42+
$builder->namesFromDescriptions = true;
4243

4344
$builder->classCreatedHook = new ClassHookCallback(function (PhpClass $class, $path, $schema) use ($app, $appNs) {
4445
$desc = '';

tests/src/Tmp/OpenAPI3/Components.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Components extends ClassStructure
2727
/** @var string[][]|Response[] */
2828
public $responses;
2929

30-
/** @var string[][]|Parameter[]|mixed[]|ParameterLocationOneOf0[]|ParameterLocationOneOf1[]|ParameterLocationOneOf2[]|ParameterLocationOneOf3[] */
30+
/** @var string[][]|Parameter[]|mixed[]|ParameterLocationParameterInPath[]|ParameterLocationParameterInQuery[]|ParameterLocationParameterInHeader[]|ParameterLocationParameterInCookie[] */
3131
public $parameters;
3232

3333
/** @var string[][]|Example[] */
@@ -39,7 +39,7 @@ class Components extends ClassStructure
3939
/** @var string[][]|Header[]|mixed[] */
4040
public $headers;
4141

42-
/** @var string[][]|APIKeySecurityScheme[]|HTTPSecurityScheme[]|HTTPSecuritySchemeOneOf0[]|HTTPSecuritySchemeOneOf1[]|OAuth2SecurityScheme[]|OpenIdConnectSecurityScheme[] */
42+
/** @var string[][]|APIKeySecurityScheme[]|HTTPSecurityScheme[]|HTTPSecuritySchemeBearer[]|HTTPSecuritySchemeNonBearer[]|OAuth2SecurityScheme[]|OpenIdConnectSecurityScheme[] */
4343
public $securitySchemes;
4444

4545
/** @var string[][]|Link[] */
@@ -214,7 +214,7 @@ public function setResponses($responses)
214214
/** @codeCoverageIgnoreEnd */
215215

216216
/**
217-
* @param string[][]|Parameter[]|mixed[]|ParameterLocationOneOf0[]|ParameterLocationOneOf1[]|ParameterLocationOneOf2[]|ParameterLocationOneOf3[] $parameters
217+
* @param string[][]|Parameter[]|mixed[]|ParameterLocationParameterInPath[]|ParameterLocationParameterInQuery[]|ParameterLocationParameterInHeader[]|ParameterLocationParameterInCookie[] $parameters
218218
* @return $this
219219
* @codeCoverageIgnoreStart
220220
*/
@@ -262,7 +262,7 @@ public function setHeaders($headers)
262262
/** @codeCoverageIgnoreEnd */
263263

264264
/**
265-
* @param string[][]|APIKeySecurityScheme[]|HTTPSecurityScheme[]|HTTPSecuritySchemeOneOf0[]|HTTPSecuritySchemeOneOf1[]|OAuth2SecurityScheme[]|OpenIdConnectSecurityScheme[] $securitySchemes
265+
* @param string[][]|APIKeySecurityScheme[]|HTTPSecurityScheme[]|HTTPSecuritySchemeBearer[]|HTTPSecuritySchemeNonBearer[]|OAuth2SecurityScheme[]|OpenIdConnectSecurityScheme[] $securitySchemes
266266
* @return $this
267267
* @codeCoverageIgnoreStart
268268
*/

tests/src/Tmp/OpenAPI3/HTTPSecurityScheme.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* Built from #/definitions/HTTPSecurityScheme
20-
* @method static HTTPSecurityScheme|HTTPSecuritySchemeOneOf0|HTTPSecuritySchemeOneOf1 import($data, Context $options = null)
20+
* @method static HTTPSecurityScheme|HTTPSecuritySchemeBearer|HTTPSecuritySchemeNonBearer import($data, Context $options = null)
2121
*/
2222
class HTTPSecurityScheme extends ClassStructure
2323
{
@@ -54,8 +54,8 @@ public static function setUpProperties($properties, Schema $ownerSchema)
5454
$ownerSchema->additionalProperties = false;
5555
$patternProperty = new Schema();
5656
$ownerSchema->setPatternProperty('^x-', $patternProperty);
57-
$ownerSchema->oneOf[0] = HTTPSecuritySchemeOneOf0::schema();
58-
$ownerSchema->oneOf[1] = HTTPSecuritySchemeOneOf1::schema();
57+
$ownerSchema->oneOf[0] = HTTPSecuritySchemeBearer::schema();
58+
$ownerSchema->oneOf[1] = HTTPSecuritySchemeNonBearer::schema();
5959
$ownerSchema->required = array(
6060
self::names()->scheme,
6161
self::names()->type,

tests/src/Tmp/OpenAPI3/HTTPSecuritySchemeOneOf0.php tests/src/Tmp/OpenAPI3/HTTPSecuritySchemeBearer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Bearer
1616
*/
17-
class HTTPSecuritySchemeOneOf0 extends ClassStructure
17+
class HTTPSecuritySchemeBearer extends ClassStructure
1818
{
1919
const BEARER = 'bearer';
2020

tests/src/Tmp/OpenAPI3/HTTPSecuritySchemeOneOf1.php tests/src/Tmp/OpenAPI3/HTTPSecuritySchemeNonBearer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Non Bearer
1616
*/
17-
class HTTPSecuritySchemeOneOf1 extends ClassStructure
17+
class HTTPSecuritySchemeNonBearer extends ClassStructure
1818
{
1919
const BEARER = 'bearer';
2020

tests/src/Tmp/OpenAPI3/Operation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Operation extends ClassStructure
3636
/** @var string */
3737
public $operationId;
3838

39-
/** @var Parameter[]|mixed[]|ParameterLocationOneOf0[]|ParameterLocationOneOf1[]|ParameterLocationOneOf2[]|ParameterLocationOneOf3[]|string[][]|array */
39+
/** @var Parameter[]|mixed[]|ParameterLocationParameterInPath[]|ParameterLocationParameterInQuery[]|ParameterLocationParameterInHeader[]|ParameterLocationParameterInCookie[]|string[][]|array */
4040
public $parameters;
4141

4242
/** @var RequestBody|string[] */
@@ -191,7 +191,7 @@ public function setOperationId($operationId)
191191
/** @codeCoverageIgnoreEnd */
192192

193193
/**
194-
* @param Parameter[]|mixed[]|ParameterLocationOneOf0[]|ParameterLocationOneOf1[]|ParameterLocationOneOf2[]|ParameterLocationOneOf3[]|string[][]|array $parameters
194+
* @param Parameter[]|mixed[]|ParameterLocationParameterInPath[]|ParameterLocationParameterInQuery[]|ParameterLocationParameterInHeader[]|ParameterLocationParameterInCookie[]|string[][]|array $parameters
195195
* @return $this
196196
* @codeCoverageIgnoreStart
197197
*/

tests/src/Tmp/OpenAPI3/Parameter.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* Built from #/definitions/Parameter
20-
* @method static Parameter|ParameterLocationOneOf0|ParameterLocationOneOf1|ParameterLocationOneOf2|ParameterLocationOneOf3 import($data, Context $options = null)
20+
* @method static Parameter|ParameterLocationParameterInPath|ParameterLocationParameterInQuery|ParameterLocationParameterInHeader|ParameterLocationParameterInCookie import($data, Context $options = null)
2121
*/
2222
class Parameter extends ClassStructure
2323
{
@@ -173,10 +173,10 @@ public static function setUpProperties($properties, Schema $ownerSchema)
173173
$ownerSchemaAllOf1->setFromRef('#/definitions/SchemaXORContent');
174174
$ownerSchema->allOf[1] = $ownerSchemaAllOf1;
175175
$ownerSchemaAllOf2 = new Schema();
176-
$ownerSchemaAllOf2->oneOf[0] = ParameterLocationOneOf0::schema();
177-
$ownerSchemaAllOf2->oneOf[1] = ParameterLocationOneOf1::schema();
178-
$ownerSchemaAllOf2->oneOf[2] = ParameterLocationOneOf2::schema();
179-
$ownerSchemaAllOf2->oneOf[3] = ParameterLocationOneOf3::schema();
176+
$ownerSchemaAllOf2->oneOf[0] = ParameterLocationParameterInPath::schema();
177+
$ownerSchemaAllOf2->oneOf[1] = ParameterLocationParameterInQuery::schema();
178+
$ownerSchemaAllOf2->oneOf[2] = ParameterLocationParameterInHeader::schema();
179+
$ownerSchemaAllOf2->oneOf[3] = ParameterLocationParameterInCookie::schema();
180180
$ownerSchemaAllOf2->description = "Parameter location";
181181
$ownerSchemaAllOf2->setFromRef('#/definitions/ParameterLocation');
182182
$ownerSchema->allOf[2] = $ownerSchemaAllOf2;

tests/src/Tmp/OpenAPI3/ParameterLocationOneOf3.php tests/src/Tmp/OpenAPI3/ParameterLocationParameterInCookie.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Parameter in cookie
1616
*/
17-
class ParameterLocationOneOf3 extends ClassStructure
17+
class ParameterLocationParameterInCookie extends ClassStructure
1818
{
1919
const COOKIE = 'cookie';
2020

tests/src/Tmp/OpenAPI3/ParameterLocationOneOf2.php tests/src/Tmp/OpenAPI3/ParameterLocationParameterInHeader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Parameter in header
1616
*/
17-
class ParameterLocationOneOf2 extends ClassStructure
17+
class ParameterLocationParameterInHeader extends ClassStructure
1818
{
1919
const HEADER = 'header';
2020

tests/src/Tmp/OpenAPI3/ParameterLocationOneOf0.php tests/src/Tmp/OpenAPI3/ParameterLocationParameterInPath.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Parameter in path
1616
*/
17-
class ParameterLocationOneOf0 extends ClassStructure
17+
class ParameterLocationParameterInPath extends ClassStructure
1818
{
1919
const PATH = 'path';
2020

tests/src/Tmp/OpenAPI3/ParameterLocationOneOf1.php tests/src/Tmp/OpenAPI3/ParameterLocationParameterInQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Parameter in query
1616
*/
17-
class ParameterLocationOneOf1 extends ClassStructure
17+
class ParameterLocationParameterInQuery extends ClassStructure
1818
{
1919
const QUERY = 'query';
2020

tests/src/Tmp/OpenAPI3/PathItem.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PathItem extends ClassStructure
3535
/** @var Server[]|array */
3636
public $servers;
3737

38-
/** @var Parameter[]|mixed[]|ParameterLocationOneOf0[]|ParameterLocationOneOf1[]|ParameterLocationOneOf2[]|ParameterLocationOneOf3[]|string[][]|array */
38+
/** @var Parameter[]|mixed[]|ParameterLocationParameterInPath[]|ParameterLocationParameterInQuery[]|ParameterLocationParameterInHeader[]|ParameterLocationParameterInCookie[]|string[][]|array */
3939
public $parameters;
4040

4141
/**
@@ -121,7 +121,7 @@ public function setServers($servers)
121121
/** @codeCoverageIgnoreEnd */
122122

123123
/**
124-
* @param Parameter[]|mixed[]|ParameterLocationOneOf0[]|ParameterLocationOneOf1[]|ParameterLocationOneOf2[]|ParameterLocationOneOf3[]|string[][]|array $parameters
124+
* @param Parameter[]|mixed[]|ParameterLocationParameterInPath[]|ParameterLocationParameterInQuery[]|ParameterLocationParameterInHeader[]|ParameterLocationParameterInCookie[]|string[][]|array $parameters
125125
* @return $this
126126
* @codeCoverageIgnoreStart
127127
*/

0 commit comments

Comments
 (0)