Skip to content

Commit 2bbb790

Browse files
authored
Merge pull request #58 from DoclerLabs/fix-composer-ext-require
fix ext-* in composer require
2 parents a1a7df2 + 19a9089 commit 2bbb790

11 files changed

+184
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [7.2.2] - 2021-10-15
8+
### Fixed
9+
- ext- in generated clients composer.json is added according to what is actually being used
10+
711
## [7.2.1] - 2021-10-07
812
### Fixed
913
- Changed default php-cs-fixer configuration to remove blank lines after class opening, according to PSR12 (`no_blank_lines_after_class_opening`)

example/gen/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"require": {
1212
"php": ">=7.4",
1313
"docler-labs/api-client-exception": "^1.0",
14-
"ext-intl": "*",
14+
"ext-dom": "*",
15+
"ext-json": "*",
1516
"guzzlehttp/psr7": "^1.6",
1617
"pimple/pimple": "^3.3",
1718
"psr/container": "^1.0",

src/Input/Specification.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
namespace DoclerLabs\ApiClientGenerator\Input;
66

77
use cebe\openapi\spec\OpenApi;
8+
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
9+
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MaxLengthConstraint;
10+
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MinLengthConstraint;
11+
use DoclerLabs\ApiClientGenerator\Entity\Field;
812
use DoclerLabs\ApiClientGenerator\Entity\FieldCollection;
913
use DoclerLabs\ApiClientGenerator\Entity\Operation;
1014
use DoclerLabs\ApiClientGenerator\Entity\OperationCollection;
@@ -100,4 +104,50 @@ public function getAllContentTypes(): array
100104

101105
return array_keys(array_filter($allContentTypes));
102106
}
107+
108+
public function requiresIntlExtension(): bool
109+
{
110+
/** @var Operation $operation */
111+
foreach ($this->getOperations() as $operation) {
112+
foreach ($operation->getRequest()->getFields() as $fields) {
113+
/** @var Field $field */
114+
foreach ($fields as $field) {
115+
if ($this->fieldRequiresIntlExtension($field)) {
116+
return true;
117+
}
118+
}
119+
}
120+
}
121+
122+
return false;
123+
}
124+
125+
private function fieldRequiresIntlExtension(Field $field): bool
126+
{
127+
/** @var ConstraintInterface $constraint */
128+
foreach ($field->getConstraints() as $constraint) {
129+
if (
130+
(
131+
$constraint instanceof MinLengthConstraint
132+
|| $constraint instanceof MaxLengthConstraint
133+
)
134+
&& $constraint->exists()
135+
) {
136+
return true;
137+
}
138+
}
139+
140+
if (
141+
$field->isObject()
142+
&& !empty($field->getObjectProperties())
143+
) {
144+
foreach ($field->getObjectProperties() as $field) {
145+
if ($this->fieldRequiresIntlExtension($field)) {
146+
return true;
147+
}
148+
}
149+
}
150+
151+
return false;
152+
}
103153
}

src/Meta/ComposerJsonTemplate.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use DoclerLabs\ApiClientGenerator\Generator\Implementation\HttpMessageImplementationStrategy;
99
use DoclerLabs\ApiClientGenerator\Input\Configuration;
1010
use DoclerLabs\ApiClientGenerator\Input\Specification;
11+
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\JsonContentTypeSerializer;
12+
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\XmlContentTypeSerializer;
1113
use DoclerLabs\ApiClientGenerator\Output\Meta\MetaFile;
1214
use DoclerLabs\ApiClientGenerator\Output\Meta\MetaFileCollection;
1315
use Twig\Environment;
@@ -40,6 +42,7 @@ public function render(Specification $specification, MetaFileCollection $fileReg
4042
{
4143
$packages = array_merge(
4244
$this->getCommonPackages(),
45+
$this->getPackagesForSpecification($specification),
4346
$this->messageImplementation->getPackages(),
4447
$this->containerImplementation->getPackages()
4548
);
@@ -63,9 +66,27 @@ private function getCommonPackages(): array
6366
{
6467
return [
6568
'docler-labs/api-client-exception' => '^1.0',
66-
'ext-intl' => '*',
6769
'psr/container' => '^1.0',
6870
'psr/http-client' => '^1.0',
6971
];
7072
}
73+
74+
private function getPackagesForSpecification(Specification $specification): array
75+
{
76+
$packages = [];
77+
78+
if ($specification->requiresIntlExtension()) {
79+
$packages['ext-intl'] = '*';
80+
}
81+
82+
if (in_array(JsonContentTypeSerializer::MIME_TYPE, $specification->getAllContentTypes(), true)) {
83+
$packages['ext-json'] = '*';
84+
}
85+
86+
if (in_array(XmlContentTypeSerializer::MIME_TYPE, $specification->getAllContentTypes(), true)) {
87+
$packages['ext-dom'] = '*';
88+
}
89+
90+
return $packages;
91+
}
7192
}

test/suite/functional/Input/SpecificationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class SpecificationTest extends TestCase
2222
public function testAllContentTypesArrayPopulatedCorrectly(array $data, array $expectedResult): void
2323
{
2424
$specification = $this->sut->parse($data, '/openapi.yaml');
25-
static::assertSame($specification->getAllContentTypes(), $expectedResult);
25+
26+
static::assertSame($expectedResult, $specification->getAllContentTypes());
2627
}
2728

2829
public function contentTypesTestProvider(): array

test/suite/functional/Meta/ComposerJson/composer_default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"require": {
1111
"php": ">=7.4",
1212
"docler-labs/api-client-exception": "^1.0",
13-
"ext-intl": "*",
13+
"ext-json": "*",
1414
"guzzlehttp/psr7": "^1.6",
1515
"pimple/pimple": "^3.3",
1616
"psr/container": "^1.0",

test/suite/functional/Meta/ComposerJson/composer_guzzle_message.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"require": {
1111
"php": ">=7.4",
1212
"docler-labs/api-client-exception": "^1.0",
13-
"ext-intl": "*",
13+
"ext-json": "*",
1414
"guzzlehttp/psr7": "^1.6",
1515
"pimple/pimple": "^3.3",
1616
"psr/container": "^1.0",

test/suite/functional/Meta/ComposerJson/composer_nyholm_message.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"require": {
1111
"php": ">=7.4",
1212
"docler-labs/api-client-exception": "^1.0",
13-
"ext-intl": "*",
13+
"ext-json": "*",
1414
"nyholm/psr7": "^1.3",
1515
"pimple/pimple": "^3.3",
1616
"psr/container": "^1.0",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "test/test-api-client",
3+
"description": "Client library for Swagger Petstore",
4+
"keywords": [
5+
"api-client"
6+
],
7+
"config": {
8+
"sort-packages": true
9+
},
10+
"require": {
11+
"php": ">=7.4",
12+
"docler-labs/api-client-exception": "^1.0",
13+
"ext-intl": "*",
14+
"ext-json": "*",
15+
"guzzlehttp/psr7": "^1.6",
16+
"pimple/pimple": "^3.3",
17+
"psr/container": "^1.0",
18+
"psr/http-client": "^1.0"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Test\\": "src/"
23+
}
24+
},
25+
"suggest": {
26+
"guzzlehttp/guzzle": "PSR-18 provided by Guzzle 7",
27+
"php-http/guzzle6-adapter": "PSR-18 wrapper for Guzzle 6"
28+
}
29+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
6+
servers:
7+
- url: http://petstore.swagger.io/api
8+
paths:
9+
/pets:
10+
post:
11+
operationId: addPet
12+
requestBody:
13+
description: Pet to add to the store
14+
required: true
15+
content:
16+
application/json:
17+
schema:
18+
$ref: '#/components/schemas/NewPet'
19+
responses:
20+
'200':
21+
description: pet response
22+
content:
23+
application/json:
24+
schema:
25+
$ref: '#/components/schemas/Pet'
26+
default:
27+
description: unexpected error
28+
content:
29+
application/json:
30+
schema:
31+
$ref: '#/components/schemas/Error'
32+
components:
33+
schemas:
34+
Pet:
35+
allOf:
36+
- $ref: '#/components/schemas/NewPet'
37+
- type: object
38+
required:
39+
- id
40+
properties:
41+
id:
42+
type: integer
43+
format: int64
44+
45+
NewPet:
46+
type: object
47+
required:
48+
- name
49+
properties:
50+
name:
51+
type: string
52+
maxLength: 50
53+
tag:
54+
type: string
55+
56+
Error:
57+
type: object
58+
required:
59+
- code
60+
- message
61+
properties:
62+
code:
63+
type: integer
64+
format: int32
65+
message:
66+
type: string

test/suite/functional/Meta/ComposerJsonTemplateTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public function exampleProvider(): array
3030
'composer.json',
3131
ConfigurationBuilder::fake()->build(),
3232
],
33+
'composer.json with intl' => [
34+
'/ComposerJson/petstore_with_intl_requirement.yaml',
35+
'/ComposerJson/composer_with_intl_requirement.json',
36+
'composer.json',
37+
ConfigurationBuilder::fake()->build(),
38+
],
3339
'Guzzle message composer.json' => [
3440
'/ComposerJson/petstore.yaml',
3541
'/ComposerJson/composer_guzzle_message.json',

0 commit comments

Comments
 (0)