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

Support title and type fields when generating HAL links #44501

Merged
merged 1 commit into from
Nov 14, 2024

Conversation

Sgitario
Copy link
Contributor

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"
    }
  }
}

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

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

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

```java
@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:

```java
@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:
```json
{
  "_links": {
    "subject": {
      "href": "/subject",
      "title": "The with rest link title",
      "type": "application/json"
    }
  }
}
```
@Sgitario
Copy link
Contributor Author

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

Copy link

quarkus-bot bot commented Nov 14, 2024

Status for workflow Quarkus CI

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

✅ 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.

Copy link
Contributor

@geoand geoand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@geoand geoand merged commit aeeb628 into quarkusio:main Nov 14, 2024
21 checks passed
@quarkus-bot quarkus-bot bot added this to the 3.18 - main milestone Nov 14, 2024
@Sgitario Sgitario deleted the 44452 branch November 14, 2024 11:39
@Sgitario Sgitario linked an issue Nov 14, 2024 that may be closed by this pull request
@gsmet gsmet modified the milestones: 3.18 - main, 3.17.0 Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

quarkus-hal omits link params from response entity
3 participants