|
8 | 8 | using Microsoft.OpenApi.Extensions; |
9 | 9 | using Microsoft.OpenApi.Interfaces; |
10 | 10 | using Microsoft.OpenApi.Models.Interfaces; |
| 11 | +using Microsoft.OpenApi.Models.References; |
11 | 12 | using Microsoft.OpenApi.Writers; |
12 | 13 |
|
13 | 14 | namespace Microsoft.OpenApi.Models |
@@ -123,19 +124,27 @@ public IEnumerable<IOpenApiParameter> ConvertToFormDataParameters(IOpenApiWriter |
123 | 124 |
|
124 | 125 | foreach (var property in Content.First().Value.Schema.Properties) |
125 | 126 | { |
126 | | - var paramSchema = new OpenApiSchema(property.Value); |
| 127 | + var paramSchema = property.Value.CreateShallowCopy(); |
127 | 128 | if ((paramSchema.Type & JsonSchemaType.String) == JsonSchemaType.String |
128 | 129 | && ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase) |
129 | 130 | || "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase))) |
130 | 131 | { |
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; |
133 | 142 | } |
134 | 143 | yield return new OpenApiFormDataParameter() |
135 | 144 | { |
136 | | - Description = property.Value.Description, |
| 145 | + Description = paramSchema.Description, |
137 | 146 | Name = property.Key, |
138 | | - Schema = property.Value, |
| 147 | + Schema = paramSchema, |
139 | 148 | Examples = Content.Values.FirstOrDefault()?.Examples, |
140 | 149 | Required = Content.First().Value.Schema.Required?.Contains(property.Key) ?? false |
141 | 150 | }; |
|
0 commit comments