question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HATEOAS part shows direct object instead of the thing we get

See original GitHub issue

Hi, currently I am using spring HATEOAS 1.0.1.RELEASE with spring-boot 2.2.1.RELEASE and springdoc-openapi-webmvc-core version 1.2.17. I saw that the project shows it support HATEOAS now but in swagger, I still see the example value show all the fields of EntityModel instead of the response we will get. Is there anything I need to config for that

Example value:

{
  "id": "string",
  "links": [
    {
      "rel": "string",
      "href": "string",
      "hreflang": "string",
      "media": "string",
      "title": "string",
      "type": "string",
      "deprecation": "string",
      "profile": "string",
      "name": "string"
    }
  ]
}

Response thing:

{
  "id": "test-id",
  "_links": {
    "self": {
      "href": "http://localhost:8374/my-path"
    }
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
mathieskristensencommented, Dec 10, 2019

Hi,

As @kingtran2112 is pointing out, there is a mismatch between the generated documentation (based on EntityModel) and the actual JSON representations. If you are relying on the OpenAPI specification for customers and you are using Spring HATEOAS, this is not desirable.

I would expect links and embedded to follow the HAL specification draft (link), i.e. _links and _embedded. Furthermore, I would expect the structure within these sections to reflect the actual response representations rather than being based on the EntityModel structure.

1reaction
milindraocommented, Jul 14, 2020

Thanks. I fixed it by doing the following.

Declare a CaseHrefRepresentation class

@SuppressWarnings("rawtypes")
@Schema (example = "{\"_links\": {\"self\": {\"href\": \"http://<domain:port>/cases/id/unique-key/case_id\"}}}")
class CaseHrefRepresentation extends RepresentationModel {}

In the method definition, use the class in the schema

@Operation(summary = "Creates and saves a case.",
           responses = { 
                @ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(implementation = CaseHrefRepresentation.class))),
                @ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content),
})
@ResponseStatus(HttpStatus.CREATED)     // Needed to prevent the creation of HttpStatus.OK in swagger.
@PostMapping(value = "/cases",
             produces = {application/hal+json},
             consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<RepresentationModel<?>> createCase(

I’m sure there’s a better more correct way to do this. But for now, it works for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring HATEOAS - Reference Documentation
Spring HATEOAS lets you work with links through its immutable Link value type. Its constructor takes both a hypertext reference and a link ......
Read more >
How to remove the "_embedded" property in Spring HATEOAS
I have a projection on an object, and am limiting it to only display the name. The problem is that there are over...
Read more >
JSON:API for Spring HATEOAS - GitHub Pages
A complex Link object of Spring HATEOAS can have optional properties like name, type, hreflang, title and others. In previous versions of this ......
Read more >
Applying HATEOAS to a REST API with Spring Boot
In this scenario it returns ResponseEntity<PersonResource> (HATEOAS) instead of ResponseEntity<Person> (REST). So what is this resource object I keep mentioning ...
Read more >
Insecure Direct Object Reference: Is your ... - Lock Me Down
We're all familiar with the idea of showing products on our website, ... In this case, what we have is a direct object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found