спецификация:
Код: Выделить всё
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"
Код: Выделить всё
@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;
}
Код: Выделить всё
@Component
public class CustomCharacteristicValidator implements ConstraintValidator {
@Override
public boolean isValid(JsonNullable value, ConstraintValidatorContext context) {
// if (Objects.nonNull(value.get())) {
// context.disableDefaultConstraintViolation();
// context.buildConstraintViolationWithTemplate("value can't be empty").addConstraintViolation();
// return false;
// }
return true;
}
}
- Как внедрить пользовательскую проверку Spring в генератор кода Swagger?
- https://bartko-mat.medium.com/openapi-g ... 3381df9215
- Как использовать пользовательские валидаторы в Spring
Код: Выделить всё
/**
* Get value
* @return value
*/
@NotNull
**//should be here**
@Schema(name = "value", required = true)
public JsonNullable getValue() {
return value;
}
Подробнее здесь: https://stackoverflow.com/questions/733 ... 3-0-0-java
Мобильная версия