Skip to content

Commit 6238e5d

Browse files
Apply schema transformer to AdditionalProperties (#59730)
Co-authored-by: Jelle Teeuwissen <[email protected]>
1 parent 8e6dbc6 commit 6238e5d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,13 @@ private async Task InnerApplySchemaTransformersAsync(OpenApiSchema schema,
212212
}
213213
}
214214
}
215-
}
215+
216+
if (schema is { AdditionalPropertiesAllowed: true, AdditionalProperties: not null } && jsonTypeInfo.ElementType is not null)
217+
{
218+
var elementTypeInfo = _jsonSerializerOptions.GetTypeInfo(jsonTypeInfo.ElementType);
219+
await InnerApplySchemaTransformersAsync(schema.AdditionalProperties, elementTypeInfo, null, context, transformer, cancellationToken);
220+
}
221+
}
216222

217223
private JsonNode CreateSchema(OpenApiSchemaKey key)
218224
=> JsonSchemaExporter.GetJsonSchemaAsNode(_jsonSerializerOptions, key.Type, _configuration);

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/SchemaTransformerTests.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ public async Task SchemaTransformer_CanModifyItemTypesInADocument()
444444

445445
builder.MapGet("/list", () => new List<int> { 1, 2, 3, 4 });
446446
builder.MapGet("/single", () => 1);
447+
builder.MapGet("/dictionary", () => new Dictionary<string, int> {{ "key", 1 }});
447448

448449
var options = new OpenApiOptions();
449450
options.AddSchemaTransformer((schema, context, cancellationToken) =>
@@ -469,7 +470,13 @@ await VerifyOpenApiDocument(builder, options, document =>
469470
getOperation = path.Operations[OperationType.Get];
470471
responseSchema = getOperation.Responses["200"].Content["application/json"].Schema.GetEffective(document);
471472
Assert.Equal("modified-number-format", responseSchema.Format);
472-
});
473+
474+
// Assert that the schema represent dictionary values has been modified
475+
path = document.Paths["/dictionary"];
476+
getOperation = path.Operations[OperationType.Get];
477+
responseSchema = getOperation.Responses["200"].Content["application/json"].Schema.GetEffective(document);
478+
Assert.Equal("modified-number-format", responseSchema.AdditionalProperties.Format);
479+
});
473480
}
474481

475482
[Fact]

0 commit comments

Comments
 (0)