OpenAPI / Swagger-ui:表单中自动生成的 JSON 会忽略参数名称
OpenAPI / Swagger-ui: Auto-generated JSON in form ignores parameter name
编辑:在此处发帖后发现 this post,请在下方回答
我正在使用 ServiceStack 及其 OpenApi 插件。我不确定这是否是 Swagger-ui 问题、ServiceStack 或我的代码中的问题。
我有一个 POST 端点,我希望在其中填充客户 属性:
[Route("/api/customers/", "POST", Summary = "Creates a new customer")]
public class CreateCustomer : IReturn<CreateCustomerResponse>
{
[ApiMember(Description = "The customer data", ParameterType = "body", IsRequired = true)]
public Customer Customer { get; set; }
}
客户 class 有许多属性,例如“名字”等
当我在 swagger-ui 中查看此内容时,我可以看到“示例值”缺少 JSON 对象“客户”应放置在其中的名称“客户”:
如果我然后按“试用”按钮,我可以看到 Swagger-ui 直接发送“客户”对象,而没有指定它应该在“客户”中(我删除了为清楚起见,请使用反斜杠并删除客户 json 的属性):
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"PopulationRegistryNumber": "string",
"Firstname": "string",
"MiddleName": "string",
"Lastname": "string"
}
我的预期是:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '
{ "Customer":
{
"PopulationRegistryNumber": "string",
"Firstname": "string",
"MiddleName": "string",
"Lastname": "string"
}
}
现在,如果我删除 ServiceStack ApiMember
属性,则 Swagger-ui 具有正确的 JSON,但它会在表单中为“客户”添加一个单独的字段, 这是误导,不应该存在,因为它应该是正文的一部分。
这个“客户”字段是一个自大的问题,一个 ServiceStack 的东西还是我遗漏的东西?
There is a thread on ServiceStack forum,其中讨论了这个确切的问题。
xplicit 的最后一个 post 提供了一个解决方案,尽管我不清楚这些属性究竟是如何协同工作的。
部分解决了我的问题的解决方案是:
You can use [ApiMember(ExcludeInSchema=true)] and
[ApiMember(ParameterType=“model”)] to exclude properties you don’t
want to see in Open API definitions. For example
[Route("/workflow/{ProjectId}", "POST")]
[Api(BodyParameter = GenerateBodyParameter.Always, IsRequired = true)]
public class WorkflowPostRequest : IReturn<Workflow>
{
[ApiMember(ParameterType = "path", ExcludeInSchema = true)]
public string ProjectId { get; set; }
[ApiMember(ParameterType = "model")]
public Workflow Workflow { get; set; }
}
will generate this Open API definition:
论坛 post 是 here。
注意:
class、[Api(BodyParameter = GenerateBodyParameter.Always, IsRequired = true)]
上的属性在我的情况下不需要,正确的 JSON 和外观仍然可以大摇大摆地工作。
所以,基本上,您需要做的就是从 ParameterType = "body"
更改为 ParameterType = "model"
另请注意,路径、查询等中的变量必须使用 ExcludeInSchema
手动排除,这很烦人但可行。
编辑:在此处发帖后发现 this post,请在下方回答
我正在使用 ServiceStack 及其 OpenApi 插件。我不确定这是否是 Swagger-ui 问题、ServiceStack 或我的代码中的问题。
我有一个 POST 端点,我希望在其中填充客户 属性:
[Route("/api/customers/", "POST", Summary = "Creates a new customer")]
public class CreateCustomer : IReturn<CreateCustomerResponse>
{
[ApiMember(Description = "The customer data", ParameterType = "body", IsRequired = true)]
public Customer Customer { get; set; }
}
客户 class 有许多属性,例如“名字”等
当我在 swagger-ui 中查看此内容时,我可以看到“示例值”缺少 JSON 对象“客户”应放置在其中的名称“客户”:
如果我然后按“试用”按钮,我可以看到 Swagger-ui 直接发送“客户”对象,而没有指定它应该在“客户”中(我删除了为清楚起见,请使用反斜杠并删除客户 json 的属性):
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"PopulationRegistryNumber": "string",
"Firstname": "string",
"MiddleName": "string",
"Lastname": "string"
}
我的预期是:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '
{ "Customer":
{
"PopulationRegistryNumber": "string",
"Firstname": "string",
"MiddleName": "string",
"Lastname": "string"
}
}
现在,如果我删除 ServiceStack ApiMember
属性,则 Swagger-ui 具有正确的 JSON,但它会在表单中为“客户”添加一个单独的字段, 这是误导,不应该存在,因为它应该是正文的一部分。
这个“客户”字段是一个自大的问题,一个 ServiceStack 的东西还是我遗漏的东西?
There is a thread on ServiceStack forum,其中讨论了这个确切的问题。
xplicit 的最后一个 post 提供了一个解决方案,尽管我不清楚这些属性究竟是如何协同工作的。
部分解决了我的问题的解决方案是:
You can use [ApiMember(ExcludeInSchema=true)] and [ApiMember(ParameterType=“model”)] to exclude properties you don’t want to see in Open API definitions. For example
[Route("/workflow/{ProjectId}", "POST")] [Api(BodyParameter = GenerateBodyParameter.Always, IsRequired = true)] public class WorkflowPostRequest : IReturn<Workflow> { [ApiMember(ParameterType = "path", ExcludeInSchema = true)] public string ProjectId { get; set; } [ApiMember(ParameterType = "model")] public Workflow Workflow { get; set; } }
will generate this Open API definition:
论坛 post 是 here。
注意:
class、[Api(BodyParameter = GenerateBodyParameter.Always, IsRequired = true)]
上的属性在我的情况下不需要,正确的 JSON 和外观仍然可以大摇大摆地工作。
所以,基本上,您需要做的就是从 ParameterType = "body"
更改为 ParameterType = "model"
另请注意,路径、查询等中的变量必须使用 ExcludeInSchema
手动排除,这很烦人但可行。