Skip to content
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

UpperSnakeCaseStrategy is not working with spring boot and ParameterObject #2643

Closed
AlehGalo opened this issue Jul 9, 2024 · 3 comments
Closed

Comments

@AlehGalo
Copy link

AlehGalo commented Jul 9, 2024

UpperSnakeCaseStrategy + ParameterObject + Spring Boot is not working properly.

Hi there.

I'm using Request DTO object.

@JsonNaming(PropertyNamingStrategies.UpperSnakeCaseStrategy.class)
@Data
public class RequestDto {
    private String personalNumber;
...
}

With Rest Controller

@GetMapping
    public Response getData(@ParameterObject @NonNull RequestDto requestDto) {
        return service.getdata(requestDto);
    }

There was a bug fixed #2484 Seems like case was missed and stayed untouched.
It's not a frequent case but our company needs it.

Versions are as follows

<springdoc-openapi.version>2.6.0</springdoc-openapi.version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

Thanks in advance.

@buzzerrookie
Copy link
Contributor

Maybe a DelegatingMethodParameterCustomizer can help.

import java.lang.reflect.Field;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

public class MyMethodParameterCustomizer implements DelegatingMethodParameterCustomizer {

    @Override
    public void customize(MethodParameter originalParameter, MethodParameter methodParameter) {
        if (AnnotatedElementUtils.isAnnotated(methodParameter.getContainingClass(), JsonNaming.class)) {
            JsonNaming jsonNaming = methodParameter.getContainingClass().getAnnotation(JsonNaming.class);
            if (jsonNaming.value().equals(PropertyNamingStrategies.UpperSnakeCaseStrategy.class)) {
                try {
                    Field parameterNameField = FieldUtils.getDeclaredField(methodParameter.getClass(), "parameterName",
                            true);
                    parameterNameField.set(methodParameter,
                            PropertyNamingStrategies.UpperSnakeCaseStrategy.INSTANCE.translate(
                                    methodParameter.getParameterName()));
                } catch (IllegalAccessException e) {
                    // log errors
                }
            }
        }
    }
}

Then inject it as a bean:

@Bean
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer() {
    return new MyMethodParameterCustomizer();
}

This will translate personNumber to PERSON_NUMBER in Swagger.
snake

@AlehGalo
Copy link
Author

Hi @buzzerrookie.

Thank you for the code sample. It seems to be working.

But in general it looks like an issue of springdoc and workaround exists.

@AlehGalo
Copy link
Author

AlehGalo commented Aug 22, 2024

Checked the code. It brakes springdoc swagger.

Requests parameters sent are not correctly mapped back.

Sending request from swagger results in Bad Request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants