Spring QuerydslPredicate's naming convention to snake case

629 views Asked by At

I'm using Predicate from QueryDsl. Backends internally uses camelCase, but promised to use snake_case when communicating with clients.

I want to use snake_case as query parameters like

http://localhost:8080/inputmethod?protocol_type=SDK

if I pass protocol_type as snake_case, Query Dsl Predicate doesn't parse it. It parse only fields of the type(target entity class) as camelCase. so protocol_type is skipped by getPredicate() of QuerydslPredicateBuilder.

    /**
     * Creates a Querydsl {@link Predicate} for the given values, {@link QuerydslBindings} on the given
     * {@link TypeInformation}.
     *
     * @param type the type to create a predicate for.
     * @param values the values to bind.
     * @param bindings the {@link QuerydslBindings} for the predicate.
     * @return
     */
    @Nullable
    public Predicate getPredicate(TypeInformation<?> type, MultiValueMap<String, String> values,
            QuerydslBindings bindings) {
    ...
            if (!bindings.isPathAvailable(path, type)) { // <-- here!
            // path : "protocol_type"
            // type : my.domain.entity.InputMethod
                continue;
            }
   ...
}
   

but it works in cameCase.

http://localhost:8080/inputmethod?protocolType=SDK

How to set the naming convention of Predicate to snake_case?


Controller

    @GetMapping
    public List<InputMethodDto.Response> getInputMethodTypeList(
            @QuerydslPredicate(root = InputMethod.class) Predicate predicate) {
        return service.getInputMethodList(predicate);
    }

Entity


@Getter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class InputMethod {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Enumerated(EnumType.STRING)
    @Column
    private RecordType recordType;

    @Enumerated(EnumType.STRING)
    @Column
    private ProtocolType protocolType;

    @Column
    private String name;

Config


@EnableWebMvc
@EnableSwagger2
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
    ...
}


Jackson naming strategy Same issue with me.

https://stackoverflow.com/questions/53273966/spring-jpa-querydslpredicate-snake-case

I also set spring.jackson.property-naming-strategy=SNAKE_CASE

1

There are 1 answers

1
Michal Drozd On

Unfortunately I am afraid you can't use snake_case here, simply because _ is considered as property splitter in spring data mapping. So mapper will convert it to protocol and type