ysk(0soo)
Lifealong
ysk(0soo)
전체 방문자
오늘
어제
  • 분류 전체보기 (238)
    • Java (50)
      • whiteship-java-study (11)
      • Java (28)
      • time (6)
    • Spring (68)
      • JPA (15)
      • Spring (1)
      • SpringBoot (1)
      • SpringMVC (6)
      • Spring Security (22)
      • Jdbc (1)
      • RestDocs (14)
      • log (6)
    • Kotlin (3)
    • Web (2)
      • nginx (1)
    • Database (14)
      • MySQL (5)
      • PostgreSQL (1)
      • SQL (1)
      • Redis (4)
    • C, C++ (0)
    • Git (1)
    • Docker (2)
    • Cloud (3)
      • AWS (3)
    • 도서, 강의 (0)
      • t5 (0)
    • 기타 (7)
      • 프로그래밍 (1)
    • 끄적끄적 (0)
    • CS (14)
      • 운영체제(OS) (2)
      • 자료구조(Data Structure) (9)
    • 하루한개 (12)
      • 우아한 테크코스-10분테코톡 (12)
    • 스터디 (12)
      • 클린 아키텍처- 로버트마틴 (2)
      • JPA 프로그래밍 스터디 (10)
    • 테스트 (34)
      • JUnit (19)
      • nGrinder (2)
      • JMeter (0)
    • Infra (3)
    • 프로그래머스 백엔드 데브코스 3기 (0)
    • 디자인 패턴 (3)
    • Issue (4)
    • system (1)
      • grafana (0)
      • Prometheus (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • github

공지사항

인기 글

태그

  • restdocs enum
  • nginx basic auth
  • querydsl
  • 정규표현식
  • AccessDecisionVoter 커스텀
  • 구조화된 동시성
  • java
  • 트랜잭션
  • restdocs custom
  • 가상 스레드 예외 핸들링
  • StructuredConcorrency
  • 동등성
  • nGrinder
  • UserDetailsService
  • node exporter basic auth
  • jpa
  • mysql
  • AccessDecisionManager
  • scope value
  • LocalDateTime
  • AuthenticationException
  • 가상 스레드
  • 동일성
  • 인가(Authorization) 처리
  • 동시성 제어
  • FilterSecurityInterceptor
  • DataJpaTest
  • junit5
  • VirtualThread Springboot
  • tree

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ysk(0soo)

Lifealong

Restdocs fieldWithPath depth(list, array, 배열 문서화)
Spring/RestDocs

Restdocs fieldWithPath depth(list, array, 배열 문서화)

2023. 1. 31. 00:50

RestDocs를 사용하여 필드를 문서화 때, 배열 depth가 길어지면 문서화하기 힘들어지는 경향이 있어 경험을 적어보려고 한다

  • https://jsoncrack.com/editor

라는 좋은 사이트를 사용하면 depth를 헷갈리지 않게 보기 좋다. (사랑해요 고마워요 스펜서)

  • https://jsonformatter.org/ : json 이쁘게 보는 사이트

예제 1 - 2depth

다음과 같은 depth의 response를 작성해야 한다면

{
  "data": [
    {
      "orderId": "20230130233846616HKuINK",
      "storeId": "store1",
      "orderStatus": "PAYMENT_COMPLETE",
      "nickname": "이디야화이팅",
      "items": [
        [
          {
            "itemName": "아이스 아메리카노",
            "quantity": 1,
            "espressoShotCount": 1,
            "espressoType": "DECAFFEINATED",
            "cupSize": "VENTI",
            "cupType": "PERSONAL"
          }
        ],
        [
          {
            "itemName": "카페 라떼",
            "quantity": 2,
            "espressoShotCount": 1,
            "espressoType": "DECAFFEINATED",
            "cupSize": "VENTI",
            "cupType": "PERSONAL"
          }
        ]
      ]
    },
    {
      "orderId": "20230130233846586HKDdfE",
      "storeId": "store1",
      "orderStatus": "PAYMENT_COMPLETE",
      "nickname": "이디야화이팅",
      "items": [
        [
          {
            "itemName": "아이스 아메리카노",
            "quantity": 1,
            "espressoShotCount": 1,
            "espressoType": "DECAFFEINATED",
            "cupSize": "VENTI",
            "cupType": "PERSONAL"
          }
        ],
        [
          {
            "itemName": "카페 라떼",
            "quantity": 2,
            "espressoShotCount": 1,
            "espressoType": "DECAFFEINATED",
            "cupSize": "VENTI",
            "cupType": "PERSONAL"
          }
        ]
      ]
    }
  ],
  "hasNext": false,
  "size": 4
}

다음과 같이 사용한다

data[].items[].[].필드이름
responseFields(
            
  fieldWithPath("data").type(JsonFieldType.ARRAY).description("데이터 양"),
  fieldWithPath("hasNext").type(JsonFieldType.BOOLEAN).description("다음 데이터 여부"),
  fieldWithPath("size").type(JsonFieldType.NUMBER).description("데이터 수"),
​
  fieldWithPath("data[].orderId").type(JsonFieldType.STRING)
    .description("주문 번호"),
  fieldWithPath("data[].storeId").type(JsonFieldType.STRING)
    .description("매장 아이디"),
  fieldWithPath("data[].orderStatus").type(JsonFieldType.STRING)
    .description("주문 상태"),
  fieldWithPath("data[].nickname").type(JsonFieldType.STRING)
    .description("주문 유저 명"),
  
  fieldWithPath("data[].items[]").type(JsonFieldType.ARRAY)
    .description("주문 아이템들"),
  fieldWithPath("data[].items[].[].itemName").type(JsonFieldType.STRING)
    .description("주문 아이템들"),
  fieldWithPath("data[].items[].[].quantity").type(JsonFieldType.NUMBER)
    .description("주문 수"),
  
  fieldWithPath("data[].items[].[].espressoShotCount").type(JsonFieldType.NUMBER)
  
  .description("에스프레소 샷 양"),
  
  fieldWithPath("data[].items[].[].espressoType").type(JsonFieldType.STRING)
  .description("커피 에스프레소 타입"),
  
  fieldWithPath("data[].items[].[].cupSize").type(JsonFieldType.STRING).description("컵 사이즈"),
  
  fieldWithPath("data[].items[].[].cupType").type(JsonFieldType.STRING).description("컵 타입")
    
)

1depth

만약 배열을 반환하고 있다면 이와 같은 식으로 작성하면 된다.

{
  [
    {
      score: 150
    },
    {
      score: 100
    }
  ]
}
fieldWithPath("[].score").type(JsonFieldType.NUMBER).description("score")

배열에 이름이 있다면

{
  scoreArray: [
    {
      score: 150
    },
    {
      score: 100
    }
  ]
}
fieldWithPath("scoreArray.[].score").type(JsonFieldType.NUMBER).description("score")

다른 예시

{
  "a":{
    "b":[
      {
        "c":"one"
      },
      {
        "c":"two"
      },
      {
        "d":"three"
      }
    ],
    "e.dot" : "four"
  }
}

아래 표는 위의 JSON 파일 기준으로 fieldWithPath()에 인자로 전달해야 하는 형식이다.

PathValue

a 요소 b 를 포함
a.b 세가지 요소를 포함하는 배열
['a']['b'] 세가지 요소를 포함하는 배열
a['b'] 세가지 요소를 포함하는 배열
['a'].b 세가지 요소를 포함하는 배열
a.b[] 세가지 요소를 포함하는 배열
a.b[].c 문자열 one, two를 포함하는 배열
a.b[].d 문자열 three
a['e.dot'] 문자열 four
['a']['e.dot'] The string four

URI 변경

modifyUris를 사용하면 request 및 response에 있는 URI를 변경할 수 있다.

mockMvc.perform(get("/api/v1/data"))
  .andDo(print())
  .andExpect(status().isOk())
  .andDo(document("signup"),
    preprocessRequest(modifyUris()
      .scheme("https")
      .host("www.lannstark.com")
      .removePort(), prettyPrint()),
    preprocessResponse(prettyPrint()));

Host가 바뀌고, port가 더이상 보이지 않는다. 하지만 https가 적용되지는 않고, 실제 SSL 설정이 끝나야 적용될 수도 있을 듯 하다

보다 자세한 내용은 여기를 참고하면 좋다!

저작자표시 비영리 (새창열림)

'Spring > RestDocs' 카테고리의 다른 글

Spring 2.7 버전대 RestDocs Cookie 문서화 방법(RestDocs 2.0.7)  (0) 2023.03.21
RestDocs - Custom Error Code Enum 문서화  (0) 2023.03.20
Restdocs header 설정  (0) 2023.01.31
REST Docs에 DTO Bean Validation 담기  (0) 2023.01.29
Restdocs pretty print, header 제거  (0) 2023.01.29
    'Spring/RestDocs' 카테고리의 다른 글
    • Spring 2.7 버전대 RestDocs Cookie 문서화 방법(RestDocs 2.0.7)
    • RestDocs - Custom Error Code Enum 문서화
    • Restdocs header 설정
    • REST Docs에 DTO Bean Validation 담기
    ysk(0soo)
    ysk(0soo)
    백엔드 개발을 좋아합니다. java kotlin spring, infra 에 관심이 많습니다. email : kim206gh@naver.com github : https://github.com/devysk

    티스토리툴바