OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java][RestTemplate] No OAuth Authentication in ApiClient#authentications Map
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
ApiClient.java is missing the "OAuth2" Authentication which results in RestClientException because when invoking the generated endpoint-method it tries to find the "OAuth2" Authentication in its map but there is none:
protected void init() {
// Use RFC3339 format for date and datetime.
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new RFC3339DateFormat();
// Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// Set default User-Agent.
setUserAgent("Java-SDK");
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
authentications.put("XToken", new HttpBearerAuth("bearer"));
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}
openapi-generator version
7.11.0
7.2.0
OpenAPI declaration file content or url
security:
- XToken: [ ]
- OAuth2: [ ]
paths:
/v1/management/logs:
get:
description: 'Get all Logs'
operationId: 'get-all-logs'
tags:
- 'management'
security:
- OAuth2: [ ]
responses:
200:
description: 'OK'
content:
application/text:
schema:
type: string
schemas:
securitySchemes:
XToken:
description: 'Must contain an encrypted X-Token.'
type: http
scheme: bearer
OAuth2:
description: 'Must contain a signed Access Token.'
type: oauth2
flows:
implicit:
authorizationUrl: 'http://localhost:9090'
scopes:
openid: 'default'
Fix I had to implement:
private static final String OPENAPI_SECURITY_OAUTH2_NAME = "OAuth2";
@SuppressWarnings("unchecked")
protected void addOAuth2AccessToken(final ApiClient apiClient, final String accessToken) {
try {
final Field authenticationsField = apiClient.getClass().getDeclaredField("authentications");
authenticationsField.setAccessible(true);
final Map<String, Authentication> originalAuthentications = (Map<String, Authentication>) authenticationsField.get(apiClient);
if (!originalAuthentications.containsKey(OPENAPI_SECURITY_OAUTH2_NAME)) {
final Map<String, Authentication> fixedAuthentications = new HashMap<>(originalAuthentications);
final OAuth oAuth = new OAuth();
oAuth.setAccessToken(accessToken);
fixedAuthentications.put(OPENAPI_SECURITY_OAUTH2_NAME, oAuth);
authenticationsField.set(apiClient, Collections.unmodifiableMap(fixedAuthentications));
}
authenticationsField.setAccessible(false);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated Java RestTemplate ApiClient.java init() method and the supplied OpenAPI security declaration, comparing the OAuth2 and XToken schemes. Trace how generated endpoint methods look up authentications; done means an OAuth2-secured endpoint finds its authentication without RestClientException while existing XToken behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, authentication
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100