Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Config][FrameworkBundle] Generate JSON schema for config#59620

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
valtzu wants to merge1 commit intosymfony:7.4fromvaltzu:dx-config-schema

Conversation

@valtzu
Copy link
Contributor

@valtzuvaltzu commentedJan 26, 2025
edited
Loading

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
IssuesFix#59603
LicenseMIT

Generate JSON schema in%kernel.build_dir%/config.schema.json for YAML config when the container is built to improve DX.

To discuss:

  1. Is there any way to deal with PHP enums (!php/enum yaml tag)? At the moment they are just ignored/excluded // just allowed a string!php/enum ...
  2. Should we include# $schema: path/to/schema.json in generated yaml files? probably no / up to Flex etc
  3. Any edge-cases I missed?
  4. Should we rather bumpsymfony/config version constraint insymfony/framework-bundle than check for class existence? This is kinda optional stuff so I thought we don't need to enforce it. already bumped in[FrameworkBundle] bump symfony/config requirement to 7.3 #59649

GromNaN, chapterjason, PierreCapel, and Jean-Beru reacted with thumbs up emoji
@GromNaN
Copy link
Member

GromNaN commentedJan 26, 2025
edited
Loading

Thank you very much for working on this feature.
I gave it a try and it works really well.

  • The per-environment configuration need to be added. This is the same as the current root definition (that need to be a type), with key following this pattern:when@[a-zA-Z0-9]+
  • The JSON schema file will be different for each environment, because the bundles are not the sames. The JSON schema must allow any additional root keys forwhen@ other environments.

Here is the files that are not validated by the generated JSON schema.

doctrine.yaml

  • Somedoctrine configuration are not recognized.
image

framework.yaml

routing.yaml

  • "Missing required property 'resource'. Since the configurations are merged from multiple files (and defaults) properties cannot be required in a single Yaml file.

validator.yaml

@valtzu
Copy link
ContributorAuthor

valtzu commentedJan 26, 2025
edited
Loading

Thanks for quick review/comment@GromNaN!

Resolvedwhen@(though it does not account for env-specific extensions yet), removed"required": [...] from the schema, added usingequivalentValues.

The validator issue still on to-do list, not sure on what basis to allow the empty array –maybeuseAttributeAsKey just addedmaxItems: 0 to non-prototyped arrays.

The doctrine issue seems to be caused bynormalization in a closure, some possible solutions below 👇

  1. always allow extra keys / have"additionalProperties": true in the schema (for every subschema), not to cause false-positives on keys that for whatever reason don't appear in the schema
  2. do some manual override / manually defined schema to cover this special case.
  3. convert(dynamically?)the xml schema to json schema(not sure if it matches yaml one-to-one)
GromNaN reacted with thumbs up emoji

@GromNaN
Copy link
Member

Resolvedwhen@(though it does not account for env-specific extensions yet), removed"required": [...] from the schema, added usingequivalentValues.

Thanks, perfect.

The validator issue still on to-do list, not sure on what basis to allow the empty array – maybeuseAttributeAsKey 🤔

I think the JSON schema can be a bit more strict than the config validation rules. But this error may be hiding something else.

The doctrine issue seems to be caused bynormalization in a closure, some possible solutions below 👇

  1. always allow extra keys / have"additionalProperties": true in the schema (for every subschema), not to cause false-positives on keys that for whatever reason don't appear in the schema

Arbitrary additional properties are not allowed by the Config component. This would fail the purpose of the JSON schema.

  1. do some manual override / manually defined schema to cover this special case.

It's the last option if you really can't do any better.

  1. convert(dynamically?)the xml schema to json schema(not sure if it matches yaml one-to-one)

I'd prefer to be able to generate the xsd rather than use it.

Maybe we don't need to support this for a first version, since there's an alternative syntax (more verbose).

@valtzuvaltzuforce-pushed thedx-config-schema branch 2 times, most recently from84f3965 to008ee37CompareJanuary 27, 2025 22:23
Copy link
Member

@GromNaNGromNaN left a comment

Choose a reason for hiding this comment

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

I did a quick code review.

