Repository: jhipster/jhipster-lite
Pull request #9963 (2024-05-31 16:54:30)

Springdoc: fix realm name with OAuth2
Following https://github.com/jhipster/jhipster-lite/pull/9366


Patch:
diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/apidocumentation/springdocoauth/domain/SpringDocOauth2ModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/apidocumentation/springdocoauth/domain/SpringDocOauth2ModuleFactory.java
index 58f8e15bb..1ecd69dc1 100644
--- a/src/main/java/tech/jhipster/lite/generator/server/springboot/apidocumentation/springdocoauth/domain/SpringDocOauth2ModuleFactory.java
+++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/apidocumentation/springdocoauth/domain/SpringDocOauth2ModuleFactory.java
@@ -2,6 +2,7 @@ package tech.jhipster.lite.generator.server.springboot.apidocumentation.springdo
 
 import static tech.jhipster.lite.module.domain.JHipsterModule.*;
 
+import java.util.regex.Pattern;
 import tech.jhipster.lite.module.domain.JHipsterModule;
 import tech.jhipster.lite.module.domain.file.JHipsterSource;
 import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
@@ -9,11 +10,18 @@ import tech.jhipster.lite.shared.error.domain.Assert;
 
 public class SpringDocOauth2ModuleFactory {
 
+  public static final String REALM_NAME = "keycloakRealmName";
+  public static final String DEFAULT_REALM_NAME = "jhipster";
+  private static final Pattern NAME_FORMAT = Pattern.compile("^[a-z0-9-]+$");
+
   private static final JHipsterSource SOURCE = from("server/springboot/apidocumentation/springdocoauth");
 
   public JHipsterModule buildModule(JHipsterModuleProperties properties) {
     Assert.notNull("properties", properties);
 
+    String realmName = properties.getOrDefaultString(REALM_NAME, DEFAULT_REALM_NAME);
+    Assert.field(REALM_NAME, realmName).notNull().matchesPattern(NAME_FORMAT).maxLength(30);
+
     //@formatter:off
     return moduleBuilder(properties)
       .files()
@@ -26,18 +34,18 @@ public class SpringDocOauth2ModuleFactory {
         .and()
       .springMainProperties()
         .set(propertyKey("springdoc.swagger-ui.oauth.client-id"), propertyValue("web_app"))
-        .set(propertyKey("springdoc.swagger-ui.oauth.realm"), propertyValue("jhipster"))
+        .set(propertyKey("springdoc.swagger-ui.oauth.realm"), propertyValue(realmName))
         .set(
           propertyKey("springdoc.oauth2.authorization-url"),
-          propertyValue("http://localhost:9080/realms/jhipster/protocol/openid-connect/auth")
+          propertyValue("http://localhost:9080/realms/" + realmName + "/protocol/openid-connect/auth")
         )
         .and()
       .springTestProperties()
         .set(propertyKey("springdoc.swagger-ui.oauth.client-id"), propertyValue("web_app"))
-        .set(propertyKey("springdoc.swagger-ui.oauth.realm"), propertyValue("jhipster"))
+        .set(propertyKey("springdoc.swagger-ui.oauth.realm"), propertyValue(realmName))
         .set(
           propertyKey("springdoc.oauth2.authorization-url"),
-          propertyValue("http://localhost:9080/realms/jhipster/protocol/openid-connect/auth")
+          propertyValue("http://localhost:9080/realms/" + realmName + "/protocol/openid-connect/auth")
         )
         .and()
       .build();


Test patch:
diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/apidocumentation/springdocoauth/domain/SpringDocOauth2ModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/apidocumentation/springdocoauth/domain/SpringDocOauth2ModuleFactoryTest.java
index d41238951..01c715b10 100644
--- a/src/test/java/tech/jhipster/lite/generator/server/springboot/apidocumentation/springdocoauth/domain/SpringDocOauth2ModuleFactoryTest.java
+++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/apidocumentation/springdocoauth/domain/SpringDocOauth2ModuleFactoryTest.java
@@ -18,6 +18,7 @@ class SpringDocOauth2ModuleFactoryTest {
   void shouldBuildModule() {
     JHipsterModuleProperties properties = JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest())
       .basePackage("com.jhipster.test")
+      .put("keycloakRealmName", "beer")
       .build();
 
     JHipsterModule module = factory.buildModule(properties);
@@ -32,9 +33,9 @@ class SpringDocOauth2ModuleFactoryTest {
           swagger-ui:
             oauth:
               client-id: web_app
-              realm: jhipster
+              realm: beer
           oauth2:
-            authorization-url: http://localhost:9080/realms/jhipster/protocol/openid-connect/auth
+            authorization-url: http://localhost:9080/realms/beer/protocol/openid-connect/auth
         """
       )
       .and()
@@ -45,9 +46,9 @@ class SpringDocOauth2ModuleFactoryTest {
           swagger-ui:
             oauth:
               client-id: web_app
-              realm: jhipster
+              realm: beer
           oauth2:
-            authorization-url: http://localhost:9080/realms/jhipster/protocol/openid-connect/auth
+            authorization-url: http://localhost:9080/realms/beer/protocol/openid-connect/auth
         """
       );
     //@formatter:on