스웨거(Swagger) 3.0.0 with yaml

API 문서를 스웨거로 관리하는 작업을 하며 yaml파일 작성법에 대해 공식홈페이지를 통해 정리해보았다. 간단한 구조이므로 몇번 직접 쓰다보면 감이 잡힐 것이다. 정리한 것 외에도 다양하니 필요한 부분은 검색.

스웨거(Swagger) API들이 가지고 있는 정보들을 명세, 관리할 수 있는 도구. API를 문서화 하는 툴. 어노테이션을 활용할 수도 있지만 코드가 난잡해지는것을 지양해야 하기 때문에 yaml파일로 작성을 선호한다. 해당글은 yaml파일 기준 작성법이다.

1. Metadata

1.1 version

openapi: 3.0.0

openapi: 3.0.0

1.2 info

info: title: Sample API description: Optional multiline or single-line description in [CommonMark](<http://commonmark.org/help/>) or HTML. version: 0.1.9

info:
  title: Sample API
  description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
  version: 0.1.9
  • title : API명

  • description : API설명

  • optional : version, information, license, terms of service, etc...

2. Servers

  • URL: 서버 URL (Base URL)

  • description : 설명.

  • 다수의 URL 등록 가능.

더 자세한 사항

https://swagger.io/docs/specification/api-host-and-base-path/

3. Paths

  • paths : Server URL을 제외한 나머지 URL

  • HTTP method : ex) get / post / put /delete

  • summary : 요약 (화면에서 드랍다운 박스에 표시되는 부분)

  • responses : 응답

  • requestBody : 리턴

  • Multi-line, Markdown도 지원.

더 자세한 사항

https://swagger.io/docs/specification/paths-and-operations/

4. Parameters

{userId} : 파라미터

parameters : 해당 파라미터에 대한 정보들 (ex. userId)

  • in : 파라미터의 위치.

  • required : 필수 여부.

  • schema : 해당 schema 정보.

  • $ref : 중복되는 스키마 정보를 참조위치를 통해 삽입 가능. (ex. $ref: '#/components/schemas/User') info section에서는 사용 할 수 없으며, paths section에서 바로 사용은 불가. 그러나 paths section에 하위에는 사용 가능

더 자세한 사항

https://swagger.io/docs/specification/describing-parameters/

5. Request Body

  • schema : 대상이 될 스키마명을 적으면 된다.

  • properties : object의 경우 프로퍼티스 ( array의 경우에는 items)

더 자세한 사항

https://swagger.io/docs/specification/describing-request-body/

6. Responses

더 자세한 사항

https://swagger.io/docs/specification/describing-responses/

7. Input and Output Models

8. Authentication

더 자세한 사항

https://swagger.io/docs/specification/authentication/api-keys/

공식 홈페이지 Example YAML 예제들 출처 Basic Structure

Example YAML 예제들 출처 Swagger Editor

Detail Object OAI/OpenAPI-Specification

출처

Last updated