- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在用 grape 编写一个 API 服务器,我选择使用 grape-entity 因为它能够自动生成 swagger 的文档。但是现在我在按要求设置参数时遇到了问题。因为葡萄不验证参数是否存在。看起来 grape 忽略了实体参数的 required: true
。
app.rb
module Smart
module Version1
class App < BaseApi
resource :app do
# POST /app
desc 'Creates a new app' do
detail 'It is used to re gister a new app on the server and get the app_id'
params Entities::OSEntity.documentation
success Entities::AppEntity
failure [[401, 'Unauthorized', Entities::ErrorEntity]]
named 'My named route'
end
post do
app = ::App.create params
present app, with: Entities::AppEntity
end
end
end
end
end
os_entity.rb
module Smart
module Entities
class OSEntity < Grape::Entity
expose :os, documentation: { type: String, desc: 'Operative system name', values: App::OS_LIST, required: true }
end
end
end
app_entity.rb
module Smart
module Entities
class AppEntity < OSEntity
expose :id, documentation: { type: 'integer', desc: 'Id of the created app', required: true }
expose :customer_id, documentation: { type: 'integer', desc: 'Id of the customer', required: true }
end
end
end
现在其他一切都很好,但我不知道如何以 DRY 方式使用实体,并让 grape 验证参数的要求。
最佳答案
经过一些工作后,我能够让 grape 工作,因为我认为它应该工作。因为我不想重复验证和文档的代码。您只需要将其添加到初始化程序中(当然,如果您在 rails 中)。我还能够支持嵌套关联。如您所见,API 代码看起来很简单,swagger 看起来很完美。以下是 API 和所有需要的实体:
app/api/smart/entities/characteristics_params_entity.rb
module Smart
module Entities
class CharacteristicsParamsEntity < Grape::Entity
root :characteristics, :characteristic
expose :id, documentation: { type: Integer, desc: 'Id of the characteristic' }
end
end
end
app/api/smart/entities/characterisitcs_entity.rb
module Smart
module Entities
class CharacteristicsEntity < CharacteristicsParamsEntity
expose :id, documentation: { type: Integer, desc: 'Id of the characteristic' }
expose :name, documentation: { type: String, desc: 'Name of the characteristic' }
expose :description, documentation: { type: String, desc: 'Description of the characteristic' }
expose :characteristic_type, documentation: { type: String, desc: 'Type of the characteristic' }
expose :updated_at, documentation: { type: Date, desc: 'Last updated time of the characteristic' }
end
end
end
app/api/smart/entities/apps_params_entity.rb
module Smart
module Entities
class AppsParamsEntity < Grape::Entity
expose :os, documentation: { type: String, desc: 'Operative system name', values: App::OS_LIST, required: true }
expose :characteristic_ids, using: CharacteristicsParamsEntity, documentation: { type: CharacteristicsParamsEntity, desc: 'List of characteristic_id that the customer has', is_array: true }
end
end
end
app/api/smart/entities/apps_entity.rb
module Smart
module Entities
class AppsEntity < AppsParamsEntity
unexpose :characteristic_ids
expose :id, documentation: { type: 'integer', desc: 'Id of the created app', required: true }
expose :customer_id, documentation: { type: 'integer', desc: 'Id of the customer', required: true }
expose :characteristics, using: CharacteristicsEntity, documentation: { is_array: true, desc: 'List of characteristics that the customer has' }
end
end
end
app/api/smart/version1/apps.rb
module Smart
module Version1
class Apps < Version1::BaseAPI
resource :apps do
# POST /apps
desc 'Creates a new app' do
detail 'It is used to register a new app on the server and get the app_id'
params Entities::AppsParamsEntity.documentation
success Entities::AppsEntity
failure [[400, 'Bad Request', Entities::ErrorEntity]]
named 'create app'
end
post do
app = ::App.create! params
present app, with: Entities::AppsEntity
end
end
end
end
end
这是神奇地让它工作的代码:
config/initializers/grape_extensions.rb
class Evaluator
def initialize(instance)
@instance = instance
end
def params parameters
evaluator = self
@instance.normal_params do
evaluator.list_parameters(parameters, self)
end
end
def method_missing(name, *args, &block)
end
def list_parameters(parameters, grape)
evaluator = self
parameters.each do |name, description|
description_filtered = description.reject { |k| [:required, :is_array].include?(k) }
if description.present? && description[:required]
if description[:type] < Grape::Entity
grape.requires name, description_filtered.merge(type: Array) do
evaluator.list_parameters description[:type].documentation, self
end
else
grape.requires name, description_filtered
end
else
if description[:type] < Grape::Entity
grape.optional name, description_filtered.merge(type: Array) do
evaluator.list_parameters description[:type].documentation, self
end
else
grape.optional name, description_filtered
end
end
end
end
end
module GrapeExtension
def desc name, options = {}, &block
Evaluator.new(self).instance_eval &block if block
super name, options do
def params *args
end
instance_eval &block if block
end
end
end
class Grape::API
class << self
prepend GrapeExtension
end
end
这是示例的结果:
关于ruby-on-rails - 葡萄 : required params with grape-entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29517362/
我经常看到这个,但我一直不明白。为什么这么多函数在函数定义中有这些嵌套的括号和奇怪的逗号? 有点像 function name(param, param param) 对我来说有意义,或者 funct
为什么参数输入有两种不同的格式?除了个人品味之外,还有其他好处吗? 例如,我正在使用它,因为那是我第一次阅读有关Powershell的文档时所看到的 function MyAwesomeFunctio
当我转到角色 Controller 时,显示操作,所有正常的 params[:id] 都是根据 REST 应该如何。 在显示 View 中,我渲染了部分。在该部分中,我有一个指向 vote_socio
我正在尝试在我的 Express 应用程序中使用 Sequelize 获取数据,并使用 MSSQL 获取数据库。这是我的代码: getInstitution: function (req, res)
从 ASP.NET 中的代码隐藏访问表单或查询字符串值时,使用的优缺点是什么,例如: // short way string p = Request["param"]; 代替: // long way
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is it called when you use object… as a parameter? 我在
在代码示例中,我看到了以下代码: SomeClass.Columns(columns => { columns.Add() .Text("Product")
我是 PySpark 的初学者。我在 PySpark 中使用 FPgrowth 计算关联。我按照以下步骤操作。 数据示例 from pyspark.sql.session import SparkSe
我想记录一个类,我在 PyCharm 中看到当自动添加文档字符串时它会添加 :param param_name1: :param param_name2: 不过,我看到有人用 @param param
我正在为一个网站设计 Android 客户端。所以我必须实现很多不同的网络操作。例如用户授权或在服务器上搜索。为此,我必须向 REST Web 服务器发送带有一些参数和 URL 的 JSON 请求。因
我对 Jersey 和 Grizzly 有疑问。这个问题可能非常基本,但我正在努力解决它。我的想法是,我正在创建一个需要存储书籍的练习应用程序。一切似乎都很好,但并没有按预期进行。这是源代码: @Pa
假设我有一个带有虚方法的接口(interface),但参数之一是: virtual void Delete(ParentClass *parentClass) = 0; 如果我稍后在子类中实现它 vo
需要一些 Python 语法方面的帮助 伪代码 if param is not None in params, param replace('xx', 'x') 我走到这一步了: param.repl
我刚开始使用 JSDocs,找不到这个问题的答案。 假设我想写这个简单的函数: function hasQ(array, item) {return array.includes(item);} 对于
我对doInBackground(Params...params)中的参数语法不熟悉 这种东西叫什么,我该如何使用它? 最佳答案 正如 devA 和 VVV 所说,这就是所谓的“可变参数”。实际上,以
我很难在两个 perl 模块之间做出选择——Params::Validate 和 Params::Check。 我正在编写一个应该易于分发的 Mojolicious 应用程序。 一般来说,我的意思是在
这个问题已经有答案了: What does [param: NotNull] mean in C#? (2 个回答) 已关闭 7 年前。 我发现了以下属性声明。 [param: Marsha
我正在阅读有关 $params 的 CakePHP 手册,并且想知道每个参数的适当用法是什么?我知道返回的数组略有不同(“url”实际上有一个名为“url”的数组键,它返回 Controller /操
我见过处理 *param1 和 ¶m2 的 C 函数调用 func1(*param1); func2(¶m2); 我知道 * 和 & 必须与指针相关。那么这两种不同的方式有
特别是,为什么对 *param 这样的参数的更改不会传播回函数的调用者,但对 **param 这样的参数的更改却会传播回来? 最佳答案 在这两种情况下,更改都会被传播,但这取决于调用函数。 请参阅下面
我是一名优秀的程序员,十分优秀!