quarkusio / quarkus

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

Support title and type fields when generating HAL links #44501

Closed Sgitario closed 1 week ago

Sgitario commented 1 week ago

Before these changes, we were only considering the "href" field for the HAL links. Example:

{
  "_links": {
    "subject": {
      "href": "/subject"
    }
  }
}

After these changes, users that populate also the title and/or the type like:

@GET
    @Path("/with-rest-link-with-all-fields")
    @Produces({ MediaType.APPLICATION_JSON, RestMediaType.APPLICATION_HAL_JSON })
    public HalEntityWrapper<TestRecordWithIdAndPersistenceIdAndRestLinkId> get() {

        var entity = // ...
        return new HalEntityWrapper<>(entity,
                Link.fromUri(URI.create("/path/to/100"))
                        .rel("all")
                        .title("The title link") // the link title
                        .type(MediaType.APPLICATION_JSON) // the link type
                        .build());
    }

Or using the annotation like:

@GET
    @Produces({ MediaType.APPLICATION_JSON, RestMediaType.APPLICATION_HAL_JSON })
    @RestLink(entityType = TestRecordWithRestLinkId.class, title = "The with rest link title", type = MediaType.APPLICATION_JSON)
    @InjectRestLinks
    public TestRecordWithRestLinkId get() {
        return // ...
    }

Then, the links will have the title and/or type fields populated:

{
  "_links": {
    "subject": {
      "href": "/subject",
      "title": "The with rest link title",
      "type": "application/json"
    }
  }
}
Sgitario commented 1 week ago

Note that the HAL specs from here also mentions other objects like: templated, deprecation, profile... . To avoid overcomplicating these changes, I decided to only support the title and type fields.

And finally, about documentation, the guide mentions how HAL links, but it does not go into details about href, so I decided that the API is descriptive enough and then do not update the guide.

fyi @geoand @gilday

quarkus-bot[bot] commented 1 week ago

Status for workflow Quarkus CI

This is the status report for running Quarkus CI on commit 679c1ed522eae8cd304d4639d7fac5ed8a6edcad.

:white_check_mark: The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

You can consult the Develocity build scans.