Search code examples
c#openapi.net-8.0nswagfast-endpoints

OpenAPI generates AdditionalProperties field twice


I am generating Swagger client via the OpenAPI but getting an error for additionalProperties as it was defined twice.

The json schema was generated from FastEndpoints project, I tried to add schema filter configuration to disallow the AdditionalPropertiesAllowed - yet no luck.

I'm trying to understand how do we configure the additionalProperties to appear only once in the json schema:

enter image description here

UPDATE:

The issue that I encountered on the generated json schema with additionalProperties defined twice is when I try to generate a CSharpClient through json the client throws an error with additionalProperties defined twice in the model.


Solution

  • You could try experimenting with DocumentSettings like below:

    builder.Services
       .AddFastEndpoints()
       .SwaggerDocument(c => c.DocumentSettings = docSettings =>
       {
           docSettings.PostProcess = doc =>
           {
               foreach (var definition in doc.Definitions)
               {
                   definition.Value.AdditionalPropertiesSchema = null;
                   // and/or 
                   definition.Value.AllowAdditionalProperties = false;
               }
           };
       });
    

    It caused additionalProperties to disappear. So maybe it's worth taking a look.

    Original answer

    This is because in new C# projects, there are nullable reference types (NRTs) turned on by default. Go to your .csproj file and see that you will have set:

    <Nullable>enable</Nullable>
    

    And this will cause Swagger to generate these properties.

    If you would disable NRTs, like below:

    <Nullable>disable</Nullable>
    

    Then Swagger would generate just "additionalProperties": false.

    And the reason there are multiple "additionalProperties" properties, is that this is just applicable for every object, and so Swagger generates this information for each object. Not once.