Skip to content

feat: defaults parameters#7758

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

feat: defaults parameters#7758
Maxcastel wants to merge 3 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 from e4ad0c9 to f41ef4c Compare February 18, 2026 12:15
@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch 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 from 2d31329 to 87829ee Compare February 18, 2026 15:48
@Maxcastel Maxcastel force-pushed the feature/config-defaults-parameters branch 2 times, most recently from 3e4abbb to 12fb7fb Compare February 18, 2026 16:28
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