본문 바로가기
개발/NestJS

[NestJS] Swagger에서 return type을 여러 개 설정하는 법

by coking 2022. 12. 3.

API를 정의하고 Swagger 문서를 작성하다 보면 API return type이 두 가지 이상일 경우
Swagger에서도 이를 표현해야 할 때가 있다. 그런 분들을 위해서 작성한 방법을 공유하려 한다.

 

 

이런 식으로 두 가지 타입이 return 값으로 정의되게 하려면 어떻게 해야 할까 바로 코드로 들어가 보자 

 

바로 정답을 보여줘서 재미없을 수도 있지만 찾는 게 어렵지 알고 나면 쉬운 거라 머라고 할 말이 없다... 

 

구현 후 schema 코드를 타고 들어가 보면 oneOf 말고도 allOf, anyOf 등 다른 타입들도 존재하니 상황에 맞게 사용하기 바란다.

 

 

참고 링크 

 

https://docs.nestjs.com/openapi/operations

 

Documentation | NestJS - A progressive Node.js framework

Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reac

docs.nestjs.com

 

댓글