Skip to content

Commit

Permalink
Convert client and server modules to use @ConfigMapping (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev authored Jan 22, 2025
1 parent b9d49cf commit a940d82
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 171 deletions.
3 changes: 0 additions & 3 deletions client/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@
import java.util.stream.Collectors;

import io.quarkiverse.openapi.generator.deployment.codegen.OpenApiGeneratorOutputPaths;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithName;
import io.smallrye.config.common.utils.StringUtil;

// This configuration is read in codegen phase (before build time), the annotation is for document purposes and avoiding quarkus warns
@ConfigRoot(name = CodegenConfig.CODEGEN_TIME_CONFIG_PREFIX, phase = ConfigPhase.BUILD_TIME)
public class CodegenConfig extends GlobalCodegenConfig {
@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
@ConfigMapping(prefix = "quarkus." + CodegenConfig.CODEGEN_TIME_CONFIG_PREFIX)
public interface CodegenConfig extends GlobalCodegenConfig {

static final String CODEGEN_TIME_CONFIG_PREFIX = "openapi-generator.codegen";
String CODEGEN_TIME_CONFIG_PREFIX = "openapi-generator.codegen";

public static final String API_PKG_SUFFIX = ".api";
public static final String MODEL_PKG_SUFFIX = ".model";
String API_PKG_SUFFIX = ".api";
String MODEL_PKG_SUFFIX = ".model";

public static final String ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER_NAME_DEFAULT = "UNEXPECTED";
public static final String ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER_STRING_VALUE_DEFAULT = "unexpected";
String ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER_NAME_DEFAULT = "UNEXPECTED";
String ADDITIONAL_ENUM_TYPE_UNEXPECTED_MEMBER_STRING_VALUE_DEFAULT = "unexpected";
// package visibility for unit tests
static final String BUILD_TIME_GLOBAL_PREFIX_FORMAT = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".%s";
static final String BUILD_TIME_SPEC_PREFIX_FORMAT = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".spec.%s";
String BUILD_TIME_GLOBAL_PREFIX_FORMAT = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".%s";
String BUILD_TIME_SPEC_PREFIX_FORMAT = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".spec.%s";

public static final List<String> SUPPORTED_CONFIGURATIONS = Arrays.stream(ConfigName.values()).map(cn -> cn.name)
List<String> SUPPORTED_CONFIGURATIONS = Arrays.stream(ConfigName.values()).map(cn -> cn.name)
.collect(Collectors.toList());

public enum ConfigName {
enum ConfigName {
//global configs
VERBOSE("verbose"),
INPUT_BASE_DIR("input-base-dir"),
Expand Down Expand Up @@ -88,28 +90,28 @@ public enum ConfigName {
/**
* OpenAPI Spec details for codegen configuration.
*/
@ConfigItem(name = "spec")
public Map<String, SpecItemConfig> specItem;
@WithName("spec")
Map<String, SpecItemConfig> specItem();

public static String resolveApiPackage(final String basePackage) {
static String resolveApiPackage(final String basePackage) {
return String.format("%s%s", basePackage, API_PKG_SUFFIX);
}

public static String resolveModelPackage(final String basePackage) {
static String resolveModelPackage(final String basePackage) {
return String.format("%s%s", basePackage, MODEL_PKG_SUFFIX);
}

/**
* Return global config name, openapi-generator.codegen.config-name
*/
public static String getGlobalConfigName(ConfigName configName) {
static String getGlobalConfigName(ConfigName configName) {
return String.format(BUILD_TIME_GLOBAL_PREFIX_FORMAT, configName.name);
}

/**
* Return spec config name openapi-generator.codegen.spec.%s.config-name
*/
public static String getSpecConfigName(ConfigName configName, final Path openApiFilePath) {
static String getSpecConfigName(ConfigName configName, final Path openApiFilePath) {
return String.format("%s.%s", getBuildTimeSpecPropertyPrefix(openApiFilePath), configName.name);
}

Expand All @@ -119,7 +121,7 @@ public static String getSpecConfigName(ConfigName configName, final Path openApi
* returned value is
* <code>openapi.generator.codegen.spec.petstore.mutiny</code>.
*/
public static String getSpecConfigNameByConfigKey(final String configKey, final ConfigName configName) {
static String getSpecConfigNameByConfigKey(final String configKey, final ConfigName configName) {
String buildTimeSpecPropertyPrefix = String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, configKey);
return String.format("%s.%s", buildTimeSpecPropertyPrefix, configName.name);
}
Expand All @@ -130,11 +132,11 @@ public static String getSpecConfigNameByConfigKey(final String configKey, final
* `quarkus.openapi-generator."petstore_json"`.
* Every the periods (.) in the file name will be replaced by underscore (_).
*/
public static String getBuildTimeSpecPropertyPrefix(final Path openApiFilePath) {
static String getBuildTimeSpecPropertyPrefix(final Path openApiFilePath) {
return String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, getSanitizedFileName(openApiFilePath));
}

public static String getSanitizedFileName(final Path openApiFilePath) {
static String getSanitizedFileName(final Path openApiFilePath) {
return StringUtil
.replaceNonAlphanumericByUnderscores(OpenApiGeneratorOutputPaths.getRelativePath(openApiFilePath).toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.util.Map;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.smallrye.config.WithName;

/*
* Model for the configuration of this extension.
Expand All @@ -13,92 +12,91 @@
* Not meant to be used outside this scope.
* Config items can be applied on spec and globally as well
*/
@ConfigGroup
public class CommonItemConfig {
public interface CommonItemConfig {

/**
* Whether to skip the generation of models for form parameters
*/
@ConfigItem(name = "skip-form-model")
public Optional<Boolean> skipFormModel;
@WithName("skip-form-model")
Optional<Boolean> skipFormModel();

/**
* Type Mapping is an OpenAPI Generator configuration specifying which Java types (the values) should be used for a
* given OAS datatype (the keys of this map)
*/
@ConfigItem(name = "type-mappings")
public Map<String, String> typeMappings;
@WithName("type-mappings")
Map<String, String> typeMappings();

/**
* Import Mapping is an OpenAPI Generator configuration specifying which Java types (the values) should be
* imported when a given OAS datatype (the keys of this map) is used
*/
@ConfigItem(name = "import-mappings")
public Map<String, String> importMappings;
@WithName("import-mappings")
Map<String, String> importMappings();

/**
* Schema Mapping is an OpenAPI Generator configuration specifying which Java types (the values) should be
* imported when a given schema type (the keys of this map) is used
*/
@ConfigItem(name = "schema-mappings")
public Map<String, String> schemaMappings;
@WithName("schema-mappings")
Map<String, String> schemaMappings();

/**
* The specified annotations will be added to the generated model files
*/
@ConfigItem(name = "additional-model-type-annotations")
public Optional<String> additionalModelTypeAnnotations;
@WithName("additional-model-type-annotations")
Optional<String> additionalModelTypeAnnotations();

/**
* Defines if the enums should have an `UNEXPECTED` member to convey values that cannot be parsed. Default is
* {@code false}.
*/
@ConfigItem(name = "additional-enum-type-unexpected-member")
public Optional<Boolean> additionalEnumTypeUnexpectedMemberAnnotations;
@WithName("additional-enum-type-unexpected-member")
Optional<Boolean> additionalEnumTypeUnexpectedMemberAnnotations();

/**
* The specified annotations will be added to the generated api files
*/
@ConfigItem(name = "additional-api-type-annotations")
public Optional<String> additionalApiTypeAnnotations;
@WithName("additional-api-type-annotations")
Optional<String> additionalApiTypeAnnotations();

/**
* Add custom/additional HTTP Headers or other args to every request
*/
@ConfigItem(name = "additional-request-args")
public Optional<String> additionalRequestArgs;
@WithName("additional-request-args")
Optional<String> additionalRequestArgs();

/**
* Defines if the methods should return {@link jakarta.ws.rs.core.Response} or a model. Default is {@code false}.
*/
@ConfigItem(name = "return-response")
public Optional<Boolean> returnResponse;
@WithName("return-response")
Optional<Boolean> returnResponse();

/**
* Defines if security support classes should be generated
*/
@ConfigItem(name = "enable-security-generation")
public Optional<String> enableSecurityGeneration;
@WithName("enable-security-generation")
Optional<String> enableSecurityGeneration();

/**
* Defines the normalizer options.
*/
@ConfigItem(name = "open-api-normalizer")
public Map<String, String> normalizer;
@WithName("open-api-normalizer")
Map<String, String> normalizer();

/**
* Enable SmallRye Mutiny support. If you set this to {@code true}, all return types will be wrapped in
* {@link io.smallrye.mutiny.Uni}.
*/
@ConfigItem(name = "mutiny")
public Optional<Boolean> supportMutiny;
@WithName("mutiny")
Optional<Boolean> supportMutiny();

/**
* Defines with SmallRye Mutiny enabled if methods should return {@link jakarta.ws.rs.core.Response} or a model. Default is
* {@code false}.
*/
@ConfigItem(name = "mutiny.return-response")
public Optional<Boolean> mutinyReturnResponse;
@WithName("mutiny.return-response")
Optional<Boolean> mutinyReturnResponse();

/**
* Handles the return type for each operation, depending on the configuration.
Expand All @@ -125,16 +123,16 @@ public class CommonItemConfig {
* - If the operation has a void return type, it will return {@link io.smallrye.mutiny.Uni<jakarta.ws.rs.core.Response>}.
* - Otherwise, it will return {@link io.smallrye.mutiny.Uni<returnType>}`.
*/
@ConfigItem(name = "mutiny.operation-ids")
public Optional<Map<String, String>> mutinyMultiOperationIds;
@WithName("mutiny.operation-ids")
Map<String, String> mutinyMultiOperationIds();

/**
* Defines, whether the `PartFilename` ({@link org.jboss.resteasy.reactive.PartFilename} or
* {@link org.jboss.resteasy.annotations.providers.multipart.PartFilename}) annotation should be generated for
* MultipartForm POJOs. By setting to {@code false}, the annotation will not be generated.
*/
@ConfigItem(name = "generate-part-filename")
public Optional<Boolean> generatePartFilename;
@WithName("generate-part-filename")
Optional<Boolean> generatePartFilename();

/**
* Defines the filename for a part in case the `PartFilename` annotation
Expand All @@ -143,40 +141,40 @@ public class CommonItemConfig {
* In case no value is set, the default one is `&lt;fieldName&gt;File` or `file`, depending on the
* {@link CommonItemConfig#useFieldNameInPartFilename} configuration.
*/
@ConfigItem(name = "part-filename-value")
public Optional<String> partFilenameValue;
@WithName("part-filename-value")
Optional<String> partFilenameValue();

/**
* Defines, whether the filename should also include the property name in case the `PartFilename` annotation
* ({@link org.jboss.resteasy.reactive.PartFilename} or
* {@link org.jboss.resteasy.annotations.providers.multipart.PartFilename}) is generated.
*/
@ConfigItem(name = "use-field-name-in-part-filename")
public Optional<Boolean> useFieldNameInPartFilename;
@WithName("use-field-name-in-part-filename")
Optional<Boolean> useFieldNameInPartFilename();

/**
* Enable bean validation. If you set this to {@code true}, validation annotations are added to generated sources E.g.
* {@code @Size}.
*/
@ConfigItem(name = "use-bean-validation")
public Optional<Boolean> useBeanValidation;
@WithName("use-bean-validation")
Optional<Boolean> useBeanValidation();

/**
* Enable the generation of APIs. If you set this to {@code false}, APIs will not be generated.
*/
@ConfigItem(name = "generate-apis")
public Optional<Boolean> generateApis;
@WithName("generate-apis")
Optional<Boolean> generateApis();

/**
* Enable the generation of models. If you set this to {@code false}, models will not be generated.
*/
@ConfigItem(name = "generate-models")
public Optional<Boolean> generateModels;
@WithName("generate-models")
Optional<Boolean> generateModels();

/**
* Enable the generation of equals and hashcode in models. If you set this to {@code false}, the models
* will not have equals and hashcode.
*/
@ConfigItem(name = "equals-hashcode")
public Optional<Boolean> equalsHashcode;
@WithName("equals-hashcode")
Optional<Boolean> equalsHashcode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;

/*
* Model for the configuration of this extension.
Expand All @@ -12,50 +12,51 @@
* Not meant to be used outside this scope.
* Config items can be applied only globally
*/
@ConfigGroup
public class GlobalCodegenConfig extends CommonItemConfig {
public interface GlobalCodegenConfig extends CommonItemConfig {

/**
* Whether to log the internal generator codegen process in the default output or not.
*/
@ConfigItem(name = "verbose", defaultValue = "false")
public boolean verbose;
@WithDefault("false")
@WithName("verbose")
boolean verbose();

/**
* Option to change the directory where OpenAPI files must be found.
*/
@ConfigItem(name = "input-base-dir")
public Optional<String> inputBaseDir;
@WithName("input-base-dir")
Optional<String> inputBaseDir();

/**
* Option to change the directory where template files must be found.
*/
@ConfigItem(name = "template-base-dir")
public Optional<String> templateBaseDir;
@WithName("template-base-dir")
Optional<String> templateBaseDir();

/**
* Whether or not to skip validating the input spec prior to generation. By default, invalid specifications will result in
* an error.
*/
@ConfigItem(name = "validateSpec", defaultValue = "true")
public boolean validateSpec;
@WithName("validateSpec")
@WithDefault("true")
boolean validateSpec();

/**
* Option to specify files for which generation should be executed only
*/
@ConfigItem(name = "include")
public Optional<String> include;
@WithName("include")
Optional<String> include();

/**
* Option to exclude file from generation
*/
@ConfigItem(name = "exclude")
public Optional<String> exclude;
@WithName("exclude")
Optional<String> exclude();

/**
* Create security for the referenced security scheme
*/
@ConfigItem(name = "default-security-scheme")
public Optional<String> defaultSecuritySchema;
@WithName("default-security-scheme")
Optional<String> defaultSecuritySchema();

}
Loading

0 comments on commit a940d82

Please sign in to comment.