在具有相同名称的不同名称空间中摇摇欲坠的不同类不起作用 [英] Swagger different classes in different namespaces with same name don't work

查看:145
本文介绍了在具有相同名称的不同名称空间中摇摇欲坠的不同类不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有(多个)两个Api POST端点.每个人都需要一个json作为参数.但是,当我在两个端点参数类中使用相同的类名 有效载荷时, Swagger不起作用.当我更改其中之一时从 Payload Payload1 . 当然,我在包装器类中设置了正确的名称空间,以便它找到有效载荷.但我希望每次都使用相同的名称有效负载". 如何使用相同的类名有效载荷? 在这两种情况下,我都可以保留JSON名称"Payload",并为属性设置不同的名称("Payload1","Payload2").有用.但是也可以使用相同的属性名称.

I got (more than) two Api POST endpoints. Each one needs a json as parameter. But when I use the same class name Payload in two endpoint argument classes, Swagger does not work. When I change one of it e.g. from Payload to Payload1 than it works. Of course I set the right namespaces into the wrapper classes so it finds it Payload. But I would love to use the same name "Payload" each time. How can I use the same class name Payload? I can keep the json name "Payload" at both cases and just set different names for the property ("Payload1", "Payload2"). It works. But would be nice to have same property names too.,

端点A

[HttpPost()]

[HttpPost()]

公共异步任务PostPerson([FromBody] JsonWrapperA jsonWrapperA)

public async Task PostPerson([FromBody]JsonWrapperA jsonWrapperA)

namespace myProject.A
{
    public class JsonWrapperA
    {
        [JsonProperty("name", Required = Required.AllowNull)]
        public string Name { get; set; }

        [JsonProperty("payload", Required = Required.AllowNull)]
        public Payload Payload { get; set; }
    }

    public class Payload
    {
        [JsonProperty("value", Required = Required.AllowNull)]
        public double Value { get; set; }
    }
}

端点B

[HttpPost()]

[HttpPost()]

公共异步任务PostCompagn([FromBody] JsonWrapperB jsonWrapperB)

public async Task PostCompagn([FromBody]JsonWrapperB jsonWrapperB)

namespace myProject.B
{
    public class JsonWrapperB
    {
        [JsonProperty("compagny", Required = Required.AllowNull)]
        public string Compagny { get; set; }

        [JsonProperty("payload", Required = Required.AllowNull)]
        public Payload Payload { get; set; }
    }

    public class Payload
    {
        [JsonProperty("age", Required = Required.AllowNull)]
        public double Age{ get; set; }
    }
}

推荐答案

默认情况下,swagger会尝试为API端点的返回类型或参数类型的对象构建其架构ID,并在文档中的模型"部分.它将基于对象的类名称构建这些架构ID.

By default swagger will attempt to build its Schema Ids for objects that are return types or parameter types for your APIs endpoints, and it will display these objects in the "Models" section of the documentation. It will build these schema Ids based on the class names of the objects.

当您尝试将两个或更多个类命名为相同时,即使它们位于不同的命名空间中,也会出现冲突的schemaIds错误:

When you try to have two or more classes named the same, even though they are in different namespaces, then you will get the conflicting schemaIds error:

InvalidOperationException:冲突的schemaIds:为NamespaceOne.MyClass和NamespaceTwo.MyClass类型检测到相同的schemaIds.请参阅配置设置-"CustomSchemaIds"以获取解决方法

InvalidOperationException: Conflicting schemaIds: Identical schemaIds detected for types NamespaceOne.MyClass and NamespaceTwo.MyClass. See config settings - "CustomSchemaIds" for a workaround

这意味着需要配置Swagger才能更改其生成SchemaId的方式.您可以简单地告诉swagger使用对象的完全限定名称,该名称将在schemaId中包含名称空间.您可以在ConfigureServices方法的Startup.cs文件中执行以下操作:

This means Swagger needs to be configured to change the way it generates its SchemaIds. You can simply tell swagger to use an objects fully qualified name which will include namespaces in the schemaIds. You can do this in your Startup.cs file in the ConfigureServices method like this:

//add using statement for Swagger in Startup.cs
using Swashbuckle.AspNetCore.Swagger;

...

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerGen(config =>
    {
        //some swagger configuration code.

        //use fully qualified object names
        config.CustomSchemaIds(x => x.FullName);
    }
}

这篇关于在具有相同名称的不同名称空间中摇摇欲坠的不同类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