Skip to content

Commit

Permalink
Added support for "requestBodies" in components
Browse files Browse the repository at this point in the history
  • Loading branch information
eatdrinksleepcode committed Jun 17, 2024
1 parent 94063c0 commit a9ae805
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/NSwag.Core/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public OpenApiComponents(OpenApiDocument document)
};
Schemas = schemas;

var requestBodies = new ObservableDictionary<string, OpenApiRequestBody>();
requestBodies.CollectionChanged += (sender, args) =>
{
foreach (var path in RequestBodies.Values)
{
path.Parent = document;
}
};
RequestBodies = requestBodies;

var responses = new ObservableDictionary<string, OpenApiResponse>();
responses.CollectionChanged += (sender, args) =>
{
Expand Down Expand Up @@ -86,6 +96,10 @@ public OpenApiComponents(OpenApiDocument document)
[JsonProperty(PropertyName = "schemas", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, JsonSchema> Schemas { get; }

/// <summary>Gets or sets the responses which can be used for all operations.</summary>
[JsonProperty(PropertyName = "requestBodies", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiRequestBody> RequestBodies { get; }

/// <summary>Gets or sets the responses which can be used for all operations.</summary>
[JsonProperty(PropertyName = "responses", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiResponse> Responses { get; }
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.Core/OpenApiMediaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public JsonSchema Schema
set
{
_schema = value;
Parent?.Parent?.UpdateBodyParameter();
Parent?.ParentOperation?.UpdateBodyParameter();
}
}

Expand All @@ -41,7 +41,7 @@ public object Example
set
{
_example = value;
Parent?.Parent?.UpdateBodyParameter();
Parent?.ParentOperation?.UpdateBodyParameter();
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/NSwag.Core/OpenApiRequestBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>Gets the actual request body, either this or the referenced request body.</summary>
[JsonIgnore]
Expand All @@ -52,7 +55,7 @@ public string Name
set
{
_name = value;
Parent?.UpdateBodyParameter();
ParentOperation?.UpdateBodyParameter();
}
}

Expand All @@ -64,7 +67,7 @@ public string Description
set
{
_description = value;
Parent?.UpdateBodyParameter();
ParentOperation?.UpdateBodyParameter();
}
}

Expand All @@ -80,7 +83,7 @@ public bool IsRequired
set
{
_isRequired = value;
Parent?.UpdateBodyParameter();
ParentOperation?.UpdateBodyParameter();
}
}

Expand All @@ -92,7 +95,7 @@ public int? Position
set
{
_position = value;
Parent?.UpdateBodyParameter();
ParentOperation?.UpdateBodyParameter();
}
}

Expand All @@ -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
}
Expand Down

0 comments on commit a9ae805

Please sign in to comment.