Skip to content

Hateoas fields names mismatch #401

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

Closed
jlefebvre-vsct opened this issue Feb 4, 2020 · 10 comments
Closed

Hateoas fields names mismatch #401

jlefebvre-vsct opened this issue Feb 4, 2020 · 10 comments
Labels
bug Something isn't working

Comments

@jlefebvre-vsct
Copy link

The naming of the fields does not match the HAL specification draft (link) : the generated documentation (based on EntityModel) and the actual JSON representations are different, e.g. "links" instead of "_links" and using default name "content" instead of the specified one by using @org.springframework.hateoas.server.core.Relation(collectionRelation = "components") on the object class.

I am using Spring-boot 2.2.x and Springdoc-openapi 1.2.30.

The expected behavior is that the generated OpenAPI3 contract matches the effective produced data structure (the purpose of generating from sources 😄).

@bnasslahsen
Copy link
Collaborator

@jlefebvre-vsct,
As you seem to know well the specification, can you provide a PR about it?

@jlefebvre-vsct
Copy link
Author

jlefebvre-vsct commented Feb 5, 2020

Hi @bnasslahsen sorry, I don't know the spec well but the generated openapi doesn't match what is actually produced and I don't manage to get them match (any workaround ?).

I have prepared the following demo to illustrate and facilitate further investigation :

Useful demo endpoints :

Notable differences :

  • "links" instead of "_links"
  • "links" structure is wrong (should be a map of links by their name instead of an array)
  • "content" should be wrapped inside an "_embedded"
  • "content" should be named "components" (in this example)
  • "content" structure is wrong (links instead of payload type)

@jlefebvre-vsct jlefebvre-vsct removed their assignment Feb 6, 2020
@jlefebvre-vsct
Copy link
Author

I don't know springdoc nor swagger at all but I looked at them to figure out how to fix these problems.
I noted the following start points but I didn't manage to fix the schema even a quite dirty way :

  • injecting a bean "List" should allow us to customize the properties :
    Schema customize(Schema property, AnnotatedType type);
  • SpringDocAnnotationsUtils#extractSchema
  • use customizers to change the schema after it has been generated (depends on how swagger-core stores the models)

@jlefebvre-vsct
Copy link
Author

jlefebvre-vsct commented Feb 6, 2020

"content" of PagedModel was wrong because I was using ResponseEntity<PagedModel<RepresentationModel<EntityModel>> instead of ResponseEntity<PagedModel<EntityModel>

@jlefebvre-vsct
Copy link
Author

everything matches when NOT using HAL format 👍

@Configuration
public class SpringRestConfiguration implements RepositoryRestConfigurer {
	@Override
	public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
		config.setDefaultMediaType(MediaType.APPLICATION_JSON);
		config.useHalAsDefaultJsonMediaType(false);
	}
}

and

spring.hateoas.use-hal-as-default-json-media-type: false

@bnasslahsen
Copy link
Collaborator

bnasslahsen commented Feb 9, 2020

Hi @jlefebvre-vsct,

Thank you for providing all this relevant information.
With v1.2.31, a better support for HAL fields naming (_links, _embedded, ...) will be added.

BTW, for your sample you just need to declare springdoc-openapi-ui and springdoc-openapi-data-rest:

  • springdoc-openapi-webmvc-core is by transitivity included in springdoc-openapi-ui.
  • The same applies for classgraph.

@ch4mpy
Copy link

ch4mpy commented Apr 23, 2020

@bnasslahsen Is there any doc about "better support for HAL fields naming" ?
I'm trying with 1.3.4 but have no idea how to generate the schemas for an HATEOAS app configured with @EnableHypermediaSupport(type = HypermediaType.HAL)

@ch4mpy
Copy link

ch4mpy commented Apr 24, 2020

@bnasslahsen thank you for your answer. I had missed the parapgraph about Pageable support (I don't know how as I applied the security one...)

The json schema now seems ok. Unfortunately, openapitools (4.3.0) then fails to process it:

[main] INFO  o.o.c.l.TypeScriptAngularClientCodegen - generating code for Angular 9.0.0 ...
[main] INFO  o.o.c.l.TypeScriptAngularClientCodegen -   (you can select the angular version by setting the additionalProperty ngVersion)
Exception in thread "main" java.lang.StackOverflowError
        at java.base/java.lang.String.startsWith(String.java:1470)
        at org.openapitools.codegen.utils.ModelUtils.getSimpleRef(ModelUtils.java:342)
        at org.openapitools.codegen.utils.ModelUtils.unaliasSchema(ModelUtils.java:1025)
        at org.openapitools.codegen.utils.ModelUtils.unaliasSchema(ModelUtils.java:1066)

but I couldn't yet figure out if it's related to a bad schema or bogous tool...

@ch4mpy
Copy link

ch4mpy commented Apr 24, 2020

@bnasslahsen I think the issue in in springdoc: Link schema definition is a reference to itself (missing definition and parsing results in an infinite loop).

I opened an issue for that: #609

@bnasslahsen bnasslahsen added the bug Something isn't working label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants