dotnet / dotnet/AspNetCore.Docs

How to modify get-only schema properties for in OpenApi transformers

Open
#36,762 2 comments 1 reaction 1 assignee Claimed by @wadepickett View on GitHub
ai-triage-action-plan aspnet-core/svc fundamentals/subsvc Source - Docs.ms
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 2h
Merged PRs (30d)
109

Description

### Description

Many properties of OpenApi.NET objects provided to the transformers are interfaces that have get-only properties. It's not clear from documentation, how should the transformer be written to modify a get-only property. It does work with casting, but that seems like rather unclean solution.

Example: I want to set an Example property of a parameters

```csharp
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)
{
foreach (var parameter in operation.Parameters ?? [])
{
if (parameter.Name == "sort")
{
// parameter is a IOpenApiParameter. The Example property is get-only and can't be set. To set it, I cast it to a concrete type that has a setter.
// The IOpenApiParameterinterface can be
// * OpenApiParameter (further subclassed to OpenApiBodyParameter / OpenApiFormDataParameter)
// * or OpenApiParameterReference
((OpenApiParameter)parameter).Example = "something";
}
}

return Task.CompletedTask;
}
```

### Page URL

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/customize-openapi?view=aspnetcore-10.0#use-operation-transformers

### Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/openapi/customize-openapi.md

### Document ID

ea5fce3c-e38f-9531-b77a-3e9a5e8e30ce

### Platform Id

126b754a-260a-3efc-a261-b0d72ad0d8ce

### Article author

@wadepickett

### Metadata

* ID: ea5fce3c-e38f-9531-b77a-3e9a5e8e30ce
* PlatformId: 126b754a-260a-3efc-a261-b0d72ad0d8ce
* Service: **aspnet-core**
* Sub-service: **fundamentals**

[Related Issues](https://github.com/dotnet/AspNetCore.Docs/issues?q=is%3Aissue+is%3Aopen+ea5fce3c-e38f-9531-b77a-3e9a5e8e30ce)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.