Skip to content

feat: defaults parameters#7758

Open
Maxcastel wants to merge 4 commits intoapi-platform:mainfrom
Maxcastel:feature/config-defaults-parameters
Open

feat: defaults parameters#7758
Maxcastel wants to merge 4 commits intoapi-platform:mainfrom
Maxcastel:feature/config-defaults-parameters

Conversation

@Maxcastel
Copy link
Contributor

@Maxcastel Maxcastel commented Feb 13, 2026

Q A
Branch? main
Tickets Closes #7509
License MIT
Doc PR api-platform/docs#...

Description

This PR allows to define global default parameters that are applied to all resources instead of configuring the parameter individually on each resource.

Before

#[ApiResource(
    operations: [
        new GetCollection(
            parameters: [
                new HeaderParameter(
                    key: 'X-API-Token',
                    required: true,
                    description: 'API Token',
                ),
            ],
        ),
    ],
)]
class Book
{

}
#[ApiResource(
    operations: [
        new GetCollection(
            parameters: [
                new HeaderParameter(
                    key: 'X-API-Token',
                    required: true,
                    description: 'API Token',
                ),
            ],
        ),
    ],
)]
class Author
{

}

After

api_platform:
    defaults:
        parameters:
            ApiPlatform\Metadata\HeaderParameter:
                key: 'X-API-Token'
                required: true
                description: 'API Token'
#[ApiResource]
class Book
{

}

#[ApiResource]
class Author
{

}

Other exemple with two Parameter

api_platform:
    title: My API
    version: 1.0.0
    defaults:
        parameters:
            ApiPlatform\Metadata\HeaderParameter:
                key: 'X-API-Token'
                required: true
                description: 'API authentication token'
            
            ApiPlatform\Metadata\QueryParameter:
                key: 'api_version'
                required: false
                description: 'API version'

@Maxcastel Maxcastel changed the title test defaults parameters feat: defaults parameters Feb 13, 2026
@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch 2 times, most recently from 98b777f to e4ad0c9 Compare February 16, 2026 15:39
@Maxcastel Maxcastel marked this pull request as ready for review February 16, 2026 15:48
->booleanNode('hydra')->defaultNull()->info('Include this parameter in Hydra documentation.')->end()
->variableNode('constraints')->defaultNull()->info('Validation constraints.')->end()
->scalarNode('security')->defaultNull()->info('The security expression.')->end()
->scalarNode('security_message')->defaultNull()->info('The security message.')->end()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you use the defineDefault method of this class? It makes this automatic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 1da905e

Copy link
Member

@soyuka soyuka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the automatic method for configuration? the rest is awesome!

@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch 2 times, most recently from 1da905e to 88619ac Compare February 18, 2026 13:02
@Maxcastel
Copy link
Contributor Author

Maxcastel commented Feb 18, 2026

Also changed the priority of DefaultParametersResourceMetadataCollectionFactory (150 to 1001) so that it is higher than ParameterValidationResourceMetadataCollectionFactory (1000) and therefore executed before it, otherwise, it doesn’t work (tested on a real project by changing the version of "api-platform/core" to "dev-feature/config-defaults-parameters", and it works).

Currently, when changing the priority of DefaultParametersResourceMetadataCollectionFactory, all tests still pass. Should we add a functional test that fails if the priority is set to 150 (or any value lower than 1001)? How could we test this behavior?

@soyuka
Copy link
Member

soyuka commented Feb 18, 2026

It's a hard problem + there's no bc-layer on priorities... I suggest to merge the code of your default factory to the one handling parameters. I know it'll be more code in a class but it keeps a better responsibility over this in my opinion.

@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch from bfab314 to 2d31329 Compare February 18, 2026 15:24
Comment on lines 183 to 206
return new $parameterClass(
key: $config['key'] ?? null,
schema: $config['schema'] ?? null,
openApi: null,
provider: null,
filter: $config['filter'] ?? null,
property: $config['property'] ?? null,
description: $config['description'] ?? null,
properties: null,
required: $config['required'] ?? false,
priority: $config['priority'] ?? null,
hydra: $config['hydra'] ?? null,
constraints: $config['constraints'] ?? null,
security: $config['security'] ?? null,
securityMessage: $config['security_message'] ?? null,
extraProperties: $config['extra_properties'] ?? [],
filterContext: null,
nativeType: null,
castToArray: null,
castToNativeType: null,
castFn: null,
default: $config['default'] ?? null,
filterClass: $config['filter_class'] ?? null,
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a an array and name convert all the keys so that you can call __construct with arguments (programatically) we know they match as the configuration does it the other way around

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 87829ee

@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch 4 times, most recently from cf81191 to 177c903 Compare February 19, 2026 10:29
@Maxcastel
Copy link
Contributor Author

Maxcastel commented Feb 19, 2026

Behat tests are failing

@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch from f97e615 to ab7d94a Compare February 19, 2026 13:01
@Maxcastel
Copy link
Contributor Author

Maxcastel commented Feb 19, 2026

I know that existing tests shouldn’t be modified, but many of them were failing due to Hydra.

@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch 5 times, most recently from 002c26d to a765758 Compare February 19, 2026 15:42
@Maxcastel Maxcastel requested a review from soyuka February 19, 2026 15:53
@Maxcastel
Copy link
Contributor Author

Tested again on a real project, and it still works.

@Maxcastel
Copy link
Contributor Author

Maxcastel commented Feb 19, 2026

but many of them were failing due to Hydra.

Same file modified and same test affected as in #7773 (tests/Functional/Parameters/HydraTest::testNoPropertyDescription)

@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch from 8ab2d5a to a0d388e Compare February 20, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

defaults parameters

2 participants

Comments