Skip to content

Commit

Permalink
enable scopes for all security scheme types (#17083)
Browse files Browse the repository at this point in the history
  • Loading branch information
kota65535 committed Dec 9, 2023
1 parent 5831848 commit a21e681
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class CodegenSecurity {
public String name;
Expand Down Expand Up @@ -89,6 +90,16 @@ public CodegenSecurity (CodegenSecurity original) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ private List<CodegenSecurity> filterAuthMethods(List<CodegenSecurity> authMethod

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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({
Expand Down

0 comments on commit a21e681

Please sign in to comment.