Skip to content

Commit d3e697f

Browse files
claudeng-galien
authored andcommitted
refactor: Move @ClientRegistrationId annotation to class level
Move the @ClientRegistrationId annotation from individual methods to the interface class level, following Spring Security's recommended practice. Changes: - Update api.mustache to place annotation on interface declaration - Modify SpringCodegen to set clientRegistrationId on operations map - Update sample code to show class-level annotation - Update README with improved example and explanation This approach is cleaner and avoids repeating the annotation on every method, as recommended in Spring Security documentation.
1 parent 35387cb commit d3e697f

4 files changed

Lines changed: 23 additions & 20 deletions

File tree

  • modules/openapi-generator/src/main
  • samples/client/petstore/spring-http-interface-oauth

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -800,11 +800,6 @@ public void setIsVoid(boolean isVoid) {
800800

801801
prepareVersioningParameters(ops);
802802
handleImplicitHeaders(operation);
803-
804-
// Add clientRegistrationId for spring-http-interface with OAuth
805-
if (SPRING_HTTP_INTERFACE.equals(library) && clientRegistrationId != null && !clientRegistrationId.isEmpty()) {
806-
operation.vendorExtensions.put("clientRegistrationId", clientRegistrationId);
807-
}
808803
}
809804
// The tag for the controller is the first tag of the first operation
810805
final CodegenOperation firstOperation = ops.get(0);
@@ -813,6 +808,11 @@ public void setIsVoid(boolean isVoid) {
813808
// But use a sensible tag name if there is none
814809
objs.put("tagName", "default".equals(firstTagName) ? firstOperation.baseName : firstTagName);
815810
objs.put("tagDescription", escapeText(firstTag.getDescription()));
811+
812+
// Add clientRegistrationId for spring-http-interface with OAuth
813+
if (SPRING_HTTP_INTERFACE.equals(library) && clientRegistrationId != null && !clientRegistrationId.isEmpty()) {
814+
operations.put("clientRegistrationId", clientRegistrationId);
815+
}
816816
}
817817

818818
removeImport(objs, "java.util.List");

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-http-interface/api.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import org.springframework.http.ResponseEntity;
1515
{{/useResponseEntity}}
1616
import org.springframework.web.bind.annotation.*;
1717
import org.springframework.web.service.annotation.*;
18-
{{#operations}}{{#operation}}{{#vendorExtensions.clientRegistrationId}}
18+
{{#operations}}{{#clientRegistrationId}}
1919
import org.springframework.security.oauth2.client.annotation.ClientRegistrationId;
20-
{{/vendorExtensions.clientRegistrationId}}{{/operation}}{{/operations}}
20+
{{/clientRegistrationId}}{{/operations}}
2121
import org.springframework.web.multipart.MultipartFile;
2222
{{#reactive}}
2323

@@ -35,6 +35,9 @@ import {{javaxPackage}}.annotation.Generated;
3535
{{>generatedAnnotation}}
3636

3737
{{#operations}}
38+
{{#clientRegistrationId}}
39+
@ClientRegistrationId("{{clientRegistrationId}}")
40+
{{/clientRegistrationId}}
3841
public interface {{classname}} {
3942
{{#operation}}
4043

@@ -60,9 +63,6 @@ public interface {{classname}} {
6063
{{#isDeprecated}}
6164
@Deprecated
6265
{{/isDeprecated}}
63-
{{#vendorExtensions.clientRegistrationId}}
64-
@ClientRegistrationId("{{vendorExtensions.clientRegistrationId}}")
65-
{{/vendorExtensions.clientRegistrationId}}
6666
{{^useResponseEntity}}
6767
@ResponseStatus({{#springHttpStatus}}{{#responses.0}}{{{code}}}{{/responses.0}}{{/springHttpStatus}})
6868
{{/useResponseEntity}}

samples/client/petstore/spring-http-interface-oauth/README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,23 @@ openapi-generator-cli generate \
3030

3131
## Generated Code
3232

33-
The generated interface methods will include the `@ClientRegistrationId` annotation:
33+
The generated interface will include the `@ClientRegistrationId` annotation at the class level:
3434

3535
```java
3636
@ClientRegistrationId("petstore-oauth")
37-
@HttpExchange(
38-
method = "GET",
39-
value = "/pet/{petId}",
40-
accept = { "application/json" }
41-
)
42-
ResponseEntity<PetDto> getPetById(@PathVariable("petId") Long petId);
37+
public interface PetApi {
38+
39+
@HttpExchange(
40+
method = "GET",
41+
value = "/pet/{petId}",
42+
accept = { "application/json" }
43+
)
44+
ResponseEntity<PetDto> getPetById(@PathVariable("petId") Long petId);
45+
}
4346
```
4447

48+
This follows the Spring Security recommendation to add `@ClientRegistrationId` at the type level to avoid repeating the declaration on every method.
49+
4550
## Spring Security Integration
4651

4752
This annotation is part of Spring Security's OAuth2 integration for HTTP Service Clients. It automatically associates OAuth2 tokens with HTTP requests.

samples/client/petstore/spring-http-interface-oauth/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
2222

23+
@ClientRegistrationId("petstore-oauth")
2324
public interface PetApi {
2425

2526
/**
@@ -28,7 +29,6 @@ public interface PetApi {
2829
* @param petDto Pet object that needs to be added to the store (required)
2930
* @return successful operation (status code 200)
3031
*/
31-
@ClientRegistrationId("petstore-oauth")
3232
@HttpExchange(
3333
method = "POST",
3434
value = "/pet",
@@ -45,7 +45,6 @@ ResponseEntity<Void> addPet(
4545
* @param petId ID of pet to return (required)
4646
* @return successful operation (status code 200)
4747
*/
48-
@ClientRegistrationId("petstore-oauth")
4948
@HttpExchange(
5049
method = "GET",
5150
value = "/pet/{petId}",
@@ -61,7 +60,6 @@ ResponseEntity<PetDto> getPetById(
6160
* @param petDto Pet object that needs to be updated in the store (required)
6261
* @return successful operation (status code 200)
6362
*/
64-
@ClientRegistrationId("petstore-oauth")
6563
@HttpExchange(
6664
method = "PUT",
6765
value = "/pet",

0 commit comments

Comments
 (0)