I am using the following packages:
I have set everything up to use OpenAPI.
When adding swagger generation with the code below I get:
services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("bearer", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
Description = "JWT Authorization header using the Bearer scheme. Example: \"Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri($"https://login.microsoftonline.com/{configuration["AzureAD:TenantId"]}/oauth2/authorize", UriKind.RelativeOrAbsolute),
Scopes = new Dictionary<string, string>
{
{
"user_impersonation", "Access api"
}
}
}
}
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.Header, Id = "Authorization" }
},
new[] { "user_impersonation" }
}
});
I may be doing things wrong, but I have tried to find ways to do this and it seems to me that there are settings/properties missing.
Please enlighten me, is this is a bug or not.
Kind regards
Thomas
It looks like this generates a invalid JSON file at /swagger/v1/swagger.json
"security": [
{{
"$ref": "#/components/headers/Authorization"
}[
"user_impersonation"
]
}
]
I haven't encountered this issue - could you create, and point me to, a minimal sample project (from scratch) that repro's the issue?
I experienced the same issue as @ThomasCronholm. I had a project using Swashbuckle.AspNetCore 5.0.0-rc2 with a configuration that calls AddSecurityDefinition followed by AddSecurityRequirement with a OpenApiSecurityScheme.Reference.
@domaindrivendev
Sorry for the late reply.
Here is a solution that reproduces the parse error.
I have removed ClientId and TenantId from the appsettings file, it gives the same result with and without them.
https://1drv.ms/u/s!AlDs6Q09M-TmibNVqh6FoeqVN9vUyA
BR
Thomas
In your call to AddSecurityRequirement you need to _reference_ the SecurityScheme that you've just defined. So ... replace
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.Header, Id = "Authorization" }
}
with ...
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "bearer" }
}
Thanks, that worked.
Here is my configuration if anyone wonders.
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Acme API", Version = "v1" });
c.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
Description = "JWT Authorization header using the Bearer scheme."
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "bearerAuth" }
},
new string[] {}
}
});
});
@domaindrivendev Works for me as well, thank you.
I am using Swashbuckle.AspnetCore (4.0.1) and get an error only on our webserver...localhost runs fine.
Unable to render this definition
The provided definition does not specify a valid version field.
Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).
I thought it was related to jwt authentication (configured but not active) and was going to try the above, but I am unable set the the SwaggerDoc( "v1" , Info parameter to new OpenApiInfo , I get a parameter mismatch compile error...thought someone might know what I am doing wrong...thanks in advance.
I am using Swashbuckle.AspnetCore (4.0.1) and get an error only on our webserver...localhost runs fine.
Unable to render this definition
The provided definition does not specify a valid version field.Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).
I thought it was related to jwt authentication (configured but not active) and was going to try the above, but I am unable set the the SwaggerDoc( "v1" , Info parameter to new OpenApiInfo , I get a parameter mismatch compile error...thought someone might know what I am doing wrong...thanks in advance.
I'm experiencing an identical issue, but I'm actually using OpenApiInfo as a parameter and argument.
In fact my json begins like this:
{
"openapi": "3.0.1",
"info": {
"title": "Personal Site Api",
"version": "v1"
},
...
},
I'm running into this issue with v5.0.0.
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0" />
Works locally, but once I deploy it shows the error.
I'm running into this issue with v5.0.0.
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0" />Works locally, but once I deploy it shows the error.
Having same problem here. With Swashbukle.AspNetCore version 5.5.1, runs fine locally but get the error when deploying to Azure.
I'm running into this issue with v5.0.0.
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0" />Works locally, but once I deploy it shows the error.
Having same problem under version 5.6.3 on published server. any solution?
Most helpful comment
Thanks, that worked.
Here is my configuration if anyone wonders.