dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.6k stars 10.06k forks source link

Add appsettings.json JSON schemas for areas that aren't already in the schema store #53737

Open eerhardt opened 10 months ago

eerhardt commented 10 months ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

When editing appsettings.json files, we get intellisense for core configuration sections like "ConnectionStrings", "Logging", "Kestrel", and "AllowedHosts". This comes from https://json.schemastore.org/appsettings.json.

However, there are plenty more areas in ASP.NET Core that read from a config section than what is described in the schema store document - for example, Auth sections. In the past we haven't added these to the schema store. I assume this is because these other areas are optional, and might not be respected in your app.

While developing .NET Aspire, we have introduced a new feature that allows NuGet packages to bring in fragments of JSON schema into a project. VS will aggregate all the fragments into a composite JSON schema and use that in the editor. (Note there is work to get this experience outside of VS as well, for example VS Code.) This feature works even outside of .NET Aspire.

Describe the solution you'd like

We should use this new JSON schema fragment feature in all the places in ASP.NET Core that reads configuration sections. That way users can tell what config settings are available, and what the format should be.

Additional context

To use this feature, add some MSBuild logic to the nuget package of the form:

<Project>
  <ItemGroup>
    <JsonSchemaSegment Include="$(MSBuildThisFileDirectory)..\..\ConfigurationSchema.json"
                       FilePathPattern="appsettings\..*json" />
  </ItemGroup>
</Project>

where JsonSchemaSegment Include is the path to the JSON schema fragment file and FilePathPattern is a Regex for the file names this schema segment applies to.

Then you can write a JSON schema that looks like:

{
  "properties": {
    "SectionName": {
      "type": "object",
      "properties": {
        "Property1": {
          "type": "boolean",
          "description": "Gets or sets a value that describes  Property1."
        }
      }
    }
  },
  "type": "object"
}

To assist in creating these schema fragments, we've developed a source generator that can generate the JSON schema given a .NET type. See https://github.com/dotnet/aspire/pull/1383 and https://github.com/dotnet/aspire/tree/main/src/Tools/ConfigurationSchemaGenerator.

eerhardt commented 10 months ago

I don't have a concrete list of all the places that this applies to. Auth sections was the first one that jumps out at me.

Tagging @halter73 @captainsafia @javiercn @SteveSandersonMS @BrennanConroy @JamesNK in case they have more places in mind. Can you list any you can think of here?