Skip to content

Commit 3885bff

Browse files
committed
extract common function to util
1 parent 7981fba commit 3885bff

4 files changed

Lines changed: 21 additions & 32 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypeOpenApiCustomizerForCodeGenerator.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import lombok.extern.slf4j.Slf4j;
99
import org.springdoc.core.customizers.OpenApiCustomizer;
1010

11-
import java.util.Optional;
11+
import static it.aboutbits.springboot.toolbox.swagger.customization.custom_type.SchemaUtil.getClassFromSchemaReference;
1212

1313
@Slf4j
1414
public class CustomTypeOpenApiCustomizerForCodeGenerator implements OpenApiCustomizer {
@@ -17,14 +17,6 @@ public void customise(OpenAPI openApi) {
1717
openApi.getComponents().getSchemas().forEach(this::updateSchema);
1818
}
1919

20-
public Optional<Class<?>> getClassFromSchemaReference(String schemaRef) {
21-
try {
22-
var className = schemaRef.replace("#/components/schemas/", "");
23-
return Optional.of(Class.forName(className));
24-
} catch (ClassNotFoundException e) {
25-
return Optional.empty();
26-
}
27-
}
2820

2921
private void updateSchema(String fqn, Schema<?> schema) {
3022
var type = getClassFromSchemaReference(fqn);

src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypeParameterCustomizer.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,12 @@
1515

1616
import java.math.BigDecimal;
1717
import java.math.BigInteger;
18-
import java.util.Optional;
1918
import java.util.UUID;
2019

20+
import static it.aboutbits.springboot.toolbox.swagger.customization.custom_type.SchemaUtil.getClassFromSchemaReference;
21+
2122
@Slf4j
2223
public class CustomTypeParameterCustomizer implements ParameterCustomizer {
23-
24-
public Optional<Class<?>> getClassFromSchemaReference(String schemaRef) {
25-
try {
26-
var className = schemaRef.replace("#/components/schemas/", "");
27-
return Optional.of(Class.forName(className));
28-
} catch (ClassNotFoundException e) {
29-
// Log the error if needed
30-
return Optional.empty();
31-
}
32-
}
33-
34-
3524
@SuppressWarnings("checkstyle:MethodLength")
3625
@Override
3726
@SneakyThrows(NoSuchMethodException.class)

src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypeParameterCustomizerForCodeGenerator.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.springdoc.core.customizers.ParameterCustomizer;
99
import org.springframework.core.MethodParameter;
1010

11-
import java.util.Optional;
11+
import static it.aboutbits.springboot.toolbox.swagger.customization.custom_type.SchemaUtil.getClassFromSchemaReference;
1212

1313
@Slf4j
1414
public class CustomTypeParameterCustomizerForCodeGenerator implements ParameterCustomizer {
@@ -58,13 +58,4 @@ public Parameter customize(Parameter parameter, MethodParameter methodParameter)
5858

5959
return parameter;
6060
}
61-
62-
public Optional<Class<?>> getClassFromSchemaReference(String schemaRef) {
63-
try {
64-
var className = schemaRef.replace("#/components/schemas/", "");
65-
return Optional.of(Class.forName(className));
66-
} catch (ClassNotFoundException e) {
67-
return Optional.empty();
68-
}
69-
}
7061
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package it.aboutbits.springboot.toolbox.swagger.customization.custom_type;
2+
3+
import java.util.Optional;
4+
5+
final class SchemaUtil {
6+
private SchemaUtil() {
7+
}
8+
9+
static Optional<Class<?>> getClassFromSchemaReference(String schemaRef) {
10+
try {
11+
var className = schemaRef.replace("#/components/schemas/", "");
12+
return Optional.of(Class.forName(className));
13+
} catch (ClassNotFoundException e) {
14+
return Optional.empty();
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)