CoreWCF / CoreWCF/CoreWCF

WebHttp OpenAPI is generating excessive types

Open
#655 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1.8k
Forks
320
PR merge metrics
No merged PRs in 30d

Description

I have been working on cleaning up the samples and creating clients to go with the server examples.
In the case of the webhttp binding, the client uses the Nswag msbuild task to generate a client wrapper based on the swagger document generated.

The problem that I am finding is how its handling inner types.

The server contract includes:

```c#
[DataContract(Name = "ExampleContract", Namespace = "http://example.com")]
internal class ExampleContract
{
[DataMember(Name = "SimpleProperty", Order = 1)]
[OpenApiProperty(Description = "SimpleProperty description.")]
public string SimpleProperty { get; set; }

[DataMember(Name = "ComplexProperty", Order = 2)]
[OpenApiProperty(Description = "ComplexProperty description.")]
public InnerContract ComplexProperty { get; set; }

[DataMember(Name = "SimpleCollection", Order = 3)]
[OpenApiProperty(Description = "SimpleCollection description.")]
public List SimpleCollection { get; set; }

[DataMember(Name = "ComplexCollection", Order = 4)]
[OpenApiProperty(Description = "ComplexCollection description.")]
public List ComplexCollection { get; set; }
}

[DataContract(Name = "InnerContract", Namespace = "http://example.com")]
internal class InnerContract
{
[DataMember(Name = "Name", Order = 1)]
[OpenApiProperty(Description = "Name description.")]
public string Name { get; set; }
}
}
```

I get a generated client (yay). but when I then look at the code generated it has:
```c#
public partial class ExampleContract
{
[Newtonsoft.Json.JsonProperty("ComplexProperty", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public ExampleContractInnerContract ComplexProperty { get; set; }
...
/// ComplexCollection description.
[Newtonsoft.Json.JsonProperty("ComplexCollection", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Collections.Generic.ICollection ComplexCollection { get; set; }
}
```

In the original contract, there is one type `InnerContract`, but here its deciding to create multiple types `ExampleContractInnerContract` and `ExampleContractArrayInnerContract`, which should be the same. This bloats the type table, and means that they can't be used across methods. Is there something going wrong in the swagger generation that is causing this?

@JonathanHopeDMRC - can you help?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.