enable scopes for all security scheme types (#17083)

This commit is contained in:
Tomohiko Ozawa
2023-12-09 22:58:01 +09:00
committed by GitHub
parent 583184888f
commit a21e68182a
3 changed files with 17 additions and 6 deletions

View File

@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public class CodegenSecurity {
public String name;
@@ -89,6 +90,16 @@ public class CodegenSecurity {
public CodegenSecurity filterByScopeNames(List<String> filterScopes) {
CodegenSecurity filteredSecurity = new CodegenSecurity(this);
// Since OAS 3.1.0, security scheme types other than "oauth2" and "openIdConnect" may have a list of role names
// which are required for the execution, but are not otherwise defined or exchanged in-band.
// In such cases, no filtering is performed.
if (!(isOAuth || isOpenId)) {
filteredSecurity.scopes = filterScopes.stream()
.map(s -> new HashMap<String, Object>(Map.of("scope", s)))
.collect(Collectors.toList());
return filteredSecurity;
}
if (scopes == null) {
return filteredSecurity;
}

View File

@@ -1594,7 +1594,7 @@ public class DefaultGenerator implements Generator {
for (CodegenSecurity security : authMethods) {
boolean filtered = false;
if (security != null && security.scopes != null) {
if (security != null) {
for (SecurityRequirement requirement : securities) {
List<String> opScopes = requirement.get(security.name);
if (opScopes != null) {

View File

@@ -178,7 +178,7 @@ public interface {{classname}} {
}{{#hasAuthMethods}},
security = {
{{#authMethods}}
@SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes={ {{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}} }{{/isOAuth}}){{^-last}},{{/-last}}
@SecurityRequirement(name = "{{name}}"{{#scopes.0}}, scopes={ {{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}} }{{/scopes.0}}){{^-last}},{{/-last}}
{{/authMethods}}
}{{/hasAuthMethods}}{{#externalDocs}},
externalDocs = @ExternalDocumentation(description = "{{externalDocs.description}}", url = "{{externalDocs.url}}"){{/externalDocs}}
@@ -196,16 +196,16 @@ public interface {{classname}} {
responseContainer = "{{{.}}}"{{/returnContainer}}{{#hasAuthMethods}},
authorizations = {
{{#authMethods}}
{{#isOAuth}}
{{#scopes.0}}
@Authorization(value = "{{name}}", scopes = {
{{#scopes}}
@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{^-last}},{{/-last}}
{{/scopes}}
}){{^-last}},{{/-last}}
{{/isOAuth}}
{{^isOAuth}}
{{/scopes.0}}
{{^scopes.0}}
@Authorization(value = "{{name}}"){{^-last}},{{/-last}}
{{/isOAuth}}
{{/scopes.0}}
{{/authMethods}} }{{/hasAuthMethods}}
)
@ApiResponses({