Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 0491e2b

Browse files
authored
test that subtypes are handled correctly in response (#392)
1 parent 195dd9c commit 0491e2b

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/payload/JacksonResponseFieldSnippetTest.java

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,13 +19,14 @@
1919
*/
2020
package capital.scalable.restdocs.payload;
2121

22-
import static capital.scalable.restdocs.SnippetRegistry.AUTO_REQUEST_FIELDS;
2322
import static capital.scalable.restdocs.SnippetRegistry.AUTO_RESPONSE_FIELDS;
2423
import static capital.scalable.restdocs.payload.TableWithPrefixMatcher.tableWithPrefix;
2524
import static capital.scalable.restdocs.util.FormatUtil.fixLineSeparator;
2625
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
2726
import static com.fasterxml.jackson.annotation.JsonProperty.Access.READ_ONLY;
2827
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;
2930
import static java.util.Collections.singletonList;
3031
import static org.assertj.core.api.Assertions.assertThat;
3132
import static org.mockito.Mockito.mock;
@@ -49,6 +50,8 @@
4950
import com.fasterxml.jackson.annotation.JsonIgnore;
5051
import com.fasterxml.jackson.annotation.JsonInclude;
5152
import com.fasterxml.jackson.annotation.JsonProperty;
53+
import com.fasterxml.jackson.annotation.JsonSubTypes;
54+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5255
import com.fasterxml.jackson.annotation.JsonUnwrapped;
5356
import com.fasterxml.jackson.databind.ObjectMapper;
5457
import org.junit.Before;
@@ -495,6 +498,42 @@ public void accessors() throws Exception {
495498
.row("bothWays", "String", "true", ""));
496499
}
497500

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+
498537
private void mockConstraintMessage(Class<?> type, String fieldName, String comment) {
499538
when(constraintReader.getConstraintMessages(type, fieldName))
500539
.thenReturn(singletonList(comment));
@@ -629,6 +668,14 @@ public HalItem halItem() {
629668
public ReadWriteAccessors accessors() {
630669
return new ReadWriteAccessors();
631670
}
671+
672+
public Response parentTypeAsResponse() {
673+
return new BalanceResponse();
674+
}
675+
676+
public BalanceResponse subTypeAsResponse() {
677+
return new BalanceResponse();
678+
}
632679
}
633680

634681
private static class Item {
@@ -744,4 +791,24 @@ private static class ReadWriteAccessors {
744791
private String writeOnly;
745792
private String bothWays;
746793
}
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+
}
747814
}

0 commit comments

Comments
 (0)