valtzu reacted with thumbs up emoji
@valtzuvaltzuforce-pushed thedx-config-schema branch 3 times, most recently from3cfc28e tode09b68CompareJanuary 29, 2025 18:55
@valtzu
Copy link
ContributorAuthor

Did a whole lot of cleanup based on the comments.

I also managed to workaround the Doctrine issue by allowing any type when$node->getNormalizedTypes() containsExprBuilder::TYPE_ANY.

This of course means we don't get a schema for those, but I think it's better than false-positives when using example configuration.

Also added a deprecation message to the schema which shows up quite nicely now

image

GromNaN and alamirault reacted with thumbs up emoji

@GromNaN
Copy link
Member

Awesome, the deprecation message is exactly why I want such schema validation.

@valtzu
Copy link
ContributorAuthor

valtzu commentedFeb 3, 2025
edited
Loading

I'm a little stuck with the support for env-specific extensions: since the schema is generated inside the kernel, it does not seem possible to generate schemas for other environments. And since the kernel class is not really exposed anywhere, it's not trivial to create multiple instances of the kernel with different envs (not that it would be a great idea anyway).

Best I could think of is

  1. always only build schema for the current environment
  2. in addition to saving the schema into single file inside env-specific directory, also insert a reference into common file outside env-specific dir

This could look something like

var/cache/dev/config.schema.jsonvar/cache/test/config.schema.jsonvar/cache/whatever/config.schema.jsonvar/config.schema.json # references all files above, like {"when@dev": { "$ref": "cache/dev/config.schema.json" } }

The downside with this solution is that unless you build the container for those other envs, you also don't get the schema for those. Also, I doubt that many people build the container locally for any env butdev &test.


If someone has a better approach, great!

Copy link
Member

@GromNaNGromNaN left a comment

Choose a reason for hiding this comment

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

We could look atbundles.php to find all the environments and bundles used, but that wouldn't cover generic use cases (if bundles are registered directly in Kernel.php, for example).

We could also scan all the directories and find all the classes that implement theBundleInterface, but that would require scanning composer autoloader and would take up too many bundles.

I think the idea of generating independent schema files for each env is more realist:

  • generatevar/cache/dev/config.schema.json,var/cache/test/config.schema.json andvar/cache/prod/config.schema.json.
  • when a file is generated, create or update the parentvar/cache/config.schema.json to include the generated fields.

I tried this with PHPStorm, but the file loading doesn't work yet.

{"$schema":"http://json-schema.org/draft-06/schema#","allOf": [    {"$ref":"./dev/config.schema.json" },    {"$ref":"./test/config.schema.json" }  ]}

@valtzuvaltzuforce-pushed thedx-config-schema branch 2 times, most recently from8715b5d to003b6f9CompareFebruary 18, 2025 21:16
@valtzu
Copy link
ContributorAuthor

valtzu commentedFeb 18, 2025
edited
Loading

I tried this with PHPStorm, but the file loading doesn't work yet.

Yeah, that is sad. I ended up putting everything in a single file,%kernel.build_dir%/../config.schema.json.

Also switched fromarray to\stdClass because after merging configs between envs I was getting[] where{} was expected, breaking the whole schema.

So now you'd need to run

bin/console --env=devbin/console --env=testbin/console --env=prod

etc to populate all the configs – though in reality I think most projects have all the bundles & extensions available indev, thus nothing extra exceptdev build should be needed.

@nicolas-grekas
Copy link
Member

Instead of one big file, we could generate one file per bundle.
That would solve the problem ofwhen@ and that would align with the practice of splitting config files per bundle.

GromNaN reacted with thumbs up emoji

@GromNaN
Copy link
Member

Instead of one big file, we could generate one file per bundle. That would solve the problem ofwhen@ and that would align with the practice of splitting config files per bundle.

That's a very good idea, and it would solve our issue.

@valtzu
Copy link
ContributorAuthor

valtzu commentedJul 31, 2025
edited
Loading

It would only solve the issue if we iterated the bundles in some other way than getting the bundles from theKernel#getBundles(), because it's not guaranteed that any environment has all the bundles available. And I don't think it's possible to do since the user may not havebundles.php at all, instead, have bundles inlined in theApp\Kernel class, and even have custom php logic when to enable a bundle.


