Skip to content
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

Spring Data REST Excerpt projections are missing from spec #2581

Open
janjouketjalsma opened this issue Apr 22, 2024 · 3 comments
Open

Spring Data REST Excerpt projections are missing from spec #2581

janjouketjalsma opened this issue Apr 22, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@janjouketjalsma
Copy link

janjouketjalsma commented Apr 22, 2024

Describe the bug
Spring Data REST allows you to define an excerpt projection for an entity. Adding this will automatically embed this projection whenever the entity is shown as part of a parent collection.

From the docs:

(...) The preceding example directs Spring Data REST to use the NoAddresses projection when embedding Person resources into collections or related resources.

It looks like this excerpt projection functionality is not (correctly) covered by Spring Doc because the embed is not showing up in the generated OpenAPI spec.

To Reproduce
Steps to reproduce the behavior:

  • Spring Boot: 3.2.5
  • Packages:
    • org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0
    • org.springdoc:springdoc-openapi-starter-webmvc-api:2.5.0
    • org.springdoc:springdoc-openapi-starter-common:2.5.0
  • Steps
    • Create an entity with a relationship to another entity
    • Create an excerpt projection for the second entity
    • Check the OpenAPI spec for the first entity and verify if the excerpt projection is part of it

Reproducer project: https://github.com/janjouketjalsma/spring-doc-excerpt-projection-issue-reproducer

Current behaviour
Only the direct properties of the main entity end up in the spec.

      "EntityModelBookStoreEntity": {
        "required": [
          "books",
          "name"
        ],
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "_links": {
            "$ref": "#/components/schemas/Links"
          }
        }
      },

Expected behavior
I expect the excerpt projection to be part of the spec for the main entity.

Example: (Bookstore is the main entity, Book is the one with the excerpt projection)

      "EntityModelBookStoreEntity": {
        "required": [
          "books",
          "name"
        ],
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "_embedded" : {
            "type": "object",
            "properties": {
              "books": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "name": {
                      "type": "string"
                    },
                    "_links": {
                      "$ref": "#/components/schemas/Links"
                    }
                  }
                }
              }
            }
          }
          "_links": {
            "$ref": "#/components/schemas/Links"
          }
        }
      },

(or maybe with a $ref, not sure what would be the implementation)

@quocanha
Copy link

quocanha commented May 29, 2024

I'm having a similar use-case where I want to use Spring Data Rest to provide the standard CRUD endpoints. I do not have the answer to OP, but want to voice my journey so far since it is the same use-case.

Additional to the standard out-of-the-box configuration of SDR, in order to minimize http calls, I want to use Excerpts and Projections, as mentioned in OP, to return more data than is strictly REST in these endpoints. Unfortunately I encountered the same problem that these Projections are not represented in the resulting OpenAPI spec.

I have found https://dzone.com/articles/extending-swagger-and-spring-doc-open-api (from https://springdoc.org/#other-resources), which looks very promising in terms of adding properties. The crux of the article is to extend the ModelResolver class that is used by Swagger and override the methods that it uses.

Unfortunately, my mind slowly implodes with my attempts to make sense of its main resolve function comprising of 800 lines.

My current understanding is that this is a recursive function that is called for each property of an object, which is a yet rather superficial description of what it does. My attempts at overriding the methods all failed due to me not properly understanding how to add properties to the current Schema object.

Although unconfirmed, I suspect to have found the required pieces once I know how to add properties to the schema, which entails retrieving all projections (using the EntityManager and then ProjectionDefinitionConfiguration::getProjectionsFor).

@bnasslahsen
Copy link
Contributor

@janjouketjalsma and @quocanha,

This is an enhancement request.
If you have identified, how to add the additional attributes feel to propose a PR.

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jun 16, 2024
@ashish3805
Copy link

+1 I am facing this issue as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants