Fix NPE in codegen security (#17378)

* fix npe in codegen security

* add new test file
This commit is contained in:
William Cheng
2023-12-12 22:33:20 +08:00
committed by GitHub
parent 2a27fd51a7
commit 64c85a8fee
3 changed files with 57 additions and 1 deletions

View File

@@ -93,7 +93,7 @@ public class CodegenSecurity {
// 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)) {
if (!(Boolean.TRUE.equals(isOAuth) || Boolean.TRUE.equals(isOpenId))) {
filteredSecurity.scopes = filterScopes.stream()
.map(s -> new HashMap<String, Object>(Map.of("scope", s)))
.collect(Collectors.toList());

View File

@@ -2571,6 +2571,19 @@ public class DefaultCodegenTest {
assertEquals(securities.get(1).name, "api_key");
}
@Test
public void testOpenIdConnectSecuritySchemes() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_17376.json");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
final Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
final List<CodegenSecurity> securities = codegen.fromSecurity(securitySchemes);
assertEquals(securities.size(), 1);
assertEquals(securities.get(0).name, "Our Identity service");
}
@Test
public void testItemsPresent() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7613.yaml");

View File

@@ -0,0 +1,43 @@
{
"openapi": "3.0.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"paths": {
"/users": {
"get": {
"summary": "Get all users",
"responses": {
"200": {
"description": "Successful response"
}
},
"operationId": "V1GetAllUsers",
"description": "Retrieve a users.",
"security": [
{
"Our Identity service": []
}
],
"tags": [
"Users"
]
}
}
},
"components": {
"securitySchemes": {
"Our Identity service": {
"type": "openIdConnect",
"description": "To authenticate your calls to the API...",
"openIdConnectUrl": "https://id-something.somewhere.com/.well-known/openid-configuration"
}
}
},
"security": [
{
"Our Identity service": []
}
]
}