From b9a77e82a67dff048994bc9a20abddb6694936ee Mon Sep 17 00:00:00 2001 From: Elzo Lubbers Date: Wed, 31 Jan 2024 11:23:24 +0100 Subject: [PATCH] Support $ref in request-bodies --- src/NSwag.Core/OpenApiComponents.cs | 14 ++++++++++++++ src/NSwag.Core/OpenApiDocument.Serialization.cs | 5 +++++ src/NSwag.Core/OpenApiOperation.cs | 12 ++++++------ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/NSwag.Core/OpenApiComponents.cs b/src/NSwag.Core/OpenApiComponents.cs index 985c04f45..4556d7167 100644 --- a/src/NSwag.Core/OpenApiComponents.cs +++ b/src/NSwag.Core/OpenApiComponents.cs @@ -58,6 +58,16 @@ public OpenApiComponents(OpenApiDocument document) }; Responses = responses; + var requestBodies = new ObservableDictionary(); + requestBodies.CollectionChanged += (sender, args) => + { + foreach (var path in RequestBodies.Values) + { + path.Parent = document; + } + }; + RequestBodies = requestBodies; + var parameters = new ObservableDictionary(); parameters.CollectionChanged += (sender, args) => { @@ -104,6 +114,10 @@ public OpenApiComponents(OpenApiDocument document) [JsonProperty(PropertyName = "responses", DefaultValueHandling = DefaultValueHandling.Ignore)] public IDictionary Responses { get; } + /// Gets or sets the request bodies which can be used for all operations. + [JsonProperty(PropertyName = "requestBodies", DefaultValueHandling = DefaultValueHandling.Ignore)] + public IDictionary RequestBodies { get; } + /// Gets or sets the parameters which can be used for all operations. [JsonProperty(PropertyName = "parameters", DefaultValueHandling = DefaultValueHandling.Ignore)] public IDictionary Parameters { get; } diff --git a/src/NSwag.Core/OpenApiDocument.Serialization.cs b/src/NSwag.Core/OpenApiDocument.Serialization.cs index 58498b218..2410acbda 100644 --- a/src/NSwag.Core/OpenApiDocument.Serialization.cs +++ b/src/NSwag.Core/OpenApiDocument.Serialization.cs @@ -97,6 +97,7 @@ private static PropertyRenameAndIgnoreSerializerContractResolver CreateJsonSeria resolver.IgnoreProperty(typeof(OpenApiDocument), "definitions"); resolver.IgnoreProperty(typeof(OpenApiDocument), "parameters"); resolver.IgnoreProperty(typeof(OpenApiDocument), "responses"); + resolver.IgnoreProperty(typeof(OpenApiDocument), "requestBodies"); resolver.IgnoreProperty(typeof(OpenApiDocument), "securityDefinitions"); resolver.IgnoreProperty(typeof(OpenApiResponse), "schema"); @@ -210,5 +211,9 @@ private void UpdateServers(ICollection schemes, string host, stri /// Gets or sets the security definitions (Swagger only). [JsonProperty(PropertyName = "securityDefinitions", Order = 16, DefaultValueHandling = DefaultValueHandling.Ignore)] public IDictionary SecurityDefinitions => Components.SecuritySchemes; + + /// Gets or sets the request bodies which can be used for all operations (Swagger only). + [JsonProperty(PropertyName = "requestBodies", Order = 17, DefaultValueHandling = DefaultValueHandling.Ignore)] + public IDictionary RequestBodies => Components.RequestBodies; } } diff --git a/src/NSwag.Core/OpenApiOperation.cs b/src/NSwag.Core/OpenApiOperation.cs index a02a60043..5690e2d46 100644 --- a/src/NSwag.Core/OpenApiOperation.cs +++ b/src/NSwag.Core/OpenApiOperation.cs @@ -298,12 +298,12 @@ private OpenApiParameter CreateBodyParameter() private void UpdateBodyParameter(OpenApiParameter parameter) { parameter.Kind = OpenApiParameterKind.Body; - parameter.Name = RequestBody.ActualName; - parameter.Position = RequestBody.Position; - parameter.Description = RequestBody.Description; - parameter.IsRequired = RequestBody.IsRequired; - parameter.Example = RequestBody.Content.FirstOrDefault().Value?.Example; - parameter.Schema = RequestBody.Content.FirstOrDefault().Value?.Schema; + parameter.Name = RequestBody.ActualRequestBody.ActualName; + parameter.Position = RequestBody.ActualRequestBody.Position; + parameter.Description = RequestBody.ActualRequestBody.Description; + parameter.IsRequired = RequestBody.ActualRequestBody.IsRequired; + parameter.Example = RequestBody.ActualRequestBody.Content.FirstOrDefault().Value?.Example; + parameter.Schema = RequestBody.ActualRequestBody.Content.FirstOrDefault().Value?.Schema; } private void UpdateRequestBody(NotifyCollectionChangedEventArgs args)