quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.75k stars 2.67k forks source link

Automatically add HAL endpoint links with custom PanacheEntityResource methods #43842

Open muItilingualism opened 2 weeks ago

muItilingualism commented 2 weeks ago

Description

In quarkus-hibernate-orm-rest-data-panache according to the docs you can add additional endpoints to your PanacheEntityResource interface like so:

@ResourceProperties
public interface PeopleResource extends PanacheEntityResource<Person, Long> {
    @GET
    @Path("/name/{name}")
    @Produces("application/json")
    default List<Person> findByName(@PathParam("name") String name) {
        return Person.find("name = :name", Collections.singletonMap("name", name)).list();
    }
}

Adding HAL support like so:

@ResourceProperties(hal = true)
public interface PeopleResource extends PanacheEntityResource<Person, Long> {
    @GET
    @Path("/name/{name}")
    @Produces({"application/json", "application/hal+json"})
    default List<Person> findByName(@PathParam("name") String name) {
        return Person.find("name = :name", Collections.singletonMap("name", name)).list();
    }
}

Note: also not sure why it is required to have the @Produces({...,"application/hal+json"}) since @ResourceProperties(hal = true) is defined.

However, this does not add the /name/{name} link.

Only the following default links are present

GET /person/name/John

{
  "_embedded": {
    "items": [
      {
        "id": 2,
        "name": "John",
        "_links": {
          "add": {
            "href": "http://localhost:8080/person"
          },
          "count": {
            "href": "http://localhost:8080/person/count"
          },
          "self": {
            "href": "http://localhost:8080/person/2"
          },
          "update": {
            "href": "http://localhost:8080/person/2"
          },
          "list": {
            "href": "http://localhost:8080/person"
          },
          "remove": {
            "href": "http://localhost:8080/person/2"
          }
        }
      }
    ]
  },
  "_links": {
    "add": {
      "href": "http://localhost:8080/person"
    },
    "count": {
      "href": "http://localhost:8080/person/count"
    },
    "list": {
      "href": "http://localhost:8080/person"
    }
  }
}

It would be great if it was possible to add new methods which were also automatically added to the hal links. Or perhaps a way to specify that it should be included in the links. I have had no luck trying to get this to work in other ways (quarkus-hal and quarkus-rest-links) in conjunction with the easy to use quarkus-hibernate-orm-rest-data-panache.

Implementation ideas

No response

quarkus-bot[bot] commented 2 weeks ago

/cc @FroMage (panache), @geoand (rest-data-panache), @loicmathieu (panache)

geoand commented 2 weeks ago

cc @Sgitario

FroMage commented 1 week ago

It would indeed be nice. It would also be nice to make the @Produces inferred and automatic indeed.