Skip to content

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

@jhavlicek-profinit

Description

@jhavlicek-profinit

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

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions