|
7 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | 8 | * you may not use this file except in compliance with the License. |
9 | 9 | * You may obtain a copy of the License at |
10 | | - * |
| 10 | + * |
11 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | - * |
| 12 | + * |
13 | 13 | * Unless required by applicable law or agreed to in writing, software |
14 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
19 | 19 | */ |
20 | 20 | package capital.scalable.restdocs.payload; |
21 | 21 |
|
22 | | -import static capital.scalable.restdocs.SnippetRegistry.AUTO_REQUEST_FIELDS; |
23 | 22 | import static capital.scalable.restdocs.SnippetRegistry.AUTO_RESPONSE_FIELDS; |
24 | 23 | import static capital.scalable.restdocs.payload.TableWithPrefixMatcher.tableWithPrefix; |
25 | 24 | import static capital.scalable.restdocs.util.FormatUtil.fixLineSeparator; |
26 | 25 | import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; |
27 | 26 | import static com.fasterxml.jackson.annotation.JsonProperty.Access.READ_ONLY; |
28 | 27 | import static com.fasterxml.jackson.annotation.JsonProperty.Access.WRITE_ONLY; |
| 28 | +import static com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY; |
| 29 | +import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME; |
29 | 30 | import static java.util.Collections.singletonList; |
30 | 31 | import static org.assertj.core.api.Assertions.assertThat; |
31 | 32 | import static org.mockito.Mockito.mock; |
|
49 | 50 | import com.fasterxml.jackson.annotation.JsonIgnore; |
50 | 51 | import com.fasterxml.jackson.annotation.JsonInclude; |
51 | 52 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 53 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 54 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
52 | 55 | import com.fasterxml.jackson.annotation.JsonUnwrapped; |
53 | 56 | import com.fasterxml.jackson.databind.ObjectMapper; |
54 | 57 | import org.junit.Before; |
@@ -495,6 +498,42 @@ public void accessors() throws Exception { |
495 | 498 | .row("bothWays", "String", "true", "")); |
496 | 499 | } |
497 | 500 |
|
| 501 | + @Test |
| 502 | + public void parentType() throws Exception { |
| 503 | + HandlerMethod handlerMethod = createHandlerMethod("parentTypeAsResponse"); |
| 504 | + |
| 505 | + new JacksonResponseFieldSnippet().document(operationBuilder |
| 506 | + .attribute(HandlerMethod.class.getName(), handlerMethod) |
| 507 | + .attribute(ObjectMapper.class.getName(), mapper) |
| 508 | + .attribute(JavadocReader.class.getName(), javadocReader) |
| 509 | + .attribute(ConstraintReader.class.getName(), constraintReader) |
| 510 | + .build()); |
| 511 | + |
| 512 | + assertThat(this.generatedSnippets.snippet(AUTO_RESPONSE_FIELDS)).is( |
| 513 | + tableWithHeader("Path", "Type", "Optional", "Description") |
| 514 | + .row("accountNumber", "String", "true", "") |
| 515 | + .row("balance", "Integer", "true", "") |
| 516 | + .row("transactions", "Array[Object]", "true", "") |
| 517 | + .row("transactions[].amount", "Decimal", "true", "")); |
| 518 | + } |
| 519 | + |
| 520 | + @Test |
| 521 | + public void subType() throws Exception { |
| 522 | + HandlerMethod handlerMethod = createHandlerMethod("subTypeAsResponse"); |
| 523 | + |
| 524 | + new JacksonResponseFieldSnippet().document(operationBuilder |
| 525 | + .attribute(HandlerMethod.class.getName(), handlerMethod) |
| 526 | + .attribute(ObjectMapper.class.getName(), mapper) |
| 527 | + .attribute(JavadocReader.class.getName(), javadocReader) |
| 528 | + .attribute(ConstraintReader.class.getName(), constraintReader) |
| 529 | + .build()); |
| 530 | + |
| 531 | + assertThat(this.generatedSnippets.snippet(AUTO_RESPONSE_FIELDS)).is( |
| 532 | + tableWithHeader("Path", "Type", "Optional", "Description") |
| 533 | + .row("accountNumber", "String", "true", "") |
| 534 | + .row("balance", "Integer", "true", "")); |
| 535 | + } |
| 536 | + |
498 | 537 | private void mockConstraintMessage(Class<?> type, String fieldName, String comment) { |
499 | 538 | when(constraintReader.getConstraintMessages(type, fieldName)) |
500 | 539 | .thenReturn(singletonList(comment)); |
@@ -629,6 +668,14 @@ public HalItem halItem() { |
629 | 668 | public ReadWriteAccessors accessors() { |
630 | 669 | return new ReadWriteAccessors(); |
631 | 670 | } |
| 671 | + |
| 672 | + public Response parentTypeAsResponse() { |
| 673 | + return new BalanceResponse(); |
| 674 | + } |
| 675 | + |
| 676 | + public BalanceResponse subTypeAsResponse() { |
| 677 | + return new BalanceResponse(); |
| 678 | + } |
632 | 679 | } |
633 | 680 |
|
634 | 681 | private static class Item { |
@@ -744,4 +791,24 @@ private static class ReadWriteAccessors { |
744 | 791 | private String writeOnly; |
745 | 792 | private String bothWays; |
746 | 793 | } |
| 794 | + |
| 795 | + @JsonTypeInfo(use = NAME, include = PROPERTY, property = "type", visible = true) |
| 796 | + @JsonSubTypes({ |
| 797 | + @JsonSubTypes.Type(value = BalanceResponse.class, name = "BalanceResponse"), |
| 798 | + @JsonSubTypes.Type(value = TransactionsResponse.class, name = "TransactionResponse") }) |
| 799 | + public static class Response { |
| 800 | + private String accountNumber; |
| 801 | + } |
| 802 | + |
| 803 | + public static class BalanceResponse extends Response { |
| 804 | + private int balance; |
| 805 | + } |
| 806 | + |
| 807 | + public static class TransactionsResponse extends Response { |
| 808 | + private List<Transaction> transactions; |
| 809 | + } |
| 810 | + |
| 811 | + public static class Transaction { |
| 812 | + private BigDecimal amount; |
| 813 | + } |
747 | 814 | } |
0 commit comments