FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Replace nested Page with PagedModel when serialization mode is VIA_DTO by jjh75607 · Pull Request #3335 · springdoc/springdoc-openapi · GitHub

Replace nested Page with PagedModel when serialization mode is VIA_DTO - #3335

Open
jjh75607 wants to merge 1 commit into
springdoc:mainfrom
jjh75607:fix/nested-page-paged-model
Open

Replace nested Page with PagedModel when serialization mode is VIA_DTO#3335
jjh75607 wants to merge 1 commit into
springdoc:mainfrom
jjh75607:fix/nested-page-paged-model

Conversation

Copy link
Copy Markdown

Fixes #3319.

Summary

With spring.data.web.pageable.serialization-mode=VIA_DTO, a Page<T> returned directly (or inside ResponseEntity) is documented as PagedModel<T>, but a Page<T> nested in another type is not:

@GetMapping("/result")
public Result<Page<UserInfo>> listUserInfosWithResult() { ... }

The response is {"data": {"content": [...], "page": {...}}, ...}, while the spec documents data as the raw Page shape (pageable, sort, first, last, ...). The generated schema is also always named PageObject, so two wrappers with different element types share one schema and the second element type is missing from components.schemas.

PageOpenAPIConverter.resolve only swaps Page for PagedModel when the type is not a schema property. For a property it keeps Page and sets a name via getParentTypeName, which appends the parent's OpenAPI type ("object") rather than the parent class.

Change

  • In the schema-property branch, swap the AnnotatedType's type to the matching PagedModel<T> in place. The instance is kept, so ctxAnnotations, jsonViewAnnotation, parent and propertyName stay as they were (creating a new AnnotatedType here drops the @JsonView context from fix: propagate JsonView context when resolving Page<T> schema #3226).
  • Extract the PagedModel<T> type computation into pagedModelType(JavaType) so both branches share it.
Case Before After
Page<T> as response body, or inside ResponseEntity PagedModelT unchanged
DIRECT mode or property not set raw Page unchanged
Page<T> as a property of another type, VIA_DTO PageObject (raw Page shape, shared name) PagedModelT

Only the last row changes. PageObject, PageableObject and SortObject disappear from such specs and PagedModelXxx takes their place. A raw Page in that position becomes PagedModel, the same as a raw Page returned directly.

Test plan

  • Added Dummy<Page<String>> and Dummy<Page<Dummy<List<String>>>> endpoints to the app10 hateoas test (v30 and v31) and updated the four expected files. Without the fix the VIA_DTO variants fail with Unexpected: PageObject, PageableObject, SortObject; the DIRECT and not-specified variants pass before and after.
  • mvn test on springdoc-openapi-starter-common, -webmvc-api, -webflux-api, hateoas-tests, data-rest-tests, groovy-tests — all green.
  • Verified against the reporter's demo from I have set spring.data.web.pageable.serialization-mode: VIA_DTO, but the Page is not being transformed into a PagedModel. #3319 (Boot 4.1.1) with this branch installed as a snapshot: Result<Page<UserInfo>> and a second Result<Page<OtherInfo>> now get PagedModelUserInfo and PagedModelOtherInfo respectively, and the documented field set matches the actual response for every endpoint. DIRECT mode produces an identical document before and after.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I have set spring.data.web.pageable.serialization-mode: VIA_DTO, but the Page is not being transformed into a PagedModel.

1 participant


Back | FazBrowse Home | New Git URL