New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding typemapping option to the generator #143
Adding typemapping option to the generator #143
Conversation
Hi @ricardozanini , to allow users to change multiple types during generation I want to use a Map to load all the custom mappings provided in the settings. So for example with the following quarkus.openapi-generator.codegen.spec.multipart_requests_yml.type-mapping.File=java.io.InputStream
quarkus.openapi-generator.codegen.spec.multipart_requests_yml.type-mapping.DateTime=java.time.LocalDateTime I would like to load this as a map like so
Do you know a way to load items from the config as a map? I am not sure if microprofile-config supports this, maybe Quarkus has some help method for this? |
I am writing another Quarkus plugin and I also wanted to do exactly what you want to do with a MAP and I could not figure it out from their docs or find any examples. |
It's actually possible to do it like this: https://smallrye.io/smallrye-config/2.12.0/config/map-support/ ! Map<String, String> map = ((SmallRyeConfig) config).getOptionalValues("my.config.map", String.class, String.class) Also, I found out that you don't have to follow that convention for the value of the map as "k1=v1;k2=v2", it also works with just specifying the key-values as children properties (The method is marked as Experimental though...) |
@Orbifoldt we are already supporting mapping properties, no? quarkus.openapi-generator.codegen.spec.multipart_requests_yml.type-mapping.File=java.io.InputStream
quarkus.openapi-generator.codegen.spec.multipart_requests_yml.type-mapping.DateTime=java.time.LocalDateTime The Maybe this can work? @ConfigGroup
public class SpecItemConfig {
/**
* Base package for where the generated code for the given OpenAPI specification will be added.
*/
@ConfigItem(name = "base-package")
public String basePackage;
/**
* Whether to skip the generation of models for form parameters
*/
@ConfigItem(name = "skip-form-model")
public Optional<Boolean> skipFormModel;
@ConfigItem(name = "type-mapping")
public Map<TypeMapping, Class> typeMappingItem;
} And then: public enum TypeMapping {
File, DateTime;
} Sorry, I wrote from the top of my head. |
How would I use that config interface to load those entries when we setup the generator? Currently, it seems we only use those ConfigItems as declaration of the config items, but it has no effect on the actual code generation. When we want to use the corresponding values we do it like this: config.getOptionalValue(getCustomRegisterProvidersFormat(openApiFilePath), String.class) (taken from Perhaps we should somehow load the full configuration (in that abstract code generator) as an instance of the configuration interface... |
@Orbifoldt indeed we are using the config on Codegen phase, that's why we don't have access to the ConfigSource, it's too early in the lifecycle. But I believe that the |
@ricardozanini ah right, that makes sense. I just tried the following (in protected void generate(final Config config, final Path openApiFilePath, final Path outDir) {
SmallRyeConfig smallRyeConfig = config.unwrap(SmallRyeConfig.class);
CodegenConfig codeGenConfig = smallRyeConfig.getConfigMapping(CodegenConfig.class);
// ....
} But this doesn't work because the In fact, I think this will never work: smallrye expects an interface whereas quarkus expects a class. As I now understand from your comment is that the Quarkus extension config can be injected only at runtime. So is my understanding correct that we can only access the properties via the programmtic (and in my opinion ugly) |
Excellent PR and documentation updates! |
@Orbifoldt replies inline
Depends on the config. There is Build or Runtime access (or both). For us, they are there just for a doc reference and have the config structured. Runtime configurations are the ones for authentication for obvious reasons. All the configs for codegen are only valid before build time.
Yes, it's ugly but this is the only way Quarkus let us access the configuration. I believe it can be quite a hurdle for them to support the config API in Codegen interface. :( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good!
The configuration might go out of control pretty soon; I need to review this tech debt.
...in/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java
Show resolved
Hide resolved
integration-tests/example-project/src/main/openapi/type-mappings-testing.yml
Show resolved
Hide resolved
@ricardozanini Thanks for the comments and answers to my questions! It's interesting to learn more and more about how Quarkus and its extensions work 😄 |
Much appreciated for this fix and showing me how to do Map type configurations as I need this for another Extension I am writing! |
This PR will enable users to configure their own type mappings via the properties. For example, this allows users to use
InputStream
for their multipart file uploads (instead of the defaultFile
) as request in #118 .NB: still a work in progress