springdoc-openapi icon indicating copy to clipboard operation
springdoc-openapi copied to clipboard

how to Convert Option<CustomClass> to nullable CustomClass

Open schneiderlin opened this issue 1 year ago • 0 comments

I want to convert Option<A> to a nullable A.

as https://github.com/springdoc/springdoc-openapi/issues/712#issuecomment-639062595 suggest

@Override
public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context, Iterator<ModelConverter> chain) {
    JavaType javaType = Json.mapper().constructType(annotatedType.getType());
    if (javaType != null) {
        Class<?> cls = javaType.getRawClass();
        if (io.vavr.control.Option.class.equals(cls)) {
            annotatedType = new AnnotatedType()
                    .type(javaType.containedType(0))
                    .ctxAnnotations(annotatedType.getCtxAnnotations())
                    .parent(annotatedType.getParent())
                    .schemaProperty(annotatedType.isSchemaProperty())
                    .name(annotatedType.getName())
                    .resolveAsRef(annotatedType.isResolveAsRef())
                    .jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
                    .propertyName(annotatedType.getPropertyName())
                    .skipOverride(true);
            Schema resolved = this.resolve(annotatedType, context, chain);

            // ------------ addition line ----------------------------
            resolved.setNullable(true);
            return resolved;
        }
    } 
    return (chain.hasNext()) ? chain.next().resolve(annotatedType, context, chain) : null;
}

but it only works for primative types like Option<Integer>, Option<String>. Option<CustomClass> would be convert to CustomClass, without nullable set to true. Can anyone help?

schneiderlin avatar Jan 05 '24 10:01 schneiderlin