|
1 | 1 | package io.quarkiverse.openapi.generator.deployment; |
2 | 2 |
|
3 | | -import java.nio.file.Path; |
4 | | -import java.util.Arrays; |
5 | | -import java.util.List; |
6 | 3 | import java.util.Map; |
7 | | -import java.util.stream.Collectors; |
8 | 4 |
|
9 | | -import io.quarkiverse.openapi.generator.deployment.codegen.OpenApiGeneratorOutputPaths; |
10 | | -import io.quarkus.runtime.annotations.ConfigItem; |
11 | 5 | import io.quarkus.runtime.annotations.ConfigPhase; |
12 | 6 | import io.quarkus.runtime.annotations.ConfigRoot; |
13 | | -import io.smallrye.config.common.utils.StringUtil; |
| 7 | +import io.smallrye.config.ConfigMapping; |
| 8 | +import io.smallrye.config.WithName; |
14 | 9 |
|
15 | 10 | // This configuration is read in codegen phase (before build time), the annotation is for document purposes and avoiding quarkus warns |
16 | | -@ConfigRoot(name = CodegenConfig.CODEGEN_TIME_CONFIG_PREFIX, phase = ConfigPhase.BUILD_TIME) |
17 | | -public class CodegenConfig extends GlobalCodegenConfig { |
18 | | - |
19 | | - static final String CODEGEN_TIME_CONFIG_PREFIX = "openapi-generator.codegen"; |
20 | | - |
21 | | - public static final String API_PKG_SUFFIX = ".api"; |
22 | | - public static final String MODEL_PKG_SUFFIX = ".model"; |
23 | | - |
24 | | - public static final String ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER_NAME_DEFAULT = "UNEXPECTED"; |
25 | | - public static final String ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER_STRING_VALUE_DEFAULT = "unexpected"; |
26 | | - // package visibility for unit tests |
27 | | - static final String BUILD_TIME_GLOBAL_PREFIX_FORMAT = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".%s"; |
28 | | - static final String BUILD_TIME_SPEC_PREFIX_FORMAT = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".spec.%s"; |
29 | | - |
30 | | - public static final List<String> SUPPORTED_CONFIGURATIONS = Arrays.stream(ConfigName.values()).map(cn -> cn.name) |
31 | | - .collect(Collectors.toList()); |
32 | | - |
33 | | - public enum ConfigName { |
34 | | - //global configs |
35 | | - VERBOSE("verbose"), |
36 | | - INPUT_BASE_DIR("input-base-dir"), |
37 | | - INCLUDE("include"), |
38 | | - EXCLUDE("exclude"), |
39 | | - VALIDATE_SPEC("validateSpec"), |
40 | | - DEFAULT_SECURITY_SCHEME("default-security-scheme"), |
41 | | - |
42 | | - //spec configs only |
43 | | - BASE_PACKAGE("base-package"), |
44 | | - API_NAME_SUFFIX("api-name-suffix"), |
45 | | - MODEL_NAME_SUFFIX("model-name-suffix"), |
46 | | - MODEL_NAME_PREFIX("model-name-prefix"), |
47 | | - |
48 | | - //global & spec configs |
49 | | - SKIP_FORM_MODEL("skip-form-model"), |
50 | | - MUTINY("mutiny"), |
51 | | - MUTINY_RETURN_RESPONSE("mutiny.return-response"), |
52 | | - MUTINY_OPERATION_IDS("mutiny.operation-ids"), |
53 | | - ADDITIONAL_MODEL_TYPE_ANNOTATIONS("additional-model-type-annotations"), |
54 | | - ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER("additional-enum-type-unexpected-member"), |
55 | | - ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER_NAME("additional-enum-type-unexpected-member-name"), |
56 | | - ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER_STRING_VALUE("additional-enum-type-unexpected-member-string-value"), |
57 | | - ADDITIONAL_API_TYPE_ANNOTATIONS("additional-api-type-annotations"), |
58 | | - TYPE_MAPPINGS("type-mappings"), |
59 | | - IMPORT_MAPPINGS("import-mappings"), |
60 | | - NORMALIZER("open-api-normalizer"), |
61 | | - RETURN_RESPONSE("return-response"), |
62 | | - ENABLE_SECURITY_GENERATION("enable-security-generation"), |
63 | | - CONFIG_KEY("config-key"), |
64 | | - GENERATE_PART_FILENAME("generate-part-filename"), |
65 | | - PART_FILENAME_VALUE("part-filename-value"), |
66 | | - USE_FIELD_NAME_IN_PART_FILENAME("use-field-name-in-part-filename"), |
67 | | - ADDITIONAL_PROPERTIES_AS_ATTRIBUTE("additional-properties-as-attribute"), |
68 | | - ADDITIONAL_REQUEST_ARGS("additional-request-args"), |
69 | | - BEAN_VALIDATION("use-bean-validation"); |
70 | | - |
71 | | - private final String name; |
72 | | - |
73 | | - ConfigName(String name) { |
74 | | - this.name = name; |
75 | | - } |
76 | | - |
77 | | - } |
78 | | - |
| 11 | +@ConfigMapping(prefix = "openapi-generator.codegen") |
| 12 | +@ConfigRoot(phase = ConfigPhase.BUILD_TIME) |
| 13 | +public interface CodegenConfig { |
79 | 14 | /** |
80 | | - * OpenAPI Spec details for codegen configuration. |
| 15 | + * Common item test. |
81 | 16 | */ |
82 | | - @ConfigItem(name = "spec") |
83 | | - public Map<String, SpecItemConfig> specItem; |
84 | | - |
85 | | - public static String resolveApiPackage(final String basePackage) { |
86 | | - return String.format("%s%s", basePackage, API_PKG_SUFFIX); |
87 | | - } |
88 | | - |
89 | | - public static String resolveModelPackage(final String basePackage) { |
90 | | - return String.format("%s%s", basePackage, MODEL_PKG_SUFFIX); |
91 | | - } |
92 | | - |
93 | | - /** |
94 | | - * Return global config name, openapi-generator.codegen.config-name |
95 | | - */ |
96 | | - public static String getGlobalConfigName(ConfigName configName) { |
97 | | - return String.format(BUILD_TIME_GLOBAL_PREFIX_FORMAT, configName.name); |
98 | | - } |
99 | | - |
100 | | - /** |
101 | | - * Return spec config name openapi-generator.codegen.spec.%s.config-name |
102 | | - */ |
103 | | - public static String getSpecConfigName(ConfigName configName, final Path openApiFilePath) { |
104 | | - return String.format("%s.%s", getBuildTimeSpecPropertyPrefix(openApiFilePath), configName.name); |
105 | | - } |
106 | | - |
107 | | - /** |
108 | | - * Return spec config name by config-key (<b>openapi-generator.codegen.spec.%s.config-key</b>) property. |
109 | | - * For example, given a configuration <code>quarkus.openapi.generator.codegen.spec.spec_yaml.config-key=petstore</code>, the |
110 | | - * returned value is |
111 | | - * <code>openapi.generator.codegen.spec.petstore.mutiny</code>. |
112 | | - */ |
113 | | - public static String getSpecConfigNameByConfigKey(final String configKey, final ConfigName configName) { |
114 | | - String buildTimeSpecPropertyPrefix = String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, configKey); |
115 | | - return String.format("%s.%s", buildTimeSpecPropertyPrefix, configName.name); |
116 | | - } |
| 17 | + @WithName("common-item-config") |
| 18 | + CommonItemConfig commonItemConfig(); |
117 | 19 |
|
118 | 20 | /** |
119 | | - * Gets the config prefix for a given OpenAPI file in the path. |
120 | | - * For example, given a path like /home/luke/projects/petstore.json, the returned value is |
121 | | - * `quarkus.openapi-generator."petstore_json"`. |
122 | | - * Every the periods (.) in the file name will be replaced by underscore (_). |
| 21 | + * OpenAPI Spec details for codegen configuration. |
123 | 22 | */ |
124 | | - public static String getBuildTimeSpecPropertyPrefix(final Path openApiFilePath) { |
125 | | - return String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, getSanitizedFileName(openApiFilePath)); |
126 | | - } |
127 | | - |
128 | | - public static String getSanitizedFileName(final Path openApiFilePath) { |
129 | | - return StringUtil |
130 | | - .replaceNonAlphanumericByUnderscores(OpenApiGeneratorOutputPaths.getRelativePath(openApiFilePath).toString()); |
131 | | - } |
| 23 | + @WithName("spec") |
| 24 | + Map<String, SpecItemConfig> specItem(); |
| 25 | + // |
132 | 26 | } |
0 commit comments