Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

AdditionalProperties has AdditionalPropertiesAllowed=false

Open runehalfdan opened this issue 5 years ago • 7 comments

When JsonDictionaryHandler sets the OpenApiSchema.AdditionalProperties, that OpenApiSchema will have AdditionalPropertiesAllowed == false. This in turns will add "additionalProperties": false to every AdditionalProperties-entry in the generated swagger.

Probably not a big issue, but I think it is unnecessary.

runehalfdan avatar Jan 23 '20 12:01 runehalfdan

Actually it is a big issue. (for me at least)

Microsofts autorest tool does not accept a boolean value for additionalProperties and crashes. Of course it should just accept these values because the spec dictates that it can be either a boolean or a schema, but if there are no additional properties the field should just be omitted.

As a workaround i implemented a DocumentFilter:

using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace XXX.Middleware {
  public class AdditionalParametersDocumentFilter : IDocumentFilter {
    public void Apply(OpenApiDocument openApiDoc, DocumentFilterContext context) {
      foreach(var schema in context.SchemaRepository.Schemas) {
        if(schema.Value.AdditionalProperties == null) {
          schema.Value.AdditionalPropertiesAllowed = true;
        }
      }
    }
  }
}

Oddly setting the AdditionalPropertiesAllowed removes the AdditionalProperties

and in the startup:

    services.AddSwaggerGen(options => {
...
        options.DocumentFilter<AdditionalParametersDocumentFilter>();
 ...
      });

Also mentioned in: https://github.com/OAI/OpenAPI-Specification/issues/668#issuecomment-281837896

richardscholten73 avatar Jan 29 '20 16:01 richardscholten73

This happens to me as well. I can't use Swashbuckle until it is fixed.

ffMathy avatar Mar 23 '21 14:03 ffMathy

More context: https://github.com/Azure/autorest/issues/3497

ffMathy avatar Mar 23 '21 14:03 ffMathy

@richardscholten73 your workaround didn't work for me unfortunately. I still get those properties. I don't get how it's different from your setup somehow. Could there be something else involved?

ffMathy avatar Mar 24 '21 08:03 ffMathy

@richardscholten73 your workaround didn't work for me unfortunately. I still get those properties. I don't get how it's different from your setup somehow. Could there be something else involved?

Not that i know of. It's been awhile since i worked on that project. Maybe the workaround does not work anymore on newer versions? My project is on 5.5.1

richardscholten73 avatar Mar 25 '21 07:03 richardscholten73

@ffMathy the workaround works for me in latest Swashbuckle version 6.1.4. Make sure you register AdditionalParametersDocumentFilter after all other filters, so it can update all schemas properly 😉

whyleee avatar May 26 '21 15:05 whyleee

Worked like a charm, thank you! As the additionalProperties are set to true by default (at least in Azure API Management), the generated Swagger document just omits the whole property using your filter.

DSpirit avatar Apr 17 '24 06:04 DSpirit

This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.

github-actions[bot] avatar Jun 18 '24 01:06 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jul 10 '24 01:07 github-actions[bot]