Skip to content

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

Closed
@mathias-ewald

Description

@mathias-ewald

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.

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