Skip to content

Spring Data Rest: Relations to exported Repositories are handled as if they were embedded #1084

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
mathias-ewald opened this issue Mar 2, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@mathias-ewald
Copy link

Describe the bug
When I create 2 entities A and B with a relation from A to B, the generated api doc describes the relation as B being embedded in A instead of modelling an href to another entity.

To Reproduce
Spring Boot: 2.4.3
Springdoc: 1.5.5

public class Pet {
        ...
	@ManyToOne
	private Owner owner;
}
public class Owner {
        ...
}

public interface PetRepo extends JpaRepository<Pet, UUID> {

}
public interface OwnerRepo extends JpaRepository<Owner, UUID> {

}

The generated api doc for GET /pets/{id}

  /pets/{id}:
    get:
      tags:
      - pet-entity-controller
      description: get-pet
      operationId: getItemResource-pet-get
      parameters:
      - name: id
        in: path
        required: true
        schema:
          type: string
      responses:
        "200":
          description: OK
          content:
            application/hal+json:
              schema:
                $ref: '#/components/schemas/EntityModelPet'
        "404":
          description: Not Found
...
    EntityModelPet:
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
        owner:
          $ref: '#/components/schemas/Owner'
        _links:
          $ref: '#/components/schemas/Links'

The owner property is not part of the response body of the GET call but it is a link in the _links section.

For the POST call, we get

    post:
      tags:
      - pet-entity-controller
      description: create-pet
      operationId: postCollectionResource-pet-post
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        "201":
          description: Created
          content:
            application/hal+json:
              schema:
                $ref: '#/components/schemas/EntityModelPet'
...
    Pet:
      required:
      - name
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
        owner:
          $ref: '#/components/schemas/Owner'

In this case, the owner property should be a String, not an Owner.

Expected behavior
In the GET-case, the model should not include any properties that are relations

In the POST-case the owner property should be a string, since the entity is references via href.

Current Workaround
I found a workaround by adding @Schema(implementation = String.class, accessMode = AccessMode.WRITE_ONLY) to the relation property in the data model as follows, but I think the springdoc should do it right out of the box.

public class Pet {
        ...
	@ManyToOne
        @Schema(implementation = String.class, accessMode = AccessMode.WRITE_ONLY)
	private Owner owner;
}
This was referenced Mar 16, 2021
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

2 participants