Skip to content

Set AdditionalProperties if Type has JsonExtensionData attribute. #2809

@n-rowe

Description

@n-rowe

Is your feature request related to a problem? Please describe.
No problem, just a nice to have.

Describe the solution you'd like
If a schema has a field with the [JsonExtensionData] attribute it should specify AdditionalProperties: {} or AdditionalProperties: true to allow for a free-form object.

Describe alternatives you've considered
I've setup my own SchemaTransformer that does this:

options.AddSchemaTransformer((schema, context, cancellationToken) =>
{
    var extensionDataProperty = context.JsonTypeInfo.Type
        .GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .FirstOrDefault(p => Attribute.IsDefined(p, typeof(JsonExtensionDataAttribute)));

    if (extensionDataProperty is not null)
    {
        schema.AdditionalPropertiesAllowed = true;
        schema.AdditionalProperties = new OpenApiSchema();
    }

    return Task.CompletedTask;
});

Additional context
I believe it makes sense for this to be supported because if you have a model that has ExtensionData it accepts additional properties, but the OpenApi schema gen doesn't represent this without manual intervention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions