CORS config interferes with JSON validation in spring boot webflux

Viewed 19

We want to expose our swagger ui so it can be consumed by a confluence page with the built in widget. As this is done from the client side we need to enable CORS. The problem is, as soon as I add a configuration for it the json validation fails in certain cases. Mind you not all, but it seems fields that are defined as enums can no longer be validated.

Extract from our model and enum:

@Data
@Builder
@Jacksonized
public class RequestModel {
    
    @NotNull(message = OrderType.VALIDATION_MSG)
    @ApiModelProperty(notes = "Type of order: standard or exchange", example = "standard")
    private final OrderType orderType;
    
    @NotEmpty
    @ApiModelProperty(required = true, notes = "Post code of the customer", example = "ME15 6LH")
    private final String postCode;
    
    @Valid
    @NotEmpty(message = "must have at least one product")
    @ApiModelProperty(required = true, notes = "List of products in the basket")
    private final List<Product> products;
}

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum OrderType {
  STANDARD("Standard"), EXCHANGE("Exchange");

  public static final String VALIDATION_MSG = "must be one of 'Standard' or 'Exchange'";

  private final String value;

  @JsonValue
  public String getValue() {
    return value;
  }
}

The config looks like this as explained by baeldung:

@Configuration
@EnableWebFlux
public class CorsGlobalConfig implements WebFluxConfigurer {

  @Override
  public void addCorsMappings(CorsRegistry corsRegistry) {
    corsRegistry.addMapping("/v3/api-docs")
            .allowedOrigins("https://wiki.domain.com")
            .allowedMethods(HttpMethod.OPTIONS.toString(), HttpMethod.GET.toString());
  }
}

Without this class the validations are fine and the response is a json with our custom validation message. But as soon this class is active we get the following error:

{
    "timestamp": 1663743832719,
    "path": "/api/v1/endpoint",
    "status": 400,
    "error": "Bad Request",
    "requestId": "e6a5565c-1"
}

Does this config need some additional settings so the validation works again?

0 Answers
Related