Swagger 사용해보기

Minkyeong Kim·2021년 12월 8일
0

[boostcourse] Web-Backend

목록 보기
54/55

Swagger

개념

  • Web API 문서화를 위한 도구
  • OAS(Open API Specification), API들이 가지는 명세(Spec)을 관리하기 위한 프로젝트
  • Web API가 수정되면 자동으로 문서를 갱신해줌

기능

1) API Design
2) API Development
3) API Documentation
4) API Testing
5) API Mocking and Virtualization
6) API Governance
7) API Monitoring
8) OpenAPI & Swagger
Web API를 만드는 사람과 Web API를 사용하는 사람 간에 미리 명세를 정의하고 공유할 수 있도록 함 -> 개발 용이

간단 사용법

  • github연동해서 회원가입

  • organization 생성 후

  • Create New API

  • 소스코드 수정 (그냥 복붙할 것)

swagger: '2.0'
info:
  description: This is a simple API
  version: 1.0.0
  title: Simple Inventory API
  # put the contact info for your development or API team
  contact:
    email: you@your-company.com

  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html

# tags are used for organizing operations
tags:
- name: admins
  description: Secured Admin-only calls
- name: developers
  description: Operations available to regular developers

paths:
  /plus:
    post:
      tags:
      - developers
      summary: plus value
      operationId: plus
      parameters:
      - in : query
        name: value1
        type: integer
      - in : query
        name: value2
        type: integer
      responses:
        200:
          description: plus result
          schema:
            $ref: '#/definitions/CalResult'
        400:
          description: bad input parameter

  /inventory:
    get:
      tags:
      - developers
      summary: searches inventory
      operationId: searchInventory
      description: |
        By passing in the appropriate options, you can search for
        available inventory in the system
      produces:
      - application/json
      parameters:
      - in: query
        name: searchString
        description: pass an optional search string for looking up inventory
        required: false
        type: string
      - in: query
        name: skip
        description: number of records to skip for pagination
        type: integer
        format: int32
        minimum: 0
      - in: query
        name: limit
        description: maximum number of records to return
        type: integer
        format: int32
        minimum: 0
        maximum: 50
      responses:
        200:
          description: search results matching criteria
          schema:
            type: array
            items:
              $ref: '#/definitions/InventoryItem'
        400:
          description: bad input parameter
    post:
      tags:
      - admins
      summary: adds an inventory item
      operationId: addInventory
      description: Adds an item to the system
      consumes:
      - application/json
      produces:
      - application/json
      parameters:
      - in: body
        name: inventoryItem
        description: Inventory item to add
        schema:
          $ref: '#/definitions/InventoryItem'
      responses:
        201:
          description: item created
        400:
          description: invalid input, object invalid
        409:
          description: an existing item already exists
definitions:
  CalResult:
    type: object
    properties:
      value1:
        type: integer
        example: 5
      value2:
        type: integer
        example: 10
      operation:
        type: string
        example: +
      result:
        type: integer
        example: 15
  InventoryItem:
    type: object
    required:
    - id
    - name
    - manufacturer
    - releaseDate
    properties:
      id:
        type: string
        format: uuid
        example: d290f1ee-6c54-4b01-90e6-d701748f0851
      name:
        type: string
        example: Widget Adapter
      releaseDate:
        type: string
        format: date-time
        example: 2016-08-29T09:12:33.001Z
      manufacturer:
        $ref: '#/definitions/Manufacturer'
  Manufacturer:
    required:
    - name
    properties:
      name:
        type: string
        example: ACME Corporation
      homePage:
        type: string
        format: url
        example:  https://www.acme-corp.com
      phone:
        type: string
        example: 408-867-5309
# Added by API Auto Mocking Plugin
host: virtserver.swaggerhub.com
basePath: /s3814/calculator/1.0.0
schemes:
 - https

  • Try it out 버튼 눌러 실행해보면 결과가 나옴

  • 아직 함수와 매핑되지 않았으므로 입력값이 10,15여도 5, 10이 입력되었다는 결과가 도출됨

0개의 댓글