Skip to content

fix(openapi): vacuum instead of swagger-cli #7130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -470,26 +470,6 @@ jobs:
export PATH="$PATH:$HOME/.composer/vendor/bin"
php-coveralls --coverage_clover=build/logs/behat/clover.xml
continue-on-error: true
- name: Export OpenAPI documents
run: |
mkdir -p build/out/openapi
tests/Fixtures/app/console api:openapi:export -o build/out/openapi/openapi_v3.json
tests/Fixtures/app/console api:openapi:export --yaml -o build/out/openapi/openapi_v3.yaml
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '14'
- name: Validate OpenAPI documents
run: |
npx swagger-cli validate build/out/openapi/openapi_v3.json
npx swagger-cli validate build/out/openapi/openapi_v3.yaml
- name: Upload OpenAPI artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: openapi-docs-php${{ matrix.php }}
path: build/out/openapi
continue-on-error: true

postgresql:
name: Behat (PHP ${{ matrix.php }}) (PostgreSQL)
Expand Down Expand Up @@ -1272,26 +1252,53 @@ jobs:
name: behat-logs-php${{ matrix.php }}
path: build/logs/behat
continue-on-error: true

openapi:
name: OpenAPI
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
matrix:
php:
- '8.4'
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: pecl, composer
extensions: intl, bcmath, curl, openssl, mbstring, pdo_sqlite, mongodb
ini-values: memory_limit=-1
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Get composer cache directory
id: composercache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Update project dependencies
run: |
composer global require soyuka/pmu
composer global config allow-plugins.soyuka/pmu true --no-interaction
composer global link .
- name: Clear test app cache
run: tests/Fixtures/app/console cache:clear --ansi
- name: Export OpenAPI documents
run: |
mkdir -p build/out/openapi
tests/Fixtures/app/console api:openapi:export -o build/out/openapi/openapi_v3.json
tests/Fixtures/app/console api:openapi:export --yaml -o build/out/openapi/openapi_v3.yaml
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '14'
- name: Validate OpenAPI documents
run: |
npx swagger-cli validate build/out/openapi/openapi_v3.json
npx swagger-cli validate build/out/openapi/openapi_v3.yaml
- name: Upload OpenAPI artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: openapi-docs-php${{ matrix.php }}
path: build/out/openapi
continue-on-error: true
npx @quobix/vacuum lint -r tests/Fixtures/app/ruleset.yaml build/out/openapi/openapi_v3.yaml -d

laravel:
name: Laravel (PHP ${{ matrix.php }})
Expand Down
54 changes: 47 additions & 7 deletions src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,15 @@
continue;
}

$parameter = new Parameter($parameterName, 'path', $uriVariable->getDescription() ?? "$resourceShortName identifier", $uriVariable->getRequired() ?? true, false, false, $uriVariable->getSchema() ?? ['type' => 'string']);
$parameter = new Parameter(
$parameterName,
'path',
$uriVariable->getDescription() ?? "$resourceShortName identifier",
$uriVariable->getRequired() ?? true,
false,
null,
$uriVariable->getSchema() ?? ['type' => 'string'],
);

if ($linkParameter = $uriVariable->getOpenApi()) {
$parameter = $this->mergeParameter($parameter, $linkParameter);
Expand Down Expand Up @@ -329,7 +337,15 @@
}

$in = $p instanceof HeaderParameterInterface ? 'header' : 'query';
$defaultParameter = new Parameter($key, $in, $p->getDescription() ?? "$resourceShortName $key", $p->getRequired() ?? false, false, false, $p->getSchema() ?? ['type' => 'string']);
$defaultParameter = new Parameter(
$key,
$in,
$p->getDescription() ?? "$resourceShortName $key",
$p->getRequired() ?? false,
false,
null,
$p->getSchema() ?? ['type' => 'string'],
);

$linkParameter = $p->getOpenApi();
if (null === $linkParameter) {
Expand Down Expand Up @@ -752,15 +768,15 @@
$description['description'] ?? '',
$description['required'] ?? false,
$description['openapi']['deprecated'] ?? false,
$description['openapi']['allowEmptyValue'] ?? true,
$description['openapi']['allowEmptyValue'] ?? null,

Check warning on line 771 in src/OpenApi/Factory/OpenApiFactory.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Factory/OpenApiFactory.php#L771

Added line #L771 was not covered by tests
$schema,
'array' === $schema['type'] && \in_array(
$description['type'],
[$arrayValueType, $objectValueType],
true
) ? 'deepObject' : 'form',
$description['openapi']['explode'] ?? ('array' === $schema['type']),
$description['openapi']['allowReserved'] ?? false,
$description['openapi']['allowReserved'] ?? null,

Check warning on line 779 in src/OpenApi/Factory/OpenApiFactory.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Factory/OpenApiFactory.php#L779

Added line #L779 was not covered by tests
$description['openapi']['example'] ?? null,
isset(
$description['openapi']['examples']
Expand All @@ -777,7 +793,15 @@
$parameters = [];

if ($operation->getPaginationEnabled() ?? $this->paginationOptions->isPaginationEnabled()) {
$parameters[] = new Parameter($this->paginationOptions->getPaginationPageParameterName(), 'query', 'The collection page number', false, false, true, ['type' => 'integer', 'default' => 1]);
$parameters[] = new Parameter(
$this->paginationOptions->getPaginationPageParameterName(),
'query',
'The collection page number',
false,
false,
null,
['type' => 'integer', 'default' => 1],
);

if ($operation->getPaginationClientItemsPerPage() ?? $this->paginationOptions->getClientItemsPerPage()) {
$schema = [
Expand All @@ -790,12 +814,28 @@
$schema['maximum'] = $maxItemsPerPage;
}

$parameters[] = new Parameter($this->paginationOptions->getItemsPerPageParameterName(), 'query', 'The number of items per page', false, false, true, $schema);
$parameters[] = new Parameter(
$this->paginationOptions->getItemsPerPageParameterName(),
'query',
'The number of items per page',
false,
false,
null,
$schema,
);
}
}

if ($operation->getPaginationClientEnabled() ?? $this->paginationOptions->isPaginationClientEnabled()) {
$parameters[] = new Parameter($this->paginationOptions->getPaginationClientEnabledParameterName(), 'query', 'Enable or disable pagination', false, false, true, ['type' => 'boolean']);
$parameters[] = new Parameter(
$this->paginationOptions->getPaginationClientEnabledParameterName(),
'query',
'Enable or disable pagination',
false,
false,
null,
['type' => 'boolean'],
);
}

return $parameters;
Expand Down
10 changes: 5 additions & 5 deletions src/OpenApi/Model/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{
use ExtensionTrait;

public function __construct(private string $name, private string $in, private string $description = '', private bool $required = false, private bool $deprecated = false, private bool $allowEmptyValue = false, private array $schema = [], private ?string $style = null, private bool $explode = false, private bool $allowReserved = false, private $example = null, private ?\ArrayObject $examples = null, private ?\ArrayObject $content = null)
public function __construct(private string $name, private string $in, private string $description = '', private bool $required = false, private bool $deprecated = false, private ?bool $allowEmptyValue = null, private array $schema = [], private ?string $style = null, private bool $explode = false, private ?bool $allowReserved = null, private $example = null, private ?\ArrayObject $examples = null, private ?\ArrayObject $content = null)
{
if (null === $style) {
if ('query' === $in || 'cookie' === $in) {
Expand Down Expand Up @@ -53,12 +53,12 @@
return $this->deprecated;
}

public function canAllowEmptyValue(): bool
public function canAllowEmptyValue(): ?bool

Check warning on line 56 in src/OpenApi/Model/Parameter.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Model/Parameter.php#L56

Added line #L56 was not covered by tests
{
return $this->allowEmptyValue;
}

public function getAllowEmptyValue(): bool
public function getAllowEmptyValue(): ?bool
{
return $this->allowEmptyValue;
}
Expand All @@ -83,12 +83,12 @@
return $this->explode;
}

public function canAllowReserved(): bool
public function canAllowReserved(): ?bool

Check warning on line 86 in src/OpenApi/Model/Parameter.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Model/Parameter.php#L86

Added line #L86 was not covered by tests
{
return $this->allowReserved;
}

public function getAllowReserved(): bool
public function getAllowReserved(): ?bool
{
return $this->allowReserved;
}
Expand Down
44 changes: 21 additions & 23 deletions src/OpenApi/Tests/Factory/OpenApiFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@
in: 'query',
description: 'Test modified collection page number',
required: false,
allowEmptyValue: true,
schema: ['type' => 'integer', 'default' => 1],
),
],
Expand Down Expand Up @@ -268,7 +267,7 @@

$baseOperation = (new HttpOperation())->withTypes(['http://schema.example.com/Dummy'])->withInputFormats(self::OPERATION_FORMATS['input_formats'])->withOutputFormats(self::OPERATION_FORMATS['output_formats'])->withClass(Dummy::class)->withShortName('Parameter')->withDescription('This is a dummy');
$parameterResource = (new ApiResource())->withOperations(new Operations([
'uriVariableSchema' => (new Get(uriTemplate: '/uri_variable_uuid', uriVariables: ['id' => new Link(schema: ['type' => 'string', 'format' => 'uuid'], description: 'hello', required: true, openApi: new Parameter('id', 'path', allowEmptyValue: true))]))->withOperation($baseOperation),
'uriVariableSchema' => (new Get(uriTemplate: '/uri_variable_uuid', uriVariables: ['id' => new Link(schema: ['type' => 'string', 'format' => 'uuid'], description: 'hello', required: true, openApi: new Parameter('id', 'path'))]))->withOperation($baseOperation),

Check warning on line 270 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L270

Added line #L270 was not covered by tests
'parameters' => (new Put(uriTemplate: '/parameters', parameters: [
'foo' => new HeaderParameter(description: 'hi', schema: ['type' => 'string', 'format' => 'uuid']),
]))->withOperation($baseOperation),
Expand Down Expand Up @@ -477,7 +476,7 @@
'type' => 'string',
'required' => true,
'strategy' => 'exact',
'openapi' => new Parameter(in: 'query', name: 'name', example: 'bar', deprecated: true, allowEmptyValue: true, allowReserved: true, explode: true),
'openapi' => new Parameter(in: 'query', name: 'name', example: 'bar', deprecated: true, allowReserved: true, explode: true),

Check warning on line 479 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L479

Added line #L479 was not covered by tests
]]),
'f2' => new DummyFilter(['ha' => [
'property' => 'foo',
Expand Down Expand Up @@ -690,16 +689,16 @@
'Retrieves the collection of Dummy resources.',
null,
[
new Parameter('page', 'query', 'Test modified collection page number', false, false, true, [
new Parameter('page', 'query', 'Test modified collection page number', false, false, null, [

Check warning on line 692 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L692

Added line #L692 was not covered by tests
'type' => 'integer',
'default' => 1,
]),
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, true, [
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, null, [

Check warning on line 696 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L696

Added line #L696 was not covered by tests
'type' => 'integer',
'default' => 30,
'minimum' => 0,
]),
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, true, [
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, null, [

Check warning on line 701 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L701

Added line #L701 was not covered by tests
'type' => 'boolean',
]),
]
Expand Down Expand Up @@ -935,29 +934,29 @@
'Retrieves the collection of Dummy resources.',
null,
[
new Parameter('page', 'query', 'The collection page number', false, false, true, [
new Parameter('page', 'query', 'The collection page number', false, false, null, [

Check warning on line 937 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L937

Added line #L937 was not covered by tests
'type' => 'integer',
'default' => 1,
]),
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, true, [
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, null, [

Check warning on line 941 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L941

Added line #L941 was not covered by tests
'type' => 'integer',
'default' => 30,
'minimum' => 0,
]),
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, true, [
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, null, [

Check warning on line 946 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L946

Added line #L946 was not covered by tests
'type' => 'boolean',
]),
new Parameter('name', 'query', '', true, true, true, [
new Parameter('name', 'query', '', true, true, null, [

Check warning on line 949 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L949

Added line #L949 was not covered by tests
'type' => 'string',
], 'form', true, true, 'bar'),
new Parameter('ha', 'query', '', false, false, false, [
new Parameter('ha', 'query', '', false, false, null, [

Check warning on line 952 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L952

Added line #L952 was not covered by tests
'type' => 'integer',
]),
new Parameter('toto', 'query', '', true, false, false, [
new Parameter('toto', 'query', '', true, false, null, [

Check warning on line 955 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L955

Added line #L955 was not covered by tests
'type' => 'array',
'items' => ['type' => 'string'],
], 'deepObject', true),
new Parameter('order[name]', 'query', '', false, false, false, [
new Parameter('order[name]', 'query', '', false, false, null, [

Check warning on line 959 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L959

Added line #L959 was not covered by tests
'type' => 'string',
'enum' => ['asc', 'desc'],
]),
Expand All @@ -981,17 +980,17 @@
'Retrieves the collection of Dummy resources.',
null,
[
new Parameter('page', 'query', 'The collection page number', false, false, true, [
new Parameter('page', 'query', 'The collection page number', false, false, null, [

Check warning on line 983 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L983

Added line #L983 was not covered by tests
'type' => 'integer',
'default' => 1,
]),
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, true, [
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, null, [

Check warning on line 987 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L987

Added line #L987 was not covered by tests
'type' => 'integer',
'default' => 20,
'minimum' => 0,
'maximum' => 80,
]),
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, true, [
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, null, [

Check warning on line 993 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L993

Added line #L993 was not covered by tests
'type' => 'boolean',
]),
]
Expand Down Expand Up @@ -1186,16 +1185,16 @@
'Retrieves the collection of Dummy resources.',
null,
[
new Parameter('page', 'query', 'The collection page number', false, false, true, [
new Parameter('page', 'query', 'The collection page number', false, false, null, [

Check warning on line 1188 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L1188

Added line #L1188 was not covered by tests
'type' => 'integer',
'default' => 1,
]),
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, true, [
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, null, [

Check warning on line 1192 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L1192

Added line #L1192 was not covered by tests
'type' => 'integer',
'default' => 30,
'minimum' => 0,
]),
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, true, [
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, null, [

Check warning on line 1197 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L1197

Added line #L1197 was not covered by tests
'type' => 'boolean',
]),
]
Expand Down Expand Up @@ -1233,7 +1232,6 @@
), $emptyRequestBodyPath->getPost());

$parameter = $paths->getPath('/uri_variable_uuid')->getGet()->getParameters()[0];
$this->assertTrue($parameter->getAllowEmptyValue());
$this->assertEquals(['type' => 'string', 'format' => 'uuid'], $parameter->getSchema());

$parameter = $paths->getPath('/parameters')->getPut()->getParameters()[0];
Expand Down Expand Up @@ -1265,16 +1263,16 @@
'Retrieves the collection of Dummy resources.',
null,
[
new Parameter('page', 'query', 'The collection page number', false, false, true, [
new Parameter('page', 'query', 'The collection page number', false, false, null, [

Check warning on line 1266 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L1266

Added line #L1266 was not covered by tests
'type' => 'integer',
'default' => 1,
]),
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, true, [
new Parameter('itemsPerPage', 'query', 'The number of items per page', false, false, null, [

Check warning on line 1270 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L1270

Added line #L1270 was not covered by tests
'type' => 'integer',
'default' => 30,
'minimum' => 0,
]),
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, true, [
new Parameter('pagination', 'query', 'Enable or disable pagination', false, false, null, [

Check warning on line 1275 in src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php#L1275

Added line #L1275 was not covered by tests
'type' => 'boolean',
]),
],
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/TestBundle/ApiResource/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ApiPlatform\OpenApi\Model\Operation;

#[ApiResource(
description: 'A resource used for OpenAPI tests.',

Check warning on line 26 in tests/Fixtures/TestBundle/ApiResource/Crud.php

View check run for this annotation

Codecov / codecov/patch

tests/Fixtures/TestBundle/ApiResource/Crud.php#L26

Added line #L26 was not covered by tests
operations: [
new Get(),
new GetCollection(openapi: new Operation(extensionProperties: [OpenApiFactory::API_PLATFORM_TAG => ['internal', 'anotherone']])),
Expand Down
Loading
Loading