Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Apply schema transformer to AdditionalProperties
  • Loading branch information
Jelle Teeuwissen authored and github-actions committed Jan 6, 2025
commit e5d7390773bbd857ab03f6ac2566018270cf2f10
8 changes: 7 additions & 1 deletion src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,13 @@ private async Task InnerApplySchemaTransformersAsync(OpenApiSchema schema,
}
}
}
}

if (schema is { AdditionalPropertiesAllowed: true, AdditionalProperties: not null } && jsonTypeInfo.ElementType is not null)
{
var elementTypeInfo = _jsonSerializerOptions.GetTypeInfo(jsonTypeInfo.ElementType);
await InnerApplySchemaTransformersAsync(schema.AdditionalProperties, elementTypeInfo, null, context, transformer, cancellationToken);
}
}

private JsonNode CreateSchema(OpenApiSchemaKey key)
=> JsonSchemaExporter.GetJsonSchemaAsNode(_jsonSerializerOptions, key.Type, _configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ public async Task SchemaTransformer_CanModifyItemTypesInADocument()

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

var options = new OpenApiOptions();
options.AddSchemaTransformer((schema, context, cancellationToken) =>
Expand All @@ -469,7 +470,13 @@ await VerifyOpenApiDocument(builder, options, document =>
getOperation = path.Operations[OperationType.Get];
responseSchema = getOperation.Responses["200"].Content["application/json"].Schema.GetEffective(document);
Assert.Equal("modified-number-format", responseSchema.Format);
});

// Assert that the schema represent dictionary values has been modified
path = document.Paths["/dictionary"];
getOperation = path.Operations[OperationType.Get];
responseSchema = getOperation.Responses["200"].Content["application/json"].Schema.GetEffective(document);
Assert.Equal("modified-number-format", responseSchema.AdditionalProperties.Format);
});
}

[Fact]
Expand Down
Loading