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

JsonApiModelBuilder is not compatible with ReactiveRepresentationModelAssembler #61

Closed
jimirocks opened this issue May 30, 2022 · 5 comments

Comments

@jimirocks
Copy link
Contributor

jimirocks commented May 30, 2022

As I commented on #59 (after close), I believe it should be possible to use JsonApiModelBuilder while implementing ReactiveRepresentationModelAssembler but it's not. See the code, which can't compile on the class declaration:

import com.toedter.spring.hateoas.jsonapi.JsonApiModelBuilder;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.hateoas.server.reactive.ReactiveRepresentationModelAssembler;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class JavaAssembler extends ReactiveRepresentationModelAssembler<MyStupidDto, RepresentationModel<?>> {
    @Override
    public Mono<RepresentationModel<?>> toModel(final MyStupidDto entity, final ServerWebExchange exchange) {
        return Mono.just(JsonApiModelBuilder.jsonApiModel().model(entity).build());
    }

    @Override
    public Mono<CollectionModel<RepresentationModel<?>>> toCollectionModel(final Flux<? extends MyStupidDto> entities, final ServerWebExchange exchange) {
        throw new UnsupportedOperationException("not implemented");
    }
}

I see two possible solutions:

  1. Making JsonApiModel public - as suggested in Make JsonApiModel public #59
  2. changing the generics of ReactiveRepresentationModelAssembler to <T, D extends RepresentationModel<?>> from <T, D extends RepresentationModel<D>>
@toedter
Copy link
Owner

toedter commented May 30, 2022

The code does not compile because D extends RepresentationModel<D>> is required in the signature, not RepresentationModel<?>. But the only thing a ReactiveRepresentationModelAssembler implements, is one line of code in the method toCollectionModel. This original method does not make sense when using JsonApiModels because a JsonApiModel can reprersent a collection model itself.

If you want to use an assembler to create representation models, you can easily build your own, like.

public class MyAssembler {
    public Mono<RepresentationModel<?>> toModel(final MyStupidDto entity, final ServerWebExchange exchange) {
        return Mono.just(JsonApiModelBuilder.jsonApiModel().model(entity).build());
    }

    public Mono<RepresentationModel<?>> toCollectionModel(Flux<? extends MyStupidDto> entities, ServerWebExchange exchange) {
       // use the builder here to create a JsonApiModel based on entities
    }
}

So why would you like to re-use ReactiveRepresentationModelAssembler at all?

@jimirocks
Copy link
Contributor Author

You are right, that we can bypass ReactiveRepresentationModelAssembler - nothing basically forces us to use that particular interface. So this issue is not anyhow blocking us.

But the fact JsonApiModeBuilder is not compatible with ReactiveRepresentationModelAssembler is still here...

@toedter
Copy link
Owner

toedter commented May 30, 2022

@jimirocks
you wrote: But the fact JsonApiModeBuilder is not compatible with ReactiveRepresentationModelAssembler is still here...

yes, you are right, but the main reason for that is that those assembler interfaces were designed to work mostly with the build-in EntityModel and CollectionModel implementations in mind.

What I was actually thinking about was to create JsonApiModel wrappers to wrap JsonApiModels in EntityModels and CollectionModels, but unfortunatelly it turned to be way more complicated than expected...

Since it is no blocker for you I would close this issue and think further about the wraping :)

@jimirocks
Copy link
Contributor Author

yes, you are right, but the main reason for that is that those assembler interfaces were designed to work mostly with the build-in EntityModel and CollectionModel implementations in mind.

Ahh ha so that was the missing piece of puzzle to understand. Thank you for explanation.

We will go with custom interface....

@toedter toedter closed this as completed May 30, 2022
@toedter
Copy link
Owner

toedter commented May 31, 2022

Just fyi, I filed an issue at Spring HATEOAS:
spring-projects/spring-hateoas#1796

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

2 participants