Skip to content

[REQ][JAVA][WebClient] Add support for setting max-in-memory-size for WebClient #5577

Description

@npeder

Is your feature request related to a problem? Please describe.

When generating a client with the Java WebClient library it is not possible to configure the max-in-memory-size of the WebClient. The generator does not honor the Spring configuration property called spring.codec.max-in-memory-size.

Describe the solution you'd like

A configuration option should be added that will set the max-in-memory-size of the WebClient.
The ApiClient.mustache file should be changed so the buildWebClient method looks something like this:

    /**
    * Build the WebClient used to make HTTP requests.
    * @return WebClient
    */
    public static WebClient buildWebClient(ObjectMapper mapper) {
        ExchangeStrategies strategies = ExchangeStrategies
            .builder()
            .codecs(clientDefaultCodecsConfigurer -> {
                clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON));
                clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.APPLICATION_JSON));
                clientDefaultCodecsConfigurer.defaultCodecs().maxInMemorySize(configuredMaxInMemorySize);
            }).build();
        WebClient.Builder webClient = WebClient.builder().exchangeStrategies(strategies);
        return webClient.build();
    }

Describe alternatives you've considered

An alternative might be some way to set the max-in-memory-size on the ApiClient class after it's created. E.g. with a method like this:

    public void setMaxInMemorySize(int maxInMemorySize) {
        webClient = webClient.mutate().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(maxInMemorySize)).build();
    }

This would require making the webClient field non-final in the ApiClient class.

Another alternative would be to inject the default WebClient.Builder instantiated and configured by spring-boot, that would honor setting the spring.codec.max-in-memory-size property.
See https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/html/boot-features-webclient.html

Additional context

I am using the gradle plugin with these settings:

plugins {
    id 'org.openapi.generator' version '4.2.3'
}

openApiGenerate {
    generatorName = "java"
    library = "webclient"
    inputSpec = "$projectDir/src/main/resources/api-spec/spec.json"
    outputDir = "$buildDir/generated"
    apiPackage = "com.test.rest.api"
    invokerPackage = "com.test.rest.invoker"
    modelPackage = "com.test.rest.model"
    modelNameSuffix = "Model"
    configOptions = [
            java11 : "true",
            dateLibrary: "java11",
            interfaceOnly: "true",
            useSwaggerAnnotations: "true"
    ]

    generateModelTests = false
    generateApiTests = false
}

See also #874

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions