Swashbuckle.aspnetcore: AdditionalProperties has AdditionalPropertiesAllowed=false

Created on 23 Jan 2020  路  5Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

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.

needs investigation

Most helpful comment

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

All 5 comments

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

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

@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?

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brucewilkins picture brucewilkins  路  3Comments

mrmartan picture mrmartan  路  3Comments

jluqueba picture jluqueba  路  4Comments

engelhardtda picture engelhardtda  路  3Comments

rgelb picture rgelb  路  3Comments