【ruby on rails】rswag使用

命令

  • 生成文档
ruby 复制代码
rake rswag

一些代码

ruby 复制代码
# frozen_string_literal: true

require 'rails_helper'

RSpec.configure do |config|

  config.swagger_root = Rails.root.join('swagger').to_s

  # the root example_group in your specs, e.g. describe '...', swagger_doc: 'v2/swagger.json'
  config.swagger_docs = {
  	#  'v1/swagger.yaml' => {
    'v1/swagger.json' => {   
      openapi: '3.0.1',
      info: {
        title: 'API V1',
        version: 'v1'
      },
      paths: {},
   	  servers: [
        {
          url: 'http://{defaultHost}',
          description: '本地开发',
          variables: {
            defaultHost: {
              default: '127.0.0.1:3000'
            }
          }
        },
        {
          url: 'https://{defaultHost}',
          description: '测试服',
          variables: {
            defaultHost: {
              default: 'api.xxx.com' # 默认https
            }
          }
        }
      ],
      components: {
        securitySchemes: {
          # Bearer: {
          #   description: 'token necessary to use API calls',
          #   type: :apiKey,
          #   name: 'X-Admin-Authorization',
          #   in: :header
          # }
          bearer_auth: {
            type: :http,
            scheme: :bearer,
            bearerFormat: 'JWT'
          }
        },

        schemas: {
          marks: {
            type: :array,
            items: {
              type: :object,
              properties: {
                date: { type: :string },
                score: { type: :number },
                worksheet: { type: :string }
              }
            }
          },
          importances: {
            type: :array,
            items: {
              type: :object,
              properties: {
                school_id: { type: :integer },
                importance: { type: :integer }
              }
            }
          },
          improvement_priorities: {
            type: :array,
            items: {
              type: :object,
              properties: {
                priority: { type: :integer },
                school_id: { type: :integer }
              }
            }
          },
          subtopics: {
            type: :array,
            items: {
              type: :object,
              properties: {
                id: { type: :integer },
                name: { type: :string },
                marks: { '$ref' => '#/components/schemas/marks' },
                topic_id: { type: :integer },
                difficulty: { type: :integer },
                consistency: { type: :number },
                importances: { '$ref' => '#/components/schemas/importances' },
                proficiency: { type: :number },
                improvement_priorities: { '$ref' => '#/components/schemas/improvement_priorities' }
              }
            }
          },
          topics: {
            type: :array,
            items: {
              type: :object,
              properties: {
                id: { type: :integer },
                name: { type: :string },
                strand_id: { type: :integer },
                subtopics: { '$ref' => '#/components/schemas/subtopics' },
                difficulty: { type: :integer },
                consistency: { type: :number },
                importances: { '$ref' => '#/components/schemas/importances' },
                proficiency: { type: :number },
                improvement_priorities: { '$ref' => '#/components/schemas/improvement_priorities' }
              }
            }
          },
          strands: {
            type: :array,
            items: {
              type: :object,
              properties: {
                id: { type: :integer },
                name: { type: :string },
                topics: { '$ref' => '#/components/schemas/topics' }
              }
            }
          },
          latest_assessment: {
            type: :object,
            properties: {
              name: { type: :string },
              marks: { type: :integer },
              date_done: { type: :string },
              total_marks: { type: :integer }
            }
          },
          subjects: {
            type: :array,
            items: {
              type: :object,
              properties: {
                id: { type: :integer },
                name: { type: :string },
                target: { type: :integer },
                strands: { '$ref' => '#/components/schemas/strands' },
                latest_assessment: { '$ref' => '#/components/schemas/latest_assessment' },
                overall_proficiency: { type: :integer }
              }
            }
          },
          levels: {
            type: :array,
            items: {
              type: :object,
              properties: {
                id: { type: :integer },
                name: { type: :string },
                level: { type: :integer },
                subjects: { '$ref' => '#/components/schemas/subjects' }
              }
            }
          },
          student: {
            type: :object,
            properties: {
              id: { type: :integer },
              student_id: { type: :string },
              first_name: { type: :string },
              last_name: { type: :string },
              registration_date: { type: :string },
              school_id: { type: :integer },
              photo: {
                type: :object,
                properties: {
                  url: { type: :string }
                }
              },
              overview_data: {
                type: :object,
                properties: {
                  levels: { '$ref' => '#/components/schemas/levels' }
                }
              }
            }
          },
          detailed_subtopic: {
            type: :object,
            properties: {
              code: { type: :integer, default: 200 },
              message: { type: :string, default: 'ok' },
              data: { '$ref' => '#/components/schemas/partial_subtopic' },
            }
          },
          detailed_topic: {
            type: :object,
            properties: {
              code: { type: :integer, default: 200 },
              message: { type: :string, default: 'ok' },
              data: {
                type: :array,
                items: {
                  type: :object,
                  properties: {
                    date: { type: :string },
                    proficiency: { type: :number },
                    contributions: { '$ref' => '#/components/schemas/partial_subtopic' },
                    subtopic_id: { type: :integer },
                    subtopic_name: { type: :string }
                  }
                }
              }
            }
          },
          partial_subtopic: {
            type: :array,
            items: {
              type: :object,
              properties: {
                date: { type: :string },
                proficiency: { type: :number },
                contributions: {
                  type: :array,
                  items: {
                    type: :object,
                    properties: {
                      score: { type: :number },
                      worksheet: { type: :string },
                      date: { type: :string }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  # Defaults to json. Accepts ':json' and ':yaml'.
  config.swagger_format = :json
end
ruby 复制代码
 path '/api/v1/students/{id}' do
    # You'll want to customize the parameter types...
   parameter name: :'X-Admin-Authorization', in: :header, type: :string, required: false, default: 'sdfsd'
   parameter name: :page, in: :query, type: :integer, required: false
   parameter name: 'id', in: :path, type: :integer, description: 'id'
   
   get('show student') do
      tags 'students'
      security [ bearer_auth: []]  # security [ Bearer: []]
      produces 'application/json'
      
 	  response '401', 'need login to create' do
        let(:id) { 100 }
        run_test!
      end
      
      response(200, 'successful') do
        schema type: :object,
               properties: {
                code: { type: :integer, default: 200 },
                message: { type: :string, default: 'ok' },
                data: { '$ref' => '#/components/schemas/student' }
               }
        # let(:id) { '123' }
        # run_test!  # 运行测试
        xit  # 跳过测试用例
      end
    end
  end
end
ruby 复制代码
  path '/api/v1/pets' do

    post 'Creates a pet' do
      tags 'Pets'
      consumes 'application/json', 'application/xml'
      parameter name: :pet, in: :body, schema: {
        type: :object,
        properties: {
          name: { type: :string },
          photo_url: { type: :string },
          status: { type: :string }
        },
        required: [ 'name', 'status' ]
      }

      response '201', 'pet created' do
        let(:pet) { { name: 'Dodo', status: 'available' } }
        run_test!
      end

      response '422', 'invalid request' do
        let(:pet) { { name: 'foo' } }
        run_test!
      end
    end
  end
相关推荐
Adolf_199344 分钟前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask
叫我:松哥1 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
海里真的有鱼1 小时前
Spring Boot 项目中整合 RabbitMQ,使用死信队列(Dead Letter Exchange, DLX)实现延迟队列功能
开发语言·后端·rabbitmq
工业甲酰苯胺1 小时前
Spring Boot 整合 MyBatis 的详细步骤(两种方式)
spring boot·后端·mybatis
新知图书2 小时前
Rust编程的作用域与所有权
开发语言·后端·rust
wn5313 小时前
【Go - 类型断言】
服务器·开发语言·后端·golang
希冀1233 小时前
【操作系统】1.2操作系统的发展与分类
后端
GoppViper4 小时前
golang学习笔记29——golang 中如何将 GitHub 最新提交的版本设置为 v1.0.0
笔记·git·后端·学习·golang·github·源代码管理
爱上语文5 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
serve the people5 小时前
springboot 单独新建一个文件实时写数据,当文件大于100M时按照日期时间做文件名进行归档
java·spring boot·后端