Skip to content

Commit

Permalink
Support $ref in request-bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
Elzo Lubbers committed Jul 29, 2024
1 parent 7d6df3a commit b9a77e8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/NSwag.Core/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public OpenApiComponents(OpenApiDocument document)
};
Responses = responses;

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

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

/// <summary>Gets or sets the request bodies 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 parameters which can be used for all operations.</summary>
[JsonProperty(PropertyName = "parameters", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiParameter> Parameters { get; }
Expand Down
5 changes: 5 additions & 0 deletions src/NSwag.Core/OpenApiDocument.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -210,5 +211,9 @@ private void UpdateServers(ICollection<OpenApiSchema> schemes, string host, stri
/// <summary>Gets or sets the security definitions (Swagger only).</summary>
[JsonProperty(PropertyName = "securityDefinitions", Order = 16, DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiSecurityScheme> SecurityDefinitions => Components.SecuritySchemes;

/// <summary>Gets or sets the request bodies which can be used for all operations (Swagger only).</summary>
[JsonProperty(PropertyName = "requestBodies", Order = 17, DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiRequestBody> RequestBodies => Components.RequestBodies;
}
}
12 changes: 6 additions & 6 deletions src/NSwag.Core/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b9a77e8

Please sign in to comment.