Skip to content

Commit

Permalink
Ugly hacks to add minimal support for parsing references to component…
Browse files Browse the repository at this point in the history
…s.requestBodies and generating code
  • Loading branch information
lakeman committed Sep 20, 2022
1 parent 3c36e47 commit 4528a59
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/NSwag.CodeGeneration.CSharp/CSharpGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public static CSharpTypeResolver CreateResolverWithExceptionSchema(CSharpGenerat
.Where(p => p.Value != exceptionSchema)
.ToDictionary(p => p.Key, p => p.Value));

resolver.RegisterSchemaDefinitions(document.Components.RequestBodies
.Select(kp => (kp.Key, kp.Value.Content.FirstOrDefault().Value?.Schema))
.Where(p => p.Schema != exceptionSchema && p.Schema != null)
.ToDictionary(p => p.Key, p => p.Schema));

return resolver;
}

Expand Down
19 changes: 17 additions & 2 deletions src/NSwag.Core/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public OpenApiComponents(OpenApiDocument document)

Examples = new Dictionary<string, OpenApiExample>();


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

var headers = new ObservableDictionary<string, OpenApiParameter>();
headers.CollectionChanged += (sender, args) =>
{
Expand Down Expand Up @@ -94,11 +105,15 @@ public OpenApiComponents(OpenApiDocument document)
[JsonProperty(PropertyName = "parameters", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiParameter> Parameters { get; }

/// <summary>Gets or sets the headers.</summary>
/// <summary>Gets or sets the examples.</summary>
[JsonProperty(PropertyName = "examples", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiExample> Examples { get; set; }

/// <summary>Gets or sets the types.</summary>
/// <summary>Gets or sets the request bodies.</summary>
[JsonProperty(PropertyName = "requestBodies", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiRequestBody> RequestBodies { get; set; }

/// <summary>Gets or sets the headers.</summary>
[JsonProperty(PropertyName = "headers", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiParameter> Headers { get; }

Expand Down
10 changes: 9 additions & 1 deletion src/NSwag.Core/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public OpenApiOperation()

/// <summary>Gets or sets the request body (OpenAPI only).</summary>
[JsonProperty(PropertyName = "requestBody", Order = 9, DefaultValueHandling = DefaultValueHandling.Ignore)]
public OpenApiRequestBody RequestBody
public OpenApiRequestBody JsonRequestBody
{
get => _requestBody;
set
Expand All @@ -106,6 +106,14 @@ public OpenApiRequestBody RequestBody
}
}

/// <summary>Gets or sets the request body (OpenAPI only).</summary>
[JsonIgnore]
public OpenApiRequestBody RequestBody
{
get => JsonRequestBody?.ActualRequestBody;
set => JsonRequestBody = value;
}

/// <summary>Gets the actual parameters (a combination of all inherited and local parameters).</summary>
[JsonIgnore]
public IReadOnlyList<OpenApiParameter> ActualParameters
Expand Down
27 changes: 26 additions & 1 deletion src/NSwag.Core/OpenApiRequestBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

using System.Collections.Generic;
using Newtonsoft.Json;
using NJsonSchema.References;
using NSwag.Collections;

namespace NSwag
{
/// <summary>The OpenApi request body (OpenAPI only).</summary>
public class OpenApiRequestBody
public class OpenApiRequestBody : JsonReferenceBase<OpenApiRequestBody>, IJsonReference
{
private string _name;
private bool _isRequired;
Expand All @@ -38,6 +39,8 @@ public OpenApiRequestBody()

[JsonIgnore]
internal OpenApiOperation Parent { get; set; }
[JsonIgnore]
internal OpenApiDocument ParentDocument { get; set; }

/// <summary>Gets or sets the name.</summary>
[JsonProperty(PropertyName = "x-name", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
Expand Down Expand Up @@ -94,5 +97,27 @@ public int? Position
/// <summary>Gets the actual name of the request body parameter.</summary>
[JsonIgnore]
public string ActualName => string.IsNullOrEmpty(Name) ? "body" : Name;

public override OpenApiRequestBody Reference {
get => base.Reference;
set
{
base.Reference = value;
Parent?.UpdateBodyParameter();
}
}
/// <summary>Gets the actual request body, either this or the referenced example.</summary>
[JsonIgnore]
public OpenApiRequestBody ActualRequestBody => Reference ?? this;

#region Implementation of IJsonReference

[JsonIgnore]
IJsonReference IJsonReference.ActualObject => ActualRequestBody;

[JsonIgnore]
object IJsonReference.PossibleRoot => (object)ParentDocument ?? Parent;

#endregion
}
}

0 comments on commit 4528a59

Please sign in to comment.