Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

asp.net - Swagger different classes in different namespaces with same name don't work

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.,

enter image description here

Endpoint A

[HttpPost()]

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; }
    }
}

Endpoint B

[HttpPost()]

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; }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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: Conflicting schemaIds: Identical schemaIds detected for types NamespaceOne.MyClass and NamespaceTwo.MyClass. See config settings - "CustomSchemaIds" for a workaround

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...