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

AdditionalProperties has AdditionalPropertiesAllowed=false #1502

Closed
runehalfdan opened this issue Jan 23, 2020 · 9 comments
Closed

AdditionalProperties has AdditionalPropertiesAllowed=false #1502

runehalfdan opened this issue Jan 23, 2020 · 9 comments
Labels
needs investigation stale Stale issues or pull requests

Comments

@runehalfdan
Copy link

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.

@richardscholten73
Copy link

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:
OAI/OpenAPI-Specification#668 (comment)

@ffMathy
Copy link

ffMathy commented Mar 23, 2021

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

@ffMathy
Copy link

ffMathy commented Mar 23, 2021

More context: Azure/autorest#3497

@ffMathy
Copy link

ffMathy commented Mar 24, 2021

@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
Copy link

richardscholten73 commented Mar 25, 2021

@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

@whyleee
Copy link

whyleee commented May 26, 2021

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

@DSpirit
Copy link

DSpirit commented Apr 17, 2024

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.

Copy link
Contributor

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 github-actions bot added the stale Stale issues or pull requests label Jun 18, 2024
Copy link
Contributor

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation stale Stale issues or pull requests
Projects
None yet
Development

No branches or pull requests

6 participants