-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Apply schema transformers on properties and other subschemas #56709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
62e16af
c525ccd
5180487
ac380b6
9653178
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,17 +144,83 @@ internal async Task<OpenApiSchema> GetOrCreateSchemaAsync(Type type, ApiParamete | |
|
||
internal async Task ApplySchemaTransformersAsync(OpenApiSchema schema, Type type, ApiParameterDescription? parameterDescription = null, CancellationToken cancellationToken = default) | ||
{ | ||
var jsonTypeInfo = _jsonSerializerOptions.GetTypeInfo(type); | ||
var context = new OpenApiSchemaTransformerContext | ||
{ | ||
DocumentName = documentName, | ||
Type = type, | ||
JsonTypeInfo = jsonTypeInfo, | ||
JsonPropertyInfo = null, | ||
ParameterDescription = parameterDescription, | ||
ApplicationServices = serviceProvider | ||
}; | ||
for (var i = 0; i < _openApiOptions.SchemaTransformers.Count; i++) | ||
{ | ||
var transformer = _openApiOptions.SchemaTransformers[i]; | ||
await transformer.TransformAsync(schema, context, cancellationToken); | ||
// If the transformer is a type-based transformer, we need to initialize and finalize it | ||
// once in the context of the top-level assembly and not the child properties we are invoking | ||
// it on. | ||
if (transformer is TypeBasedOpenApiSchemaTransformer typeBasedTransformer) | ||
{ | ||
var initializedTransformer = typeBasedTransformer.InitializeTransformer(serviceProvider); | ||
try | ||
{ | ||
await InnerApplySchemaTransformersAsync(schema, jsonTypeInfo, context, initializedTransformer, cancellationToken); | ||
} | ||
finally | ||
{ | ||
await TypeBasedOpenApiSchemaTransformer.FinalizeTransformer(initializedTransformer); | ||
} | ||
} | ||
else | ||
{ | ||
await InnerApplySchemaTransformersAsync(schema, jsonTypeInfo, context, transformer, cancellationToken); | ||
} | ||
} | ||
} | ||
|
||
private async Task InnerApplySchemaTransformersAsync(OpenApiSchema schema, | ||
JsonTypeInfo jsonTypeInfo, | ||
OpenApiSchemaTransformerContext context, | ||
IOpenApiSchemaTransformer transformer, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
await transformer.TransformAsync(schema, context, cancellationToken); | ||
|
||
// Only apply transformers on polymorphic schemas where we can resolve the derived | ||
// types associated with the base type. | ||
if (schema.AnyOf is { Count: > 0 } && jsonTypeInfo.PolymorphismOptions is not null) | ||
{ | ||
var anyOfIndex = 0; | ||
foreach (var derivedType in jsonTypeInfo.PolymorphismOptions.DerivedTypes) | ||
{ | ||
var derivedJsonTypeInfo = _jsonSerializerOptions.GetTypeInfo(derivedType.DerivedType); | ||
context.UpdateJsonTypeInfo(derivedJsonTypeInfo, null); | ||
if (schema.AnyOf.Count <= anyOfIndex) | ||
{ | ||
break; | ||
} | ||
await InnerApplySchemaTransformersAsync(schema.AnyOf[anyOfIndex], derivedJsonTypeInfo, context, transformer, cancellationToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't see any bounds checking here. If it's done earlier, it's far from this callsite and doesn't feel safe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this takes an assumption on the implementation details of the schema generator which generates the anyOf child schemas based on the ordering in |
||
anyOfIndex++; | ||
} | ||
} | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (schema.Items is not null) | ||
{ | ||
var elementTypeInfo = _jsonSerializerOptions.GetTypeInfo(jsonTypeInfo.ElementType!); | ||
context.UpdateJsonTypeInfo(elementTypeInfo, null); | ||
await InnerApplySchemaTransformersAsync(schema.Items, elementTypeInfo, context, transformer, cancellationToken); | ||
} | ||
|
||
if (schema.Properties is { Count: > 0 }) | ||
{ | ||
foreach (var propertyInfo in jsonTypeInfo.Properties) | ||
{ | ||
context.UpdateJsonTypeInfo(_jsonSerializerOptions.GetTypeInfo(propertyInfo.PropertyType), propertyInfo); | ||
if (schema.Properties.TryGetValue(propertyInfo.Name, out var propertySchema)) | ||
{ | ||
await InnerApplySchemaTransformersAsync(propertySchema, _jsonSerializerOptions.GetTypeInfo(propertyInfo.PropertyType), context, transformer, cancellationToken); | ||
} | ||
} | ||
} | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.