0

In the project we use open api 3.0. I need to write a custom validator for JsonNullable value... specification:

Characteristic:
      type: object
      required:
        - name
        - value
      description: Describes a given characteristic of an object or entity through a
        name/value pair.
      properties:
        id:
          type: string
          description: Unique identifier of characteristic
        name:
          type: string
          description: Name of the characteristic
        valueType:
          type: string
          description: Data type of the value of the characteristic
        value:
          $ref: "#/components/schemas/Any"
          x-constraints: "@CustomNotNull"

anotation:

@Documented
@Constraint(validatedBy = {CustomCharacteristicValidator.class})
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomNotNull {
    String message() default "Value must not be null.";
    boolean required() default true;
}

validator:

  @Component
public class CustomCharacteristicValidator implements ConstraintValidator<CustomNotNull, JsonNullable<Object>> {

    @Override
    public boolean isValid(JsonNullable<Object> value, ConstraintValidatorContext context) {
//        if (Objects.nonNull(value.get())) {
//            context.disableDefaultConstraintViolation();
//            context.buildConstraintViolationWithTemplate("value can't be empty").addConstraintViolation();
//            return false;
//        }
        return true;
    }
}

used the following resources:

  1. How to inject custom spring validation inside swagger codegen?
  2. https://bartko-mat.medium.com/openapi-generator-to-spring-boot-with-custom-java-validations-623381df9215
  3. How to use custom validators in Spring

Problem: The required annotation is generated for all data types except $ref: "#/components/schemas/Any"

  /**
   * Get value
   * @return value
  */
  @NotNull 
**//should be here**
  @Schema(name = "value", required = true)
  public JsonNullable<Object> getValue() {
    return value;
  }

Maybe someone came across? Are there any solutions to this problem at all?

Vladius
  • 1
  • 3

0 Answers0