Skip to content

Spring Data Rest: Wrong response schema after POST,PUT,PATCH operations #1068

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 Feb 19, 2021 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@mathias-ewald
Copy link

Describe the bug
For POST,PUT,PATCH operations, Spring Data Rest returns the entity in the response body. For a given entity A, the generated api-doc specifies RepresentationModelA rather than EntityModelA as the returned schema. While EntityModelA contains all properties of the entity, RepresentationModelA defines only the _links, hence does not represent what Spring Data Rest returns.

To Reproduce

  • Spring Boot 2.4.3
  • Springdocs 1.5.4 (springdoc-openapi-ui, springdoc-openapi-data-rest)

Example entity:

@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Builder
public class Clinic {

	@Id
	@Type(type = "uuid-char")
	@Schema(hidden = true)
	private UUID id;

	@NotNull
	@NotBlank
	private String name;

	@PrePersist
	private void generateId() {
		if (id == null)
			id = UUID.randomUUID();
	}

}

Example Repository:

@CrossOrigin
public interface ClinicRepo extends CrudRepository<Clinic, UUID> {

}

Excerpt from api-docs:

...
paths:
  /clinics:
    ...
    post:
      ...
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Clinic'
      responses:
        "201":
          description: Created
          content:
            application/hal+json:
              schema:
                $ref: '#/components/schemas/RepresentationModelClinic'
...
components:
  schemas:
    ...
    RepresentationModelClinic:
      type: object
      properties:
        _links:
          type: array
          items:
            $ref: '#/components/schemas/Link'
    EntityModelClinic:
      type: object
      properties:
        name:
          type: string
        addresses:
          maxItems: 2147483647
          minItems: 1
          uniqueItems: true
          type: array
          items:
            $ref: '#/components/schemas/Address'
        _links:
          type: array
          items:
            $ref: '#/components/schemas/Link'
    ...

Expected behavior

  • A clear and concise description of what you expected to happen.
  • What is the expected result using OpenAPI Description (yml or json)?

The schema in the api-doc should match the actual data structure returned by Spring Data Rest. It is appropriately represented by EntityModelA but not RepresentationModelA.

@bnasslahsen
Copy link
Collaborator

@mathias-ewald,

Your analysis is correct.
I have added a fix for that.
You can test it using the latest snapshot.

This was referenced Mar 16, 2021
@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

2 participants