diff --git a/README.md b/README.md index 6904fa7e22..11f8d75575 100644 --- a/README.md +++ b/README.md @@ -1156,7 +1156,7 @@ services.AddSwaggerGen(c => }); ``` -_NOTE: If you're using the [DotSwashbuckle Annotations library](#swashbuckleaspnetcoreannotations), it contains a custom selector that's based on the presence of `SwaggerSubType` attributes on base class definitions. This way, you can use simple attributes to explicitly list the inheritance and/or polymorphism relationships you want to expose. To enable this behavior, check out the [Annotations docs](#list-known-subtypes-for-inheritance-and-polymorphism)._ +_NOTE: If you're using the [DotSwashbuckle Annotations library](#swashbuckleaspnetcoreannotations), it contains a custom selector that's based on the presence of `JsonDerivedType` attributes on base class definitions. This way, you can use simple attributes to explicitly list the inheritance and/or polymorphism relationships you want to expose. To enable this behavior, check out the [Annotations docs](#list-known-subtypes-for-inheritance-and-polymorphism)._ #### Describing Discriminators #### @@ -1222,7 +1222,7 @@ services.AddSwaggerGen(c => }); ``` -_NOTE: If you're using the [DotSwashbuckle Annotations library](#swashbuckleaspnetcoreannotations), it contains custom selector functions that are based on the presence of `SwaggerDiscriminator` and `SwaggerSubType` attributes on base class definitions. This way, you can use simple attributes to explicitly provide discriminator metadata. To enable this behavior, check out the [Annotations docs](#enrich-polymorphic-base-classes-with-discriminator-metadata)._ +_NOTE: If you're using the [DotSwashbuckle Annotations library](#swashbuckleaspnetcoreannotations), it contains custom selector functions that are based on the presence of `JsonPolymorphic` and `JsonDerivedType` attributes on base class definitions. This way, you can use simple attributes to explicitly provide discriminator metadata. To enable this behavior, check out the [Annotations docs](#enrich-polymorphic-base-classes-with-discriminator-metadata)._ ## DotSwashbuckle.AspNetCore.SwaggerUI ## @@ -1505,8 +1505,8 @@ services.AddSwaggerGen(c => }); // Shape.cs -[SwaggerSubType(typeof(Rectangle))] -[SwaggerSubType(typeof(Circle))] +[JsonDerivedType(typeof(Rectangle))] +[JsonDerivedType(typeof(Circle))] public abstract class Shape { } @@ -1514,7 +1514,7 @@ public abstract class Shape ### Enrich Polymorphic Base Classes with Discriminator Metadata ### -If you're using annotations to _explicitly_ indicate the "known" subtypes for a polymorphic base type, you can combine the `SwaggerDiscriminatorAttribute` with the `SwaggerSubTypeAttribute` to provide additional metadata about the "discriminator" property, which will then be incorporated into the generated schema definition: +If you're using annotations to _explicitly_ indicate the "known" subtypes for a polymorphic base type, you can combine the `JsonPolymorphicAttribute` with the `JsonDerivedTypeAttribute` to provide additional metadata about the "discriminator" property, which will then be incorporated into the generated schema definition: ```csharp @@ -1525,12 +1525,21 @@ services.AddSwaggerGen(c => }); // Shape.cs -[SwaggerDiscriminator("shapeType")] -[SwaggerSubType(typeof(Rectangle), DiscriminatorValue = "rectangle")] -[SwaggerSubType(typeof(Circle), DiscriminatorValue = "circle")] +[JsonPolymorphic(TypeDiscriminatorPropertyName = "shapeType")] +[JsonDerivedType(typeof(Rectangle), "rectangle")] +[JsonDerivedType(typeof(Circle), "circle")] public abstract class Shape { - public ShapeType { get; set; } + // Avoid using a JsonPolymorphicAttribute.TypeDiscriminatorPropertyName + // that conflicts with a property in your type hierarchy. + // Related issue: https://github.com/dotnet/runtime/issues/72170 +} + +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum AnimalType +{ + Circle, + Rectangle } ``` diff --git a/src/DotSwashbuckle.AspNetCore.Annotations/AnnotationsSwaggerGenOptionsExtensions.cs b/src/DotSwashbuckle.AspNetCore.Annotations/AnnotationsSwaggerGenOptionsExtensions.cs index 086ee87db6..b3707ba8e3 100644 --- a/src/DotSwashbuckle.AspNetCore.Annotations/AnnotationsSwaggerGenOptionsExtensions.cs +++ b/src/DotSwashbuckle.AspNetCore.Annotations/AnnotationsSwaggerGenOptionsExtensions.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.Json.Serialization; using DotSwashbuckle.AspNetCore.SwaggerGen; using DotSwashbuckle.AspNetCore.Annotations; +using System.Reflection; namespace Microsoft.Extensions.DependencyInjection { @@ -56,21 +58,13 @@ public static void EnableAnnotations(this SwaggerGenOptions options) private static IEnumerable AnnotationsSubTypesSelector(Type type) { - var subTypeAttributes = type.GetCustomAttributes(false) - .OfType(); + var jsonDerivedTypeAttributes = type.GetCustomAttributes(false) + .OfType() + .ToArray(); - if (subTypeAttributes.Any()) + if (jsonDerivedTypeAttributes.Any()) { - return subTypeAttributes.Select(attr => attr.SubType); - } - - var obsoleteAttribute = type.GetCustomAttributes(false) - .OfType() - .FirstOrDefault(); - - if (obsoleteAttribute != null) - { - return obsoleteAttribute.SubTypes; + return jsonDerivedTypeAttributes.Select(attr => attr.DerivedType); } return Enumerable.Empty(); @@ -78,22 +72,13 @@ private static IEnumerable AnnotationsSubTypesSelector(Type type) private static string AnnotationsDiscriminatorNameSelector(Type baseType) { - var discriminatorAttribute = baseType.GetCustomAttributes(false) - .OfType() - .FirstOrDefault(); - - if (discriminatorAttribute != null) - { - return discriminatorAttribute.PropertyName; - } - - var obsoleteAttribute = baseType.GetCustomAttributes(false) - .OfType() + var jsonPolymorphicAttribute = baseType.GetCustomAttributes(false) + .OfType() .FirstOrDefault(); - if (obsoleteAttribute != null) + if (jsonPolymorphicAttribute is not null) { - return obsoleteAttribute.Discriminator; + return jsonPolymorphicAttribute.TypeDiscriminatorPropertyName; } return null; @@ -104,13 +89,13 @@ private static string AnnotationsDiscriminatorValueSelector(Type subType) if (subType.BaseType == null) return null; - var subTypeAttribute = subType.BaseType.GetCustomAttributes(false) - .OfType() - .FirstOrDefault(attr => attr.SubType == subType); + var jsonDerivedTypeAttribute = subType.BaseType.GetCustomAttributes(false) + .OfType() + .FirstOrDefault(attr => attr.DerivedType == subType); - if (subTypeAttribute != null) + if (jsonDerivedTypeAttribute is not null) { - return subTypeAttribute.DiscriminatorValue; + return jsonDerivedTypeAttribute.TypeDiscriminator?.ToString(); } return null; diff --git a/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerDiscriminatorAttribute.cs b/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerDiscriminatorAttribute.cs deleted file mode 100644 index d2c3ecad83..0000000000 --- a/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerDiscriminatorAttribute.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DotSwashbuckle.AspNetCore.Annotations -{ - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = false)] - public class SwaggerDiscriminatorAttribute : Attribute - { - public SwaggerDiscriminatorAttribute(string propertyName) - { - PropertyName = propertyName; - } - - public string PropertyName { get; set; } - } -} \ No newline at end of file diff --git a/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerSubTypeAttribute.cs b/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerSubTypeAttribute.cs deleted file mode 100644 index 8ef9dc24ef..0000000000 --- a/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerSubTypeAttribute.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace DotSwashbuckle.AspNetCore.Annotations -{ - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = true)] - public class SwaggerSubTypeAttribute : Attribute - { - public SwaggerSubTypeAttribute(Type subType) - { - SubType = subType; - } - - public Type SubType { get; set; } - - public string DiscriminatorValue { get; set; } - } -} \ No newline at end of file diff --git a/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerSubTypesAttribute.cs b/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerSubTypesAttribute.cs deleted file mode 100644 index 9134db45be..0000000000 --- a/src/DotSwashbuckle.AspNetCore.Annotations/SwaggerSubTypesAttribute.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace DotSwashbuckle.AspNetCore.Annotations -{ - [Obsolete("Use multiple SwaggerSubType attributes instead")] - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = false)] - public class SwaggerSubTypesAttribute : Attribute - { - public SwaggerSubTypesAttribute(params Type[] subTypes) - { - SubTypes = subTypes; - } - - public IEnumerable SubTypes { get; } - - public string Discriminator { get; set; } - } -} \ No newline at end of file diff --git a/src/DotSwashbuckle.AspNetCore.Annotations/test/WebSites/NswagClientExample/NSwagClient/Client.cs b/src/DotSwashbuckle.AspNetCore.Annotations/test/WebSites/NswagClientExample/NSwagClient/Client.cs new file mode 100644 index 0000000000..5def602bfc --- /dev/null +++ b/src/DotSwashbuckle.AspNetCore.Annotations/test/WebSites/NswagClientExample/NSwagClient/Client.cs @@ -0,0 +1,480 @@ +//---------------------- +// +// Generated using the NSwag toolchain v14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) +// +//---------------------- + +#pragma warning disable 108 // Disable "CS0108 '{derivedDto}.ToJson()' hides inherited member '{dtoBase}.ToJson()'. Use the new keyword if hiding was intended." +#pragma warning disable 114 // Disable "CS0114 '{derivedDto}.RaisePropertyChanged(String)' hides inherited member 'dtoBase.RaisePropertyChanged(String)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword." +#pragma warning disable 472 // Disable "CS0472 The result of the expression is always 'false' since a value of type 'Int32' is never equal to 'null' of type 'Int32?' +#pragma warning disable 612 // Disable "CS0612 '...' is obsolete" +#pragma warning disable 1573 // Disable "CS1573 Parameter '...' has no matching param tag in the XML comment for ... +#pragma warning disable 1591 // Disable "CS1591 Missing XML comment for publicly visible type or member ..." +#pragma warning disable 8073 // Disable "CS8073 The result of the expression is always 'false' since a value of type 'T' is never equal to 'null' of type 'T?'" +#pragma warning disable 3016 // Disable "CS3016 Arrays as attribute arguments is not CLS-compliant" +#pragma warning disable 8603 // Disable "CS8603 Possible null reference return" +#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" +#pragma warning disable 8625 // Disable "CS8625 Cannot convert null literal to non-nullable reference type" + +namespace NSwagClient +{ + using System = global::System; + + [System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + public partial class Client + { + #pragma warning disable 8618 + private string _baseUrl; + #pragma warning restore 8618 + + private System.Net.Http.HttpClient _httpClient; + private static System.Lazy _settings = new System.Lazy(CreateSerializerSettings, true); + + public Client(System.Net.Http.HttpClient httpClient) + { + BaseUrl = "http://example.com"; + _httpClient = httpClient; + } + + private static Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings() + { + var settings = new Newtonsoft.Json.JsonSerializerSettings(); + UpdateJsonSerializerSettings(settings); + return settings; + } + + public string BaseUrl + { + get { return _baseUrl; } + [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))] + set + { + _baseUrl = value; + if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/")) + _baseUrl += '/'; + } + } + + protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } } + + static partial void UpdateJsonSerializerSettings(Newtonsoft.Json.JsonSerializerSettings settings); + + partial void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, string url); + partial void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, System.Text.StringBuilder urlBuilder); + partial void ProcessResponse(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response); + + /// Success + /// A server side error occurred. + public virtual System.Threading.Tasks.Task AnimalsAsync(Animal body) + { + return AnimalsAsync(body, System.Threading.CancellationToken.None); + } + + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// Success + /// A server side error occurred. + public virtual async System.Threading.Tasks.Task AnimalsAsync(Animal body, System.Threading.CancellationToken cancellationToken) + { + if (body == null) + throw new System.ArgumentNullException("body"); + + var client_ = _httpClient; + var disposeClient_ = false; + try + { + using (var request_ = new System.Net.Http.HttpRequestMessage()) + { + var json_ = Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value); + var content_ = new System.Net.Http.StringContent(json_); + content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json"); + request_.Content = content_; + request_.Method = new System.Net.Http.HttpMethod("POST"); + + var urlBuilder_ = new System.Text.StringBuilder(); + if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl); + // Operation Path: "Animals" + urlBuilder_.Append("Animals"); + + PrepareRequest(client_, request_, urlBuilder_); + + var url_ = urlBuilder_.ToString(); + request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute); + + PrepareRequest(client_, request_, url_); + + var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); + var disposeResponse_ = true; + try + { + var headers_ = new System.Collections.Generic.Dictionary>(); + foreach (var item_ in response_.Headers) + headers_[item_.Key] = item_.Value; + if (response_.Content != null && response_.Content.Headers != null) + { + foreach (var item_ in response_.Content.Headers) + headers_[item_.Key] = item_.Value; + } + + ProcessResponse(client_, response_); + + var status_ = (int)response_.StatusCode; + if (status_ == 200) + { + return; + } + else + { + var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); + throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null); + } + } + finally + { + if (disposeResponse_) + response_.Dispose(); + } + } + } + finally + { + if (disposeClient_) + client_.Dispose(); + } + } + + protected struct ObjectResponseResult + { + public ObjectResponseResult(T responseObject, string responseText) + { + this.Object = responseObject; + this.Text = responseText; + } + + public T Object { get; } + + public string Text { get; } + } + + public bool ReadResponseAsString { get; set; } + + protected virtual async System.Threading.Tasks.Task> ReadObjectResponseAsync(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary> headers, System.Threading.CancellationToken cancellationToken) + { + if (response == null || response.Content == null) + { + return new ObjectResponseResult(default(T), string.Empty); + } + + if (ReadResponseAsString) + { + var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + try + { + var typedBody = Newtonsoft.Json.JsonConvert.DeserializeObject(responseText, JsonSerializerSettings); + return new ObjectResponseResult(typedBody, responseText); + } + catch (Newtonsoft.Json.JsonException exception) + { + var message = "Could not deserialize the response body string as " + typeof(T).FullName + "."; + throw new ApiException(message, (int)response.StatusCode, responseText, headers, exception); + } + } + else + { + try + { + using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) + using (var streamReader = new System.IO.StreamReader(responseStream)) + using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(streamReader)) + { + var serializer = Newtonsoft.Json.JsonSerializer.Create(JsonSerializerSettings); + var typedBody = serializer.Deserialize(jsonTextReader); + return new ObjectResponseResult(typedBody, string.Empty); + } + } + catch (Newtonsoft.Json.JsonException exception) + { + var message = "Could not deserialize the response body stream as " + typeof(T).FullName + "."; + throw new ApiException(message, (int)response.StatusCode, string.Empty, headers, exception); + } + } + } + + private string ConvertToString(object value, System.Globalization.CultureInfo cultureInfo) + { + if (value == null) + { + return ""; + } + + if (value is System.Enum) + { + var name = System.Enum.GetName(value.GetType(), value); + if (name != null) + { + var field = System.Reflection.IntrospectionExtensions.GetTypeInfo(value.GetType()).GetDeclaredField(name); + if (field != null) + { + var attribute = System.Reflection.CustomAttributeExtensions.GetCustomAttribute(field, typeof(System.Runtime.Serialization.EnumMemberAttribute)) + as System.Runtime.Serialization.EnumMemberAttribute; + if (attribute != null) + { + return attribute.Value != null ? attribute.Value : name; + } + } + + var converted = System.Convert.ToString(System.Convert.ChangeType(value, System.Enum.GetUnderlyingType(value.GetType()), cultureInfo)); + return converted == null ? string.Empty : converted; + } + } + else if (value is bool) + { + return System.Convert.ToString((bool)value, cultureInfo).ToLowerInvariant(); + } + else if (value is byte[]) + { + return System.Convert.ToBase64String((byte[]) value); + } + else if (value is string[]) + { + return string.Join(",", (string[])value); + } + else if (value.GetType().IsArray) + { + var valueArray = (System.Array)value; + var valueTextArray = new string[valueArray.Length]; + for (var i = 0; i < valueArray.Length; i++) + { + valueTextArray[i] = ConvertToString(valueArray.GetValue(i), cultureInfo); + } + return string.Join(",", valueTextArray); + } + + var result = System.Convert.ToString(value, cultureInfo); + return result == null ? "" : result; + } + } + + [Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), "animalType")] + [JsonInheritanceAttribute("Cat", typeof(Cat))] + [JsonInheritanceAttribute("Dog", typeof(Dog))] + [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + public partial class Animal + { + + } + + [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + public enum AnimalType + { + + [System.Runtime.Serialization.EnumMember(Value = @"Cat")] + Cat = 0, + + [System.Runtime.Serialization.EnumMember(Value = @"Dog")] + Dog = 1, + + } + + [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + public partial class Cat : Animal + { + [Newtonsoft.Json.JsonProperty("catSpecificProperty", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] + public string CatSpecificProperty { get; set; } + + } + + [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + public partial class Dog : Animal + { + [Newtonsoft.Json.JsonProperty("dogSpecificProperty", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] + public string DogSpecificProperty { get; set; } + + } + + [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + [System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple = true)] + internal class JsonInheritanceAttribute : System.Attribute + { + public JsonInheritanceAttribute(string key, System.Type type) + { + Key = key; + Type = type; + } + + public string Key { get; } + + public System.Type Type { get; } + } + + [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + public class JsonInheritanceConverter : Newtonsoft.Json.JsonConverter + { + internal static readonly string DefaultDiscriminatorName = "discriminator"; + + private readonly string _discriminatorName; + + [System.ThreadStatic] + private static bool _isReading; + + [System.ThreadStatic] + private static bool _isWriting; + + public JsonInheritanceConverter() + { + _discriminatorName = DefaultDiscriminatorName; + } + + public JsonInheritanceConverter(string discriminatorName) + { + _discriminatorName = discriminatorName; + } + + public string DiscriminatorName { get { return _discriminatorName; } } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + try + { + _isWriting = true; + + var jObject = Newtonsoft.Json.Linq.JObject.FromObject(value, serializer); + jObject.AddFirst(new Newtonsoft.Json.Linq.JProperty(_discriminatorName, GetSubtypeDiscriminator(value.GetType()))); + writer.WriteToken(jObject.CreateReader()); + } + finally + { + _isWriting = false; + } + } + + public override bool CanWrite + { + get + { + if (_isWriting) + { + _isWriting = false; + return false; + } + return true; + } + } + + public override bool CanRead + { + get + { + if (_isReading) + { + _isReading = false; + return false; + } + return true; + } + } + + public override bool CanConvert(System.Type objectType) + { + return true; + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var jObject = serializer.Deserialize(reader); + if (jObject == null) + return null; + + var discriminatorValue = jObject.GetValue(_discriminatorName); + var discriminator = discriminatorValue != null ? Newtonsoft.Json.Linq.Extensions.Value(discriminatorValue) : null; + var subtype = GetObjectSubtype(objectType, discriminator); + + var objectContract = serializer.ContractResolver.ResolveContract(subtype) as Newtonsoft.Json.Serialization.JsonObjectContract; + if (objectContract == null || System.Linq.Enumerable.All(objectContract.Properties, p => p.PropertyName != _discriminatorName)) + { + jObject.Remove(_discriminatorName); + } + + try + { + _isReading = true; + return serializer.Deserialize(jObject.CreateReader(), subtype); + } + finally + { + _isReading = false; + } + } + + private System.Type GetObjectSubtype(System.Type objectType, string discriminator) + { + foreach (var attribute in System.Reflection.CustomAttributeExtensions.GetCustomAttributes(System.Reflection.IntrospectionExtensions.GetTypeInfo(objectType), true)) + { + if (attribute.Key == discriminator) + return attribute.Type; + } + + return objectType; + } + + private string GetSubtypeDiscriminator(System.Type objectType) + { + foreach (var attribute in System.Reflection.CustomAttributeExtensions.GetCustomAttributes(System.Reflection.IntrospectionExtensions.GetTypeInfo(objectType), true)) + { + if (attribute.Type == objectType) + return attribute.Key; + } + + return objectType.Name; + } + } + + + + [System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + public partial class ApiException : System.Exception + { + public int StatusCode { get; private set; } + + public string Response { get; private set; } + + public System.Collections.Generic.IReadOnlyDictionary> Headers { get; private set; } + + public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary> headers, System.Exception innerException) + : base(message + "\n\nStatus: " + statusCode + "\nResponse: \n" + ((response == null) ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException) + { + StatusCode = statusCode; + Response = response; + Headers = headers; + } + + public override string ToString() + { + return string.Format("HTTP Response: \n\n{0}\n\n{1}", Response, base.ToString()); + } + } + + [System.CodeDom.Compiler.GeneratedCode("NSwag", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")] + public partial class ApiException : ApiException + { + public TResult Result { get; private set; } + + public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary> headers, TResult result, System.Exception innerException) + : base(message, statusCode, response, headers, innerException) + { + Result = result; + } + } + +} + +#pragma warning restore 108 +#pragma warning restore 114 +#pragma warning restore 472 +#pragma warning restore 612 +#pragma warning restore 1573 +#pragma warning restore 1591 +#pragma warning restore 8073 +#pragma warning restore 3016 +#pragma warning restore 8603 +#pragma warning restore 8604 +#pragma warning restore 8625 \ No newline at end of file diff --git a/test/WebSites/NswagClientExample/Controllers/AnimalsController.cs b/test/WebSites/NswagClientExample/Controllers/AnimalsController.cs index 758a3f6969..f0cde66e77 100644 --- a/test/WebSites/NswagClientExample/Controllers/AnimalsController.cs +++ b/test/WebSites/NswagClientExample/Controllers/AnimalsController.cs @@ -17,9 +17,9 @@ public void CreateAnimal([Required]Animal animal) } } - [SwaggerDiscriminator("animalType")] - [SwaggerSubType(typeof(Cat), DiscriminatorValue = "Cat")] - [SwaggerSubType(typeof(Dog), DiscriminatorValue = "Dog")] + [JsonPolymorphic(TypeDiscriminatorPropertyName = "animalType")] + [JsonDerivedType(typeof(Cat), "Cat")] + [JsonDerivedType(typeof(Dog), "Dog")] public class Animal { public AnimalType AnimalType { get; set; }