From 10182953d9d0b96a2f75cb1c4398cf67ce2795d8 Mon Sep 17 00:00:00 2001 From: David Nelson Date: Thu, 13 Jun 2024 13:10:57 -0500 Subject: [PATCH] Added support for "requestBodies" in components --- src/NSwag.Core/OpenApiComponents.cs | 14 ++++++++++++++ src/NSwag.Core/OpenApiMediaType.cs | 4 ++-- src/NSwag.Core/OpenApiRequestBody.cs | 17 ++++++++++------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/NSwag.Core/OpenApiComponents.cs b/src/NSwag.Core/OpenApiComponents.cs index b1e416d111..985c04f454 100644 --- a/src/NSwag.Core/OpenApiComponents.cs +++ b/src/NSwag.Core/OpenApiComponents.cs @@ -38,6 +38,16 @@ public OpenApiComponents(OpenApiDocument document) }; Schemas = schemas; + var requestBodies = new ObservableDictionary(); + requestBodies.CollectionChanged += (sender, args) => + { + foreach (var path in RequestBodies.Values) + { + path.Parent = document; + } + }; + RequestBodies = requestBodies; + var responses = new ObservableDictionary(); responses.CollectionChanged += (sender, args) => { @@ -86,6 +96,10 @@ public OpenApiComponents(OpenApiDocument document) [JsonProperty(PropertyName = "schemas", DefaultValueHandling = DefaultValueHandling.Ignore)] public IDictionary Schemas { get; } + /// Gets or sets the responses which can be used for all operations. + [JsonProperty(PropertyName = "requestBodies", DefaultValueHandling = DefaultValueHandling.Ignore)] + public IDictionary RequestBodies { get; } + /// Gets or sets the responses which can be used for all operations. [JsonProperty(PropertyName = "responses", DefaultValueHandling = DefaultValueHandling.Ignore)] public IDictionary Responses { get; } diff --git a/src/NSwag.Core/OpenApiMediaType.cs b/src/NSwag.Core/OpenApiMediaType.cs index bf16abfb08..3ed4e81735 100644 --- a/src/NSwag.Core/OpenApiMediaType.cs +++ b/src/NSwag.Core/OpenApiMediaType.cs @@ -29,7 +29,7 @@ public JsonSchema Schema set { _schema = value; - Parent?.Parent?.UpdateBodyParameter(); + Parent?.ParentOperation?.UpdateBodyParameter(); } } @@ -41,7 +41,7 @@ public object Example set { _example = value; - Parent?.Parent?.UpdateBodyParameter(); + Parent?.ParentOperation?.UpdateBodyParameter(); } } diff --git a/src/NSwag.Core/OpenApiRequestBody.cs b/src/NSwag.Core/OpenApiRequestBody.cs index 1f5093841a..5663eb8f15 100644 --- a/src/NSwag.Core/OpenApiRequestBody.cs +++ b/src/NSwag.Core/OpenApiRequestBody.cs @@ -32,13 +32,16 @@ public OpenApiRequestBody() mediaType.Parent = this; } - Parent?.UpdateBodyParameter(); + ParentOperation?.UpdateBodyParameter(); }; Content = content; } [JsonIgnore] - internal OpenApiOperation Parent { get; set; } + internal object Parent { get; set; } + + [JsonIgnore] + internal OpenApiOperation ParentOperation => Parent as OpenApiOperation; /// Gets the actual request body, either this or the referenced request body. [JsonIgnore] @@ -52,7 +55,7 @@ public string Name set { _name = value; - Parent?.UpdateBodyParameter(); + ParentOperation?.UpdateBodyParameter(); } } @@ -64,7 +67,7 @@ public string Description set { _description = value; - Parent?.UpdateBodyParameter(); + ParentOperation?.UpdateBodyParameter(); } } @@ -80,7 +83,7 @@ public bool IsRequired set { _isRequired = value; - Parent?.UpdateBodyParameter(); + ParentOperation?.UpdateBodyParameter(); } } @@ -92,7 +95,7 @@ public int? Position set { _position = value; - Parent?.UpdateBodyParameter(); + ParentOperation?.UpdateBodyParameter(); } } @@ -106,7 +109,7 @@ public int? Position IJsonReference IJsonReference.ActualObject => ActualRequestBody; [JsonIgnore] - object IJsonReference.PossibleRoot => Parent?.Parent?.Parent; + object IJsonReference.PossibleRoot => ParentOperation?.Parent?.Parent; #endregion }