Skip to content

HATOEAS Links produces a circular reference #1014

Closed
@twobiers

Description

@twobiers

Describe the bug
When using springodc-openapi-hateoas in a Spring Boot HATEOAS project the generated OpenAPI Schema of the HATEOAS Links object is a circular reference to itself.

To Reproduce

Steps to reproduce the behavior:
I am using the following Dependencies:

<dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-ui</artifactId>
      <version>1.5.2</version>
    </dependency>
    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-webmvc-core</artifactId>
      <version>1.5.2</version>
    </dependency>
    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-hateoas</artifactId>
      <version>1.5.2</version>
    </dependency>
    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-security</artifactId>
      <version>1.5.2</version>
    </dependency>

And a simple HATEOAS controller like this:

@RestController
public class FooController {

  private final FooService fooService;
  private final FooRepresentationModelAssembler fooRepresentationModelAssembler;

  @Autowired
  public FooController(
      FooService fooService,
      FooRepresentationModelAssembler fooRepresentationModelAssembler) {
    this.fooService = fooService;
    this.fooRepresentationModelAssembler = fooRepresentationModelAssembler;
  }

  @GetMapping(value = "foo/{id}", produces = MediaTypes.HAL_JSON_VALUE)
  public ResponseEntity<EntityModel<Foo>> getFoo(@PathVariable("id") UUID id) throws Exception {
    var foo = fooService.getFoo(id).orElseThrow(Exception::new);
    return ResponseEntity.ok(fooRepresentationModelAssembler.toModel(foo));
  }
}

Springdoc generates the following Schema definition:

{
   "EntityModelFoo":{
      "type":"object",
      "properties":{
         "_links": {
            "$ref":"#/components/schemas/Links"
         }
      }
   },
   "Links":{
      "$ref": "#/components/schemas/Links",
   }
}

Expected behavior
I would expect the schema to be like this:

{
   "EntityModelFoo":{
      "type":"object",
      "properties":{
         "links":{
            "type":"array",
            "items":{
               "$ref":"#/components/schemas/Link"
            }
         }
      }
   },
   "Link":{
      "type":"object",
      "properties":{
         "rel":{
            "type":"string"
         },
         "href":{
            "type":"string"
         },
         "hreflang":{
            "type":"string"
         },
         "media":{
            "type":"string"
         },
         "title":{
            "type":"string"
         },
         "type":{
            "type":"string"
         },
         "deprecation":{
            "type":"string"
         },
         "profile":{
            "type":"string"
         },
         "name":{
            "type":"string"
         }
      }
   }
}

Additional context
For me this is a problem as the OpenAPI Generator will produce a StackOverflow Error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions