Code Monkey home page Code Monkey logo

RestBuilder::getRouter passes a List<Object> as an array element, instead of converting the list to an array of objects about rest.vertx HOT 9 CLOSED

zandero avatar zandero commented on May 31, 2024
RestBuilder::getRouter passes a List as an array element, instead of converting the list to an array of objects

from rest.vertx.

Comments (9)

drejc avatar drejc commented on May 31, 2024

Not quite following what is the issue ... ?
You have registered MyObject.class as your API ... and It is not working? ...

Or are you trying to register and array as an API? ... this will not work ... as rest.vertx takes each registered object as an API endpoint ... so you are basically trying to register and ArrayList as an API endpoint? ....

from rest.vertx.

juanavelez avatar juanavelez commented on May 31, 2024

From the README.md, it is apparent you can register an instance of an object

RestRest rest = new TestRest();
Router router = RestRouter.register(vertx, rest);

vertx.createHttpServer()
	.requestHandler(router::accept)
	.listen(PORT);

I am trying to do such but via the Builder API. The instance is saved in the RestBuilder's apis instance variable which is an actual ArrayList. The apis instance variable is fed to the RestRouter.register call, which accepts an array of Objects (VarArgs). By doing so, the first object of the VarArgs becomes the ArrayList and not the elements of such List.

from rest.vertx.

drejc avatar drejc commented on May 31, 2024

Can you please provide the exact code ... I'm lost.

from rest.vertx.

juanavelez avatar juanavelez commented on May 31, 2024

Sure, I will work on a reproducer of the issue.

from rest.vertx.

juanavelez avatar juanavelez commented on May 31, 2024

More information. The RestBuilder.build() does build correctly, but the issue (and I am being picky here) is that the initial call by the build() method to getRouter() is not really doing anything because the passed parameter is an ArrayList. However down the build() code, there is an actual iteration of each of the elements of the apis variable which in that case is correctly registering the instance.

		// register APIs
		apis.forEach(api -> RestRouter.register(output, api));

I still think that this method is incorrect and not necessary (well, getting a Vert.x Router is necessary)

	private Router getRouter() {
		if (vertx == null) {
			return RestRouter.register(router, apis);
		}

		return RestRouter.register(vertx, apis);
	}

Oh,and thank you very much for creating this project, it is really helpful. I appreciate it.

from rest.vertx.

drejc avatar drejc commented on May 31, 2024

I see ... the getRouter() method is needed ... because you can build up you RESTs in two ways ...

  1. new RestBuilder(vertx). ... .build() OR
  2. new RestBuilder(router) ... build() in case you want to add new routes to an existing router
    so this is in place to match all possible cases ..

from rest.vertx.

juanavelez avatar juanavelez commented on May 31, 2024

Correct, but the thing is that this call

return RestRouter.register(vertx, apis);

Does not register anything because the object passed (a List) is begin treated as a List itself which has no annotations so the iteration over

			Map<RouteDefinition, Method> definitions = AnnotationProcessor.get(api.getClass());

Returns an empty Map (because the class of api at that point is java.util.ArrayList which has no REST annotations and neither its methods)

I agree that the RestBuilder either by vertx or router is needed.

I believe the solution is

	private Router getRouter() {
               Object[] apiObjects = apis.toList(new Object[apis.size()])

		if (vertx == null) {
			return RestRouter.register(router, apiObjects);
		}

		return RestRouter.register(vertx, apiObjects);
	}

However, this would duplicate the "registration" because you still have

		// register APIs
		apis.forEach(api -> RestRouter.register(output, api));

so the correct solution would be

	private Router getRouter() {
            return vertx == null ? router : Router.router(vertx);
	}

from rest.vertx.

drejc avatar drejc commented on May 31, 2024

O I see ... you want to register a list holding you APIs ... as:

List<Object> list = new ArrayList();
list.add(api1);
list.add(api2);

new RestBuilder(vertx).register(list).build();

This will not work ... the list is taken as the API (which in this case it is not) ...

Usage is intended to be:
new RestBuilder(vertx).register(api1, api2).build();

from rest.vertx.

juanavelez avatar juanavelez commented on May 31, 2024

Your RestBuilder api does provide a correct way to register things. The thing is that there is code that is not doing much besides creating the Vertx router. For example, try to debug

	@Test
	public void testCustomInput_WithBuilder(TestContext context) {

		Router router = new RestBuilder(vertx)
			                .register(TestReaderRest.class)
			                .build();

and you will see that the call to

return RestRouter.register(vertx, apis);

does nothing besides creating the vertx router because when iterates over the api loop variable, the class for the api object is a List which is not REST annotated, so there are no RouteDefinitions created.

from rest.vertx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.