Another issue:previously we faced the issue where PHPStorm does not understand referenced schemas – if that's still the case, then a file per bundle is not too great. Of course one can say "it's not a Symfony issue" but no DX is improved if the IDE doesn't use the schema(s).


I have to admit this PR is too much work for me right now, especially since a brick wall was hit due tobeforeNormalization stuff(to solve this, only reasonable option I see would be to include hard-coded json schemas in bundles, similar to#61282). If someone wants to continue from here / start over, feel free.

fabpot added a commit that referenced this pull requestAug 22, 2025
…idating and autocompleting YAML config files (nicolas-grekas)This PR was merged into the 7.4 branch.Discussion----------[DependencyInjection][Routing] Add JSON schema for validating and autocompleting YAML config files| Q             | A| ------------- | ---| Branch?       | 7.4| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Issues        |#60572| License       | MITRelated to#59620This provides autocompletion/validation on IDEs that support JSON schema for YAML files, such as phpStorm. Note that this cannot support yaml tags, so no autocompletion after `!service` for example.The only way to provide this would be to ship a language server. Out of reach I fear.Recipe update atsymfony/recipes#1447Commits-------f535cc6 [DependencyInjection][Routing] Add JSON schema for validating and autocompleting YAML config files
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull requestAug 22, 2025
…idating and autocompleting YAML config files (nicolas-grekas)This PR was merged into the 7.4 branch.Discussion----------[DependencyInjection][Routing] Add JSON schema for validating and autocompleting YAML config files| Q             | A| ------------- | ---| Branch?       | 7.4| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Issues        | #60572| License       | MITRelated tosymfony/symfony#59620This provides autocompletion/validation on IDEs that support JSON schema for YAML files, such as phpStorm. Note that this cannot support yaml tags, so no autocompletion after `!service` for example.The only way to provide this would be to ship a language server. Out of reach I fear.Recipe update atsymfony/recipes#1447Commits-------f535cc60622 [DependencyInjection][Routing] Add JSON schema for validating and autocompleting YAML config files
symfony-splitter pushed a commit to symfony/routing that referenced this pull requestAug 22, 2025
…idating and autocompleting YAML config files (nicolas-grekas)This PR was merged into the 7.4 branch.Discussion----------[DependencyInjection][Routing] Add JSON schema for validating and autocompleting YAML config files| Q             | A| ------------- | ---| Branch?       | 7.4| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Issues        | #60572| License       | MITRelated tosymfony/symfony#59620This provides autocompletion/validation on IDEs that support JSON schema for YAML files, such as phpStorm. Note that this cannot support yaml tags, so no autocompletion after `!service` for example.The only way to provide this would be to ship a language server. Out of reach I fear.Recipe update atsymfony/recipes#1447Commits-------f535cc60622 [DependencyInjection][Routing] Add JSON schema for validating and autocompleting YAML config files
Copy link
Member

@GromNaNGromNaN left a comment

Choose a reason for hiding this comment

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

We are making changes in other PRs to unlock this feature.

private function createObjectSchema(ArrayNode $node, \stdClass $definitions): \stdClass
{
$schema = (object) [
'type' => 'object',
Copy link
Member

Choose a reason for hiding this comment

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

If additional properties are allowed, we loose the validation on property names.
I think they can be disallowed once alternative documentation formats are fully described:#51273

Suggested change
'type' =>'object',
'type' =>'object',
'additionalProperties' => false,

Comment on lines +112 to +114
if (\in_array(ExprBuilder::TYPE_ANY, $normalizedTypes)) {
// This will make IDEs not complain about configurations containing complex beforeNormalization logic
$subSchemas[] = new \stdClass();
}
Copy link
Member

Choose a reason for hiding this comment

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

This block also need to be reviewed if we can provide a better description of alternative config trees.

@valtzu
Copy link
ContributorAuthor

Closing in favor of#62125

@valtzuvaltzu closed thisOct 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@GromNaNGromNaNGromNaN requested changes

Assignees

No one assigned

Projects

None yet

Milestone

7.4

Development

Successfully merging this pull request may close these issues.

[DX][Config] Generate JSON schema for Yaml configuration

5 participants

@valtzu@GromNaN@nicolas-grekas@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp