Skip to content

[Openapi] How to achieve custom OneOfToAllOfNullableTransformer #64651

@SpringHgui

Description

@SpringHgui

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

/// <summary>
/// Transforms schemas with oneOf nullable pattern to allOf + nullable pattern.
/// Converts:
///   { "oneOf": [ { "type": "null" }, { "$ref": "..." } ] }
/// To:
///   { "allOf": [ { "$ref": "..." } ], "nullable": true }
/// </summary>
public sealed class OneOfToAllOfNullableTransformer : IOpenApiSchemaTransformer
{
    public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken)
    {
        if (schema is OpenApiSchema openApiSchema)
        {
            TransformSchema(openApiSchema);
        }
        return Task.CompletedTask;
    }

    private void TransformSchema(OpenApiSchema schema)
    {
        // How to do here ?
        // Filter oneOf
        if (schema.OneOf != null && schema.OneOf.Count == 2)
        {
             // The breakpoint cannot be hit here.
        }
    }
}

Describe the solution you'd like

The breakpoint can be hit, and I can configure oneOf and allOf.

// Filter oneOf
if (schema.OneOf != null && schema.OneOf.Count == 2)
{
     // The breakpoint cannot be hit here.
}

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions