Skip to content

Commit

Permalink
Merge pull request #57 from DoclerLabs/fix-dependabot-alert-about-vul…
Browse files Browse the repository at this point in the history
…nerability-with-composer-version

Fix dependabot alert about vulnerability with composer version
  • Loading branch information
vsouz4 authored Oct 22, 2021
2 parents 1c6de73 + 2bbb790 commit 860b5a0
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 44 deletions.
1 change: 1 addition & 0 deletions .php_cs.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'no_multiline_whitespace_before_semicolons' => true,
'no_blank_lines_after_class_opening' => true,
'no_short_echo_tag' => true,
'no_unused_imports' => true,
'blank_line_after_opening_tag' => true,
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [7.2.2] - 2021-10-15
### Fixed
- ext- in generated clients composer.json is added according to what is actually being used

## [7.2.1] - 2021-10-07
### Fixed
- Changed default php-cs-fixer configuration to remove blank lines after class opening, according to PSR12 (`no_blank_lines_after_class_opening`)

## [7.2.0] - 2021-09-24
### Removed
- Remove virtual package dependency (psr/http-client-implementation)

## [7.1.1] - 2021-09-09
### Fixed
- Changed default php-cs-fixer configuration to add blank line after opening tag, according to PSR12
- Changed default php-cs-fixer configuration to add blank line after opening tag, according to PSR12 (`blank_line_after_opening_tag`)

## [7.1.0] - 2021-08-30
### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"twig/twig": "^3.0"
},
"require-dev": {
"composer/composer": "^2.0",
"composer/composer": "^2.1",
"php-coveralls/php-coveralls": "^2.2",
"phpstan/phpstan": "^0.12.32",
"phpunit/phpunit": "^9.2",
Expand Down
73 changes: 37 additions & 36 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion example/gen/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"require": {
"php": ">=7.4",
"docler-labs/api-client-exception": "^1.0",
"ext-intl": "*",
"ext-dom": "*",
"ext-json": "*",
"guzzlehttp/psr7": "^1.6",
"pimple/pimple": "^3.3",
"psr/container": "^1.0",
Expand Down
50 changes: 50 additions & 0 deletions src/Input/Specification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace DoclerLabs\ApiClientGenerator\Input;

use cebe\openapi\spec\OpenApi;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MaxLengthConstraint;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MinLengthConstraint;
use DoclerLabs\ApiClientGenerator\Entity\Field;
use DoclerLabs\ApiClientGenerator\Entity\FieldCollection;
use DoclerLabs\ApiClientGenerator\Entity\Operation;
use DoclerLabs\ApiClientGenerator\Entity\OperationCollection;
Expand Down Expand Up @@ -100,4 +104,50 @@ public function getAllContentTypes(): array

return array_keys(array_filter($allContentTypes));
}

public function requiresIntlExtension(): bool
{
/** @var Operation $operation */
foreach ($this->getOperations() as $operation) {
foreach ($operation->getRequest()->getFields() as $fields) {
/** @var Field $field */
foreach ($fields as $field) {
if ($this->fieldRequiresIntlExtension($field)) {
return true;
}
}
}
}

return false;
}

private function fieldRequiresIntlExtension(Field $field): bool
{
/** @var ConstraintInterface $constraint */
foreach ($field->getConstraints() as $constraint) {
if (
(
$constraint instanceof MinLengthConstraint
|| $constraint instanceof MaxLengthConstraint
)
&& $constraint->exists()
) {
return true;
}
}

if (
$field->isObject()
&& !empty($field->getObjectProperties())
) {
foreach ($field->getObjectProperties() as $field) {
if ($this->fieldRequiresIntlExtension($field)) {
return true;
}
}
}

return false;
}
}
23 changes: 22 additions & 1 deletion src/Meta/ComposerJsonTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use DoclerLabs\ApiClientGenerator\Generator\Implementation\HttpMessageImplementationStrategy;
use DoclerLabs\ApiClientGenerator\Input\Configuration;
use DoclerLabs\ApiClientGenerator\Input\Specification;
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\JsonContentTypeSerializer;
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\XmlContentTypeSerializer;
use DoclerLabs\ApiClientGenerator\Output\Meta\MetaFile;
use DoclerLabs\ApiClientGenerator\Output\Meta\MetaFileCollection;
use Twig\Environment;
Expand Down Expand Up @@ -40,6 +42,7 @@ public function render(Specification $specification, MetaFileCollection $fileReg
{
$packages = array_merge(
$this->getCommonPackages(),
$this->getPackagesForSpecification($specification),
$this->messageImplementation->getPackages(),
$this->containerImplementation->getPackages()
);
Expand All @@ -63,9 +66,27 @@ private function getCommonPackages(): array
{
return [
'docler-labs/api-client-exception' => '^1.0',
'ext-intl' => '*',
'psr/container' => '^1.0',
'psr/http-client' => '^1.0',
];
}

private function getPackagesForSpecification(Specification $specification): array
{
$packages = [];

if ($specification->requiresIntlExtension()) {
$packages['ext-intl'] = '*';
}

if (in_array(JsonContentTypeSerializer::MIME_TYPE, $specification->getAllContentTypes(), true)) {
$packages['ext-json'] = '*';
}

if (in_array(XmlContentTypeSerializer::MIME_TYPE, $specification->getAllContentTypes(), true)) {
$packages['ext-dom'] = '*';
}

return $packages;
}
}
3 changes: 2 additions & 1 deletion test/suite/functional/Input/SpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class SpecificationTest extends TestCase
public function testAllContentTypesArrayPopulatedCorrectly(array $data, array $expectedResult): void
{
$specification = $this->sut->parse($data, '/openapi.yaml');
static::assertSame($specification->getAllContentTypes(), $expectedResult);

static::assertSame($expectedResult, $specification->getAllContentTypes());
}

public function contentTypesTestProvider(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"require": {
"php": ">=7.4",
"docler-labs/api-client-exception": "^1.0",
"ext-intl": "*",
"ext-json": "*",
"guzzlehttp/psr7": "^1.6",
"pimple/pimple": "^3.3",
"psr/container": "^1.0",
Expand Down
Loading

0 comments on commit 860b5a0

Please sign in to comment.