【问题标题】:Why does openapi-generator create InlineObjects?为什么 openapi-generator 会创建 InlineObjects?
【发布时间】:2021-03-21 23:48:39
【问题描述】:

我正在使用

https://github.com/OpenAPITools/openapi-generator

为我的 API 创建一个客户端。它大部分工作正常,但是生成器创建了许多 InlineObject 类型,这些类型封装了包含任何复杂类型的参数,例如 DictionaryIFormFileStream

例如,

public async Task<ApiResponse<FileUploadResponseAPIModel>> MyApiClientMethod(InlineObject11 inlineObject11 = default(InlineObject11))
    {
    }

其中 InlineObject11 被定义为

public partial class InlineObject11 : IEquatable<InlineObject11>, IValidatableObject
{
    
    [JsonConstructorAttribute]
    protected InlineObject11() { }
    
    public InlineObject11(Stream rebalanceTradeFile = default(Stream))
    {
        // to ensure "rebalanceTradeFile" is required (not null)
        this.RebalanceTradeFile = rebalanceTradeFile ?? throw new ArgumentNullException("rebalanceTradeFile is a required property for InlineObject11 and cannot be null");
    }

    [DataMember(Name = "RebalanceTradeFile", IsRequired = true, EmitDefaultValue = false)]
    public System.IO.Stream RebalanceTradeFile { get; set; }

这有什么意义?为什么不生成客户端以获取Stream rebalanceTradeFile 而不是将其包装到InlineObject 中?我该如何解决?这破坏了我使用旧版本生成客户端的一堆工具。

【问题讨论】:

  • 当这是谷歌上的第一个结果时,我很兴奋,但我才意识到这是我的问题,但仍然没有答案。

标签: openapi openapi-generator


【解决方案1】:

内联对象是从内联模式创建的,例如https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/inline_model_resolver.yaml#L15-L24 是一个架构,它为 MIME 类型为 application/json 的有效负载内联定义了 2 个属性。

为避免 OpenAPI Generator 自动为内联模式生成模型,可以单独定义模型 (example) 并改用 $ref

        application/json:
          schema:
            $ref: '#/components/schemas/Pet' 

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2022-07-23
    • 2021-11-09
    • 2019-04-21
    • 1970-01-01
    • 2022-09-30
    • 2022-07-15
    相关资源
    最近更新 更多