Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please point me on how to add new collection types (e.g. Vavr / Guava) #712

Closed
domdorn opened this issue Jun 4, 2020 · 5 comments
Closed

Comments

@domdorn
Copy link

domdorn commented Jun 4, 2020

I'm trying to add the collection types ( Option, List, Tuple, etc.) from the vavr-library, but can't seem to find where to plug them in (https://www.vavr.io/vavr-docs/).
I already figured out on how to make the Future type work here https://dominikdorn.com/2020/05/spring-doc-openapi-swagger-vavr-future/
but I'm a little lost on how to make the collections types work.
It would be great if you could point me in the right direction!

This is what I tried so far:

  1. I've already tried creating a io.swagger.v3.core.converter.ModelConverter but this leads to NPEs when swagger tries to clone the schema.
  2. I also tried SpringDocUtils.getConfig().replaceWithClass(io.vavr.control.Option.class, java.util.Optional.class); but this seems to be unable to handle generics.
  3. I tried to register the VavrModule with the ObjectMapper of SpringDoc/Swagger, but this also doesn't help ( Json.mapper().registerModule(new VavrModule()); )

Thx,
Dominik

@bnasslahsen
Copy link
Contributor

bnasslahsen commented Jun 4, 2020

@domdorn,

For collections, i assume you already have registred Jackson VavrModule.
If you need to use the mapping of Java Collection instead of the one of Vavr Collection. You can declare in your application:

static {
	SpringDocUtils.getConfig()
			.replaceWithClass(io.vavr.collection.Set.class, java.util.Set.class)
			.replaceWithClass(io.vavr.collection.List.class, java.util.List.class)
			.replaceWithClass(io.vavr.collection.Map.class, java.util.Map.class);
}

For io.vavr.control.Option, you can use a custom ModelConverter, as it is not available on ModelResolver._isOptionalType is only supporting com.google.common.base.Optional and java.util.Optional.
Here is a sample code for a ModelConverter.

@Component
public class VavrOptionSupportConverter implements ModelConverter {

@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);
			return this.resolve(annotatedType, context, chain);
		}
	}
	return (chain.hasNext()) ? chain.next().resolve(annotatedType, context, chain) : null;
}

}

@domdorn
Copy link
Author

domdorn commented Jun 9, 2020

perfect, thank you @bnasslahsen ! While the replaceWithClass didn't had the intended result (e.g. it lost type information and thus all collections became untyped arrays), your code of the ModelConverter works good enough so I could also implement it for the collection types. Will create a blog post with the full code soon and give attribution to you @bnasslahsen !

@rhushikeshSahaj
Copy link

rhushikeshSahaj commented Nov 2, 2021

Hi @domdorn, did you write a blog post for collection types. I am also facing the same problem so it would be a great help for me

@rhushikeshSahaj
Copy link

rhushikeshSahaj commented Nov 2, 2021

hi @domdorn found your gist here https://gist.github.com/domdorn/bddf5ce1348464179fdd4ffc65b70870

thanks, it helped.

Commenting it here so that others will be able to find it easily

@domdorn
Copy link
Author

domdorn commented Nov 2, 2021

The post is supposed to be this one:
https://dominikdorn.com/2020/05/spring-doc-openapi-swagger-vavr-future/
but apparently the github-gist plugin for wordpress fails for some reason.. anyway, you've found the right gist :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants