Skip to content
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

Automatic generation of ConfigurationSchema.json files #1146

Closed
eerhardt opened this issue Nov 30, 2023 · 1 comment · Fixed by #1383
Closed

Automatic generation of ConfigurationSchema.json files #1146

eerhardt opened this issue Nov 30, 2023 · 1 comment · Fixed by #1383
Assignees
Labels
area-integrations Issues pertaining to Aspire Integrations packages

Comments

@eerhardt
Copy link
Member

Today, each Aspire Component has a ConfigurationSchema.json file that is added to the nupkg to provide JSON intellisense in appsettings.json files in the IDE:

"ConfigurationOptions": {
"type": "object",
"description": "The options relevant to a set of redis connections.",
"properties": {
"AbortOnConnectFail": {
"type": "boolean",
"description": "Gets or sets whether connect/configuration timeouts should be explicitly notified via a TimeoutException."
},
"AllowAdmin": {
"type": "boolean",
"description": "Indicates whether admin operations should be allowed."
},

image

Currently these .json files are hand-crafted. We should be able to generate them. My high-level thinking is:

  • Add a "post-compile" task to the build that uses Roslyn APIs to inspect and generate these .json files based on a supplied set of Types and config paths.
  • The ConfigurationSchema.json files stay checked into source control
    • This allows us to inspect, link to, and see the history of the file over time.
  • If the generated file is different than the checked-in file, fail the build.
  • The generator has an MSBuild option to overwrite the file, so when it needs to be updated you can simply dotnet build /p:UpdateConfigurationSchema=true and produce the new file
@eerhardt eerhardt self-assigned this Nov 30, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-integrations Issues pertaining to Aspire Integrations packages label Nov 30, 2023
@eerhardt eerhardt added this to the preview 3 (Jan) milestone Nov 30, 2023
@eerhardt
Copy link
Member Author

Another reason this tool can't be a Roslyn source generator is because it needs to read XML docs for referenced assemblies. For example, the ConfigurationOptions.AllowAdmin property above gets its description from the XML docs on the StackExchange.Redis ConfigurationOptions type. A Roslyn source generator can't get this information due to dotnet/roslyn#51769.

eerhardt added a commit that referenced this issue Dec 15, 2023
Adds a tool that automatically generates ConfigurationSchema.json files for components.

To use the tool, a component adds an assembly attribute. For example:

```C#
[assembly: ConfigurationSchema("Aspire:Azure:Storage:Blobs", typeof(AzureStorageBlobsSettings))]
[assembly: ConfigurationSchema("Aspire:Azure:Storage:Blobs:ClientOptions", typeof(BlobClientOptions), exclusionPaths: ["Default"])]

[assembly: LoggingCategories("Azure", "Azure.Core", "Azure.Identity")]
```

### ConfigurationSchemaAttribute

* The first parameter is the config section path where the type is loaded from.
* The second parameter is the Type that is bound at that path.
* ExclusionPaths - (optional) The config sections to exclude from the ConfigurationSchema. This is useful if there are properties you don't want to publicize in the config schema.

### LoggingCategoriesAttribute
* The list of log categories produced by the component. These categories will show up under the `Logging` section in appsettings.json

This tool works after compilation by scanning the assembly for `ConfigurationSchema` and `LoggingCategories` attributes, and uses Roslyn APIs to inspect all properties of the types. It reuses all the parsing logic from https://github.com/dotnet/runtime/tree/main/src/libraries/Microsoft.Extensions.Configuration.Binder/gen, and using the underlying model objects, generates a JSON schema using System.Text.Json.Nodes APIs.

I've copied the necessary code from dotnet/runtime into the `RuntimeSource` folder for now. This code doesn't need to be reviewed since it is a straight copy from dotnet/runtime. After this PR goes through, we will set up an automatic "sync" action that will update this code anytime changes in the dotnet/runtime code is updated.

Fix #1146
@github-actions github-actions bot locked and limited conversation to collaborators Apr 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-integrations Issues pertaining to Aspire Integrations packages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant