Skip to content

Commit d87375d

Browse files
committed
fix: last reference to copy constructor
Signed-off-by: Vincent Biret <[email protected]>
1 parent e4c14a4 commit d87375d

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.OpenApi.Extensions;
99
using Microsoft.OpenApi.Interfaces;
1010
using Microsoft.OpenApi.Models.Interfaces;
11+
using Microsoft.OpenApi.Models.References;
1112
using Microsoft.OpenApi.Writers;
1213

1314
namespace Microsoft.OpenApi.Models
@@ -123,19 +124,27 @@ public IEnumerable<IOpenApiParameter> ConvertToFormDataParameters(IOpenApiWriter
123124

124125
foreach (var property in Content.First().Value.Schema.Properties)
125126
{
126-
var paramSchema = new OpenApiSchema(property.Value);
127+
var paramSchema = property.Value.CreateShallowCopy();
127128
if ((paramSchema.Type & JsonSchemaType.String) == JsonSchemaType.String
128129
&& ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)
129130
|| "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)))
130131
{
131-
paramSchema.Type = "file".ToJsonSchemaType();
132-
paramSchema.Format = null;
132+
var updatedSchema = paramSchema switch {
133+
OpenApiSchema s => s, // we already have a copy
134+
// we have a copy of a reference but don't want to mutate the source schema
135+
// TODO might need recursive resolution of references here
136+
OpenApiSchemaReference r => (OpenApiSchema)r.Target.CreateShallowCopy(),
137+
_ => throw new InvalidOperationException("Unexpected schema type")
138+
};
139+
updatedSchema.Type = "file".ToJsonSchemaType();
140+
updatedSchema.Format = null;
141+
paramSchema = updatedSchema;
133142
}
134143
yield return new OpenApiFormDataParameter()
135144
{
136-
Description = property.Value.Description,
145+
Description = paramSchema.Description,
137146
Name = property.Key,
138-
Schema = property.Value,
147+
Schema = paramSchema,
139148
Examples = Content.Values.FirstOrDefault()?.Examples,
140149
Required = Content.First().Value.Schema.Required?.Contains(property.Key) ?? false
141150
};

0 commit comments

Comments
 (0)