question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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<T> 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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
domdorncommented, 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 😃

1reaction
bnasslahsencommented, Jun 8, 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;
}

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Guide to Collections API in Vavr - Baeldung
In this article, we explore its powerful collections API. To get more information about this library, please read this article.
Read more >
Java Collection Views - Blog - Vavr
Conversion to Java collections using the toJava*() methods requires to iterate the persistent collection and build a new Java collection by ...
Read more >
Functional Programming in Java 8 with vavr - jOOQ blog
Guava is designed to be a toolset of missing types and extensions to existing Java types. It is superior in simplifying daily tasks,...
Read more >
Using vavr to write more robust Java code - Stéphane Derosiaux
We'll work from a base solution and improve it using vavr and some of its functional types: Either, then a combination of Either...
Read more >
Immutable Collections should be Your Default
Java's standard library has utilities for creating unmodifiable copies, which are better than nothing; · VAVR works well for Java; · Guava is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found