Skip to content

Commit 53e18e3

Browse files
authored
Merge pull request #140 from php-api-clients/content-type-configuration
Content type configuration
2 parents f9c021a + 9e744ce commit 53e18e3

File tree

10 files changed

+540
-382
lines changed

10 files changed

+540
-382
lines changed

example/openapi-client-one.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ templates:
1515
schemas:
1616
allowDuplication: true
1717
useAliasesForDuplication: true
18+
contentType:
19+
- ApiClients\Tools\OpenApiClientGenerator\ContentType\Json
20+
- ApiClients\Tools\OpenApiClientGenerator\ContentType\Raw
1821
voter:
1922
listOperation:
2023
- ApiClients\Tools\OpenApiClientGenerator\Voter\ListOperation\PageAndPerPageInQuery

example/openapi-client-subsplit.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ templates:
1414
schemas:
1515
allowDuplication: true
1616
useAliasesForDuplication: true
17+
contentType:
18+
- ApiClients\Tools\OpenApiClientGenerator\ContentType\Json
19+
- ApiClients\Tools\OpenApiClientGenerator\ContentType\Raw
1720
voter:
1821
listOperation:
1922
- ApiClients\Tools\OpenApiClientGenerator\Voter\ListOperation\PageAndPerPageInQuery

example/templates/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ psalm: ## Run static analysis (Psalm)
4444
$(DOCKER_RUN) vendor/bin/psalm --threads=$(shell nproc) --shepherd --stats --config=./etc/qa/psalm.xml
4545

4646
unit-testing: ## Run tests
47-
$(DOCKER_RUN) vendor/bin/phpunit --colors=always -c ./etc/qa/phpunit.xml --coverage-text --coverage-html ./var/tests-unit-coverage-html --coverage-clover ./var/tests-unit-clover-coverage.xml
47+
$(DOCKER_RUN) vendor/bin/phpunit --colors=always -c ./etc/qa/phpunit.xml
4848
$(DOCKER_RUN) test -n "$(COVERALLS_REPO_TOKEN)" && test -n "$(COVERALLS_RUN_LOCALLY)" && test -f ./var/tests-unit-clover-coverage.xml && vendor/bin/php-coveralls -v --coverage_clover ./build/logs/clover.xml --json_path ./var/tests-unit-clover-coverage-upload.json || true
4949

5050
mutation-testing: ## Run mutation testing

src/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@
88
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Templates;
99
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Voter;
1010
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Schemas;
11+
use ApiClients\Tools\OpenApiClientGenerator\Contract\ContentType;
1112
use EventSauce\ObjectHydrator\MapFrom;
1213

1314
final readonly class Configuration
1415
{
16+
/**
17+
* @param array<class-string<ContentType>>|null $contentType
18+
*/
1519
public function __construct(
1620
public string $spec,
1721
public Templates $templates,
1822
public Namespace_ $namespace,
1923
public Destination $destination,
24+
#[MapFrom('contentType')]
25+
public ?array $contentType,
2026
#[MapFrom('subSplit')]
2127
public ?SubSplit $subSplit,
2228
public ?Schemas $schemas,

src/ContentType/Json.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator\ContentType;
4+
5+
use ApiClients\Tools\OpenApiClientGenerator\Contract\ContentType;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
10+
final class Json implements ContentType
11+
{
12+
public static function contentType(): iterable
13+
{
14+
yield 'application/json';
15+
}
16+
17+
public static function parse(Expr $expr): Expr {
18+
return new Node\Expr\FuncCall(
19+
new Node\Name('json_decode'),
20+
[
21+
new Arg(
22+
$expr,
23+
),
24+
new Node\Expr\ConstFetch(
25+
new Node\Name('true'),
26+
)
27+
],
28+
);
29+
}
30+
}

src/ContentType/Raw.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator\ContentType;
4+
5+
use ApiClients\Tools\OpenApiClientGenerator\Contract\ContentType;
6+
use PhpParser\Node\Expr;
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
10+
final class Raw implements ContentType
11+
{
12+
public static function contentType(): iterable
13+
{
14+
yield 'text/plain';
15+
yield 'text/x-markdown';
16+
yield 'text/html';
17+
}
18+
19+
public static function parse(Expr $expr): Expr {
20+
return $expr;
21+
}
22+
}

src/Contract/ContentType.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator\Contract;
4+
5+
use ApiClients\Tools\OpenApiClientGenerator\Representation\Path;
6+
use ApiClients\Tools\OpenApiClientGenerator\Representation\WebHook;
7+
use PhpParser\Node\Expr;
8+
9+
interface ContentType
10+
{
11+
/**
12+
* @return iterable<string>
13+
*/
14+
public static function contentType(): iterable;
15+
16+
public static function parse(Expr $expr): Expr;
17+
}

src/Generator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
174174
$operation,
175175
$path->hydrator,
176176
$throwableSchemaRegistry,
177+
$configuration,
177178
);
178179
yield from OperationTest::generate(
179180
$configuration->destination->test . DIRECTORY_SEPARATOR,
@@ -182,6 +183,7 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
182183
$operation,
183184
$path->hydrator,
184185
$throwableSchemaRegistry,
186+
$configuration,
185187
);
186188
}
187189
}
@@ -282,6 +284,7 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
282284
$operation,
283285
$path->hydrator,
284286
$throwableSchemaRegistry,
287+
$configuration,
285288
);
286289
yield from OperationTest::generate(
287290
$configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($configuration->subSplit->sectionPackage, $split) . $configuration->destination->test,
@@ -290,6 +293,7 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
290293
$operation,
291294
$path->hydrator,
292295
$throwableSchemaRegistry,
296+
$configuration,
293297
);
294298
}
295299
}

0 commit comments

Comments
 (0)