Skip to content

Commit

Permalink
updated for SpringDoc 2.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-kuba committed Apr 2, 2024
1 parent 2b389ff commit 39ce738
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -52,22 +53,25 @@ public ResponseEntity<ChatMessage> createMessage(NewChatMessageRequest r, String
.author(author)
.textColor(textColor != null ? textColor.getValue() : null)
.backgroundColor(backgroundColor);
messages.add(0, chatMessage);
messages.addFirst(chatMessage);
return new ResponseEntity<>(chatMessage, HttpStatus.CREATED);
}

@Override
public ResponseEntity<PageChatMessage> paged(Integer page, Integer size, List<String> sort) {
log.debug("paged(page={}, size={}, sort={})", page, size, sort);
PageRequest p = PageRequest.of(page, size);
log.debug("paged(page={}, size={})", page, size);
PageRequest p = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "timestamp"));
List<ChatMessage> chatMessages = messages.stream().skip(p.getOffset()).limit(p.getPageSize()).toList();
Page<ChatMessage> m = new PageImpl<>(chatMessages, p, messages.size());

// copy to generated models :-(
SortObject s = new SortObject()
.sorted(p.getSort().isSorted())
.unsorted(p.getSort().isUnsorted())
.empty(p.getSort().isEmpty());
List<SortObject> s = p.getSort().get().map(order -> new SortObject()
.property(order.getProperty())
.ascending(order.isAscending())
.direction(order.getDirection().name())
.ignoreCase(order.isIgnoreCase())
.nullHandling(order.getNullHandling().name())
).toList();
PageableObject pageableObject = new PageableObject()
.paged(p.isPaged())
.unpaged(p.isUnpaged())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ public ChatMessage createMessage(@Valid @RequestBody NewChatMessageRequest r,
@Operation(
summary = "Paged messages",
description = """
Returns a page of chat messages. Messages are ordered from the newest to the oldest.
Returns a page of chat messages.
The parameter `page` specifies zero-based index of the requested page,
and the parameter `size` specifies the size of the page.
the parameter `size` specifies the size of the page.
The parameter `sort` is ignored, sorting is always from the newest to the oldest.
""")
@GetMapping(path = "/paged")
@CrossOrigin(origins = "*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
Expand Down Expand Up @@ -39,7 +41,7 @@ public StoredMessage createNewMessage(String text, String author, String textCol
UUID uuid = UUID.randomUUID();
StoredMessage c = new StoredMessage(uuid.toString(), ZonedDateTime.now(), text, author,
textColor, backgroundColor, random.nextLong());
messages.add(0, c);
messages.addFirst(c);
return c;
}

Expand All @@ -62,7 +64,8 @@ public Page<StoredMessage> getPageOfMessages(Pageable pageable) {
.skip(pageable.getOffset())
.limit(pageable.getPageSize())
.toList();
Page<StoredMessage> page = new PageImpl<>(msgs, pageable, messages.size());
Page<StoredMessage> page = new PageImpl<>(msgs, PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
Sort.Direction.DESC, "timestamp"), messages.size());
log.debug("pageable = {}", pageable);
log.debug("page: {}", page);
return page;
Expand Down
31 changes: 20 additions & 11 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ paths:
- Chat
summary: Paged messages
description: |
Returns a page of chat messages. Messages are ordered from the newest to the oldest.
Returns a page of chat messages.
The parameter `page` specifies zero-based index of the requested page,
and the parameter `size` specifies the size of the page.
the parameter `size` specifies the size of the page.
The parameter `sort` is ignored, sorting is always from the newest to the oldest.
operationId: paged
parameters:
- name: page
Expand Down Expand Up @@ -257,12 +258,12 @@ components:
PageChatMessage:
type: object
properties:
totalElements:
type: integer
format: int64
totalPages:
type: integer
format: int32
totalElements:
type: integer
format: int64
pageable:
$ref: '#/components/schemas/PageableObject'
first:
Expand All @@ -280,7 +281,9 @@ components:
type: integer
format: int32
sort:
$ref: '#/components/schemas/SortObject'
type: array
items:
$ref: '#/components/schemas/SortObject'
numberOfElements:
type: integer
format: int32
Expand All @@ -299,19 +302,25 @@ components:
type: integer
format: int64
sort:
$ref: '#/components/schemas/SortObject'
type: array
items:
$ref: '#/components/schemas/SortObject'
paged:
type: boolean
unpaged:
type: boolean
SortObject:
type: object
properties:
sorted:
type: boolean
empty:
direction:
type: string
nullHandling:
type: string
ascending:
type: boolean
unsorted:
property:
type: string
ignoreCase:
type: boolean
responses:
SingleMessageResponse:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down

0 comments on commit 39ce738

Please sign in to comment.