Skip to content

Commit 1707fe5

Browse files
committed
Making more progress on swagger generating being done
1 parent 499df98 commit 1707fe5

9 files changed

Lines changed: 57 additions & 24 deletions

File tree

.devcontainer/post-create.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ sudo pecl install ast;
44
echo "extension=ast.so" | sudo tee -a /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini;
55
composer install
66
composer dump-autoload
7+
8+
cd sample
9+
composer install
10+
composer dump-autoload

sample/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "schentrup-software/php-api-sample",
33
"type": "project",
4+
"minimum-stability": "dev",
45
"autoload": {
56
"psr-4": {
67
"PhpApiSample\\": "src/"
@@ -16,4 +17,4 @@
1617
"php": ">=8.1",
1718
"schentrup-software/php-api": "*"
1819
}
19-
}
20+
}

sample/src/Routes/Path/Subpath/GetPathSubpath.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
class GetPathSubpath
99
{
10-
public function execute(GetRequest $_, int $pathVar2): GetResponse
10+
public function execute(GetRequest $r, int $pathVar2): GetResponse
1111
{
1212
$response = new GetResponse(
13-
message: 'Hello World 2',
13+
message: $r->someMessage,
1414
pathVar: $pathVar2,
15+
someVar: $r->someVar,
1516
);
1617
$response->setCode(200);
1718
return $response;
@@ -20,19 +21,19 @@ public function execute(GetRequest $_, int $pathVar2): GetResponse
2021

2122
class GetRequest extends AbstractRequest
2223
{
23-
public function __construct(
24-
public ?int $someVar,
25-
public ?string $someMessage,
26-
) {
27-
}
24+
public ?int $someVar; // TODO: This does not work becuase property is not intialized
25+
public string $someMessage = 'Has a default value';
2826
}
2927

3028
class GetResponse extends AbstractJsonResponse
3129
{
3230
public const ResponseCode = 200;
3331

32+
// TODO: This is kinda weird that we have the constructor version of properties for responses
33+
// But that does not work for requests. We need to work that out.
3434
public function __construct(
3535
public int $pathVar,
36+
public ?int $someVar = null,
3637
public string $message = 'Hello World 2',
3738
) {
3839
}

src/Model/Request/AbstractRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(
1414
) {
1515
$this->request = $request;
1616

17-
$paramTypes = RequestParser::getParamTypes(self::class, $this->request->method->name);
17+
$paramTypes = RequestParser::getParamTypes($this::class, $this->request->method->name);
1818

1919
foreach ($paramTypes as $paramType) {
2020
if ($paramType->type === InputParamType::Query) {

src/Model/Request/RequestParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static function getParamTypes(string $requestClass, ?string $method): arr
3535
}
3636

3737
$attributes = $property->getAttributes();
38+
$name = null;
39+
3840
foreach ($attributes as $attribute) {
3941
$attributeInstance = $attribute->newInstance();
4042
$inputParamType = InputParamType::fromClassInstance($attributeInstance);
@@ -59,6 +61,7 @@ public static function getParamTypes(string $requestClass, ?string $method): arr
5961
name: $name,
6062
propertyName: $property->getName(),
6163
type: $inputParamType,
64+
hasDefaultValue: $property->hasDefaultValue(),
6265
);
6366
}
6467

src/Model/Request/RequestProperty.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class RequestProperty
99
public function __construct(
1010
public readonly string $name,
1111
public readonly string $propertyName,
12-
public readonly InputParamType $type
12+
public readonly InputParamType $type,
13+
public readonly bool $hasDefaultValue,
1314
) {
1415
}
1516
}

src/Swagger/GenerateSwaggerDocs.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpApi\Swagger\Model\Path;
2222
use PhpApi\Swagger\Model\RequestBody;
2323
use PhpApi\Swagger\Model\RequestObjectParseResults;
24+
use PhpApi\Swagger\Model\RequestObjectQueryParam;
2425
use PhpApi\Swagger\Model\Response;
2526
use PhpApi\Swagger\Model\ResponseContent;
2627
use PhpApi\Swagger\Model\Schema;
@@ -126,14 +127,15 @@ private function generateSwagger(array $urls): SwaggerDoc
126127
throw new InvalidArgumentException("Intersection types are not supported");
127128
}
128129

130+
// TODO: This should be an attribute
129131
$description = $reflectionMethod->getDocComment();
130132
if ($description == false) {
131133
$description = $reflectionMethod->getName();
132134
}
133135

134136
$paths[$cleanPath][strtolower($method)] = new Path(
135137
tags: [],
136-
summary: $description,
138+
summary: $description, //TODO: Add summary attribute to the method
137139
description: $description,
138140
operationId: $method . '_' . $reflectionClass->getName(),
139141
parameters: $parameters,
@@ -163,7 +165,7 @@ private function generateSwagger(array $urls): SwaggerDoc
163165
url: $this->swaggerOptions->externalDocsUrl,
164166
description: $this->swaggerOptions->externalDocsDescription,
165167
),
166-
tags: [],
168+
tags: [], // TODO: tags for endpoints
167169
paths: $paths,
168170
);
169171
}
@@ -185,12 +187,15 @@ private function parseRequestType(ReflectionNamedType|ReflectionUnionType $refle
185187
throw new InvalidArgumentException("Return type must be a subclass of AbstractResponse");
186188
}
187189

188-
foreach ($parsedType->queryParams as $name => $schema) {
190+
foreach ($parsedType->queryParams as $name => $propertyData) {
189191
$parameters[] = new Parameter(
190192
name: $name,
191193
in: 'query',
192-
required: !$type->allowsNull(),
193-
schema: $schema,
194+
required: !$reflectionType->allowsNull()
195+
&& !$parsedType->allowsNull
196+
&& !$propertyData->allowsNull,
197+
schema: $propertyData->schema,
198+
description: $propertyData->description,
194199
);
195200
}
196201
} else {
@@ -231,16 +236,15 @@ private function parseRequestType(ReflectionNamedType|ReflectionUnionType $refle
231236

232237
$parsedType = $this->parseNamedRequestType($reflectionType, $method);
233238

234-
foreach ($parsedType->queryParams as $name => $schema) {
239+
foreach ($parsedType->queryParams as $name => $propertyData) {
235240
$parameters[] = new Parameter(
236241
name: $name,
237242
in: 'query',
238-
// This does not work with nullable properties becuase the schema does not have a
239-
// required property. This data, along with "description" will have to be pulled
240-
// up from the property class. This should probably be done with a wrapper on the
241-
// schema
242-
required: !$reflectionType->allowsNull() && !$parsedType->allowsNull,
243-
schema: $schema,
243+
required: !$reflectionType->allowsNull()
244+
&& !$parsedType->allowsNull
245+
&& !$propertyData->allowsNull,
246+
schema: $propertyData->schema,
247+
description: $propertyData->description,
244248
);
245249
}
246250

@@ -257,6 +261,7 @@ private function parseRequestType(ReflectionNamedType|ReflectionUnionType $refle
257261
return null;
258262
}
259263

264+
// TODO: Request object descriptions
260265
return new RequestBody(
261266
required: !$reflectionType->allowsNull(),
262267
content: $content,
@@ -284,7 +289,12 @@ private function parseNamedRequestType(ReflectionNamedType $reflectionType, stri
284289
}
285290

286291
if ($paramType->type === InputParamType::Query) {
287-
$queryContent[$paramType->name] = $this->getSchemaFromClass($propertyType);
292+
$queryContent[$paramType->name] = new RequestObjectQueryParam(
293+
schema: $this->getSchemaFromClass($propertyType),
294+
allowsNull: $propertyType->allowsNull()
295+
|| $paramType->hasDefaultValue,
296+
description: '' // TODO: Add description attribute to the property
297+
);
288298
} elseif ($paramType->type === InputParamType::Json) {
289299
if ($inputContentType === null) {
290300
$inputContentType = InputParamType::Json;

src/Swagger/Model/RequestObjectParseResults.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class RequestObjectParseResults
88
{
99
/**
10-
* @param array<string, Schema> $queryParams
10+
* @param array<string, RequestObjectQueryParam> $queryParams
1111
* @param array<string, Schema> $inputContent
1212
*/
1313
public function __construct(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace PhpApi\Swagger\Model;
4+
5+
class RequestObjectQueryParam
6+
{
7+
public function __construct(
8+
public readonly Schema $schema,
9+
public readonly bool $allowsNull,
10+
public readonly string $description,
11+
) {
12+
}
13+
}

0 commit comments

Comments
 (0)