-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Provide a way to generate an empty JSON security array for an operation #1819
Comments
This appears to be a problem with the underlying OpenAPI.NET library being used by Swagger. I've opened an issue there: microsoft/OpenAPI.NET#521 |
@domaindrivendev @sbebrys Any input into this? It looks like Swashbuckle uses its own version of OpenAPI.NET (the reference in |
I ran into same issue. For OAS 3:
So if you add an empty Also, be aware if you include only one (empty) security requirement, Swagger UI will always call operation anonymously even if user is authenticated. As stated in quoted spec, you will have to add your default security requirement in addition to the empty one if you want Swagger UI to send authentication information when available). For OAS 2: |
Yeah, and the empty object still only makes it optional. I want to make it anonymous only. An empty array is required. |
Did you tried an empty object in the array? |
Didn't you just quote from the spec saying that that means optional security, rather than anonymous only? |
@jez9999 Re-reading the language of the specification I can see the confusion now. I'm not sure why we added the language about the empty array. I'm not exactly sure what is the value of saying "I'm not telling you what the security requirements are for this operation". |
@darrelmiller The value of an empty array is the ability to define default security requirements, and then remove them for certain operations. The OpenAPI spec says that the way you do this is to define your security requirements against the entire API, then use an empty array for operations that can be called anonymously. What I'm having to do now is define a default anonymous requirement and then add security for 95% of my operations. It works but as you can imagine it greatly bloats the JSON and is semantically unintuative. |
@jez9999 it seems you have a solution to your original issue with a simple update to your operation filter:
Therefore I'm going to close this issue |
@domaindrivendev That doesn't create an empty array, it creates an array with one empty object in it. This is still not detected by sawgger-ui as an anonymous operation, and so still displays a padlock. |
@eric-b It doesn't detect it, and I'm not sure I disagree with swagger-ui's behaviour. Basically, the spec says that you list supported security operations, and add the empty object to make them all optional. I think this is a bit of a dodgy choice for the spec to be honest, as it allows for the rather ambiguous IMHO Swashbuckle and/or OpenAPI.NET should be modified to have a property on |
I can confirm I use this "feature" and it works. If you post an example of your swagger document, maybe me or someone else can try to spot why it does not work in your case. I agree on need for OpenAPI.NET to support an empty array because
I'm not sure to agree with this: if "[you] haven't specified any [security requirement at all in your document]", |
@eric-b Can you point me to exactly where in the spec it clearly states that
Does it work in the sense that you use swagger-ui, and operations where global security is overridden using [{}] display nothing in the top-right corner instead of a padlock? The code does not support that:
It only checks |
You are right there is a UI difference between My understanding of padlocks in Swagger-UI is:
IMHO:
For spec pointer, I already gave a link in my first comment. This is my interpretation, and I find spec quite clear in its logic and outcome described here. But you also proved it is not so obvious for everyone. |
@eric-b Yes, but I don't want any padlock appearing for certain of my operations as security simply doesn't make sense for them. Hence I want the empty array to trigger swagger-ui to do that. |
I understand. Empty object in array is a workaround in my use case. As far as I know, if you want an empty array, you'll need to transform response from swagger endpoint with a custom middleware of your own. This is what I've done for an Owin application (OAS 2 does not support empty object in an array). |
Or you could change the Swagger OpenAPI.NET fork to allow an |
I agree on this, but decision is not mine :-). I was trying to help with a workaround, I'm not involved in Swashbuckle project. |
In order to mark operations as requiring authorization in a Swagger document, you can individually mark them one-by-one as requiring security:
However, this will rather bloat the Swagger JSON file by separately specifying the same security for every single operation:
If your API has only a few operations that are anonymous, and don't require authentication, with every other operation requiring the same authentication, the more efficient way to indicate this is to apply a global security requirement and then specify an empty JSON security array for the few operations that are anonymous:
... to generate:
At least, that's what I wish it would generate, which should mark the few anonymous methods I have as anonymous in the Swagger document. Unfortunately, setting
.Security
to an empty list removes the security element altogether, meaning the operation gets the global security applied to it.Can a way be added to get the Swagger document to have an empty JSON security array for certain operations?
The text was updated successfully, but these errors were encountered: