Read a custom property and populate the swagger UI with specified attributes

127 Views Asked by At

My requirement is to have a generic annotation specified with all the required javax and swagger annotations so that I can reuse it in multiple places, below is my generic annotation,

import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import net.contal.api.common.utils.domain.DomainPersonName;

import javax.validation.Constraint;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Schema(extensions = {
        @Extension(properties = { @ExtensionProperty(name = DomainPersonName.UI_ELEMENT_KEY, value =
                DomainPersonName.UI_ELEMENT_VALUE), @ExtensionProperty(name = DomainPersonName.ERROR_MESSAGE_KEY,
                value = DomainPersonName.ERROR_MESSAGE)})})
@Size(min = DomainPersonName.MIN_CHARACTER_LENGTH, max = DomainPersonName.MAX_CHARACTER_LENGTH)
@Pattern(regexp= DomainPersonName.REGEX)
@Target({METHOD, FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PersonName {
    String message() default "PersonName is not valid";
    Class<?>[] groups() default { };
    Class<? extends PersonName>[] PersonName() default { };
}

Then In my profile controller has a request object (@Valid @RequestBody ProfileDto profileDto) which I specify above custom annotation to relevant places like below,

@PersonName
private String givenName;

Since swagger does not read this annotation, I wrote a custom PropertyCustomizer where i need to identify the PersonName annotation and read @Schema , @Size and @Pattern annotations and populate Schema property, sample I tried is below,

@Component
public class CustomAnnotationRegister implements PropertyCustomizer {

    @Override
    public Schema customize(Schema property, AnnotatedType type) {
        if (type.getCtxAnnotations() == null) {
            return property;
        }

        final PersonName[] foundPersonNameAnnotation = {null};

        boolean isDomainPersonName = Arrays.stream(type.getCtxAnnotations())
                .anyMatch(annotation -> {
                    if (annotation.annotationType().equals(PersonName.class)) {
                        foundPersonNameAnnotation[0] = (PersonName) annotation;
                        return true;
                    }
                    return false;
                });

        if (isDomainPersonName) {

            PersonName x = foundPersonNameAnnotation[0];
            Annotation[] list = foundPersonNameAnnotation[0].annotationType().getAnnotations();

            Annotation[] annotations = foundPersonNameAnnotation[0].annotationType().getAnnotations();
            for (Annotation annotation : annotations) {
               
                if (annotation.annotationType().getName().equals("io.swagger.v3.oas.annotations.media.Schema")) {
                    
                }
            }

        }

        return property;
    }
}

Im not sure how to read the schema and other attributes. Any help would be appreciated Springdoc 1.7.0

0

There are 0 best solutions below