Skip to content

Fix attributes on primary constructor parameters in OpenAPI documents #62027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
81 changes: 81 additions & 0 deletions src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -326,12 +327,92 @@
schema.ApplyValidationAttributes(validationAttributes);
}

// Handle class types with primary constructors
// Primary constructors in classes don't automatically create properties like they do in records,
// so we need to manually find constructor parameters that match property names and apply their attributes
if (schema is JsonObject schemaObject && schemaObject["properties"] is JsonObject propertiesObject)
{
ApplyCtorAttributes(parameterInfo.ParameterType, propertiesObject);
}

schema.ApplyNullabilityContextInfo(parameterInfo);
}

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

Check failure on line 339 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L339

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(339,10): error CS1513: (NETCORE_ENGINEERING_TELEMETRY=Build) } expected

/// <summary>
/// Applies attributes from constructor parameters to the corresponding properties.
/// </summary>
/// <param name="dtoType">The type to inspect for constructor parameters.</param>
/// <param name="propertiesObject">The JSON object containing property schemas.</param>
private static void ApplyCtorAttributes(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
Type dtoType,
JsonObject propertiesObject)
{
// Get all constructors of the type - limited to public ones only for trim safety
var constructors = dtoType.GetConstructors();

// Find primary constructor - look for the constructor with the most parameters
// that match property names, similar to how records work
var bestConstructor = constructors
.OrderByDescending(c => c.GetParameters().Length)
.FirstOrDefault();

if (bestConstructor?.GetParameters() is { Length: > 0 } parameters)
{
ApplyParameterAttributesToProperties(parameters, propertiesObject);
}
}

/// <summary>
/// Helper method to apply parameter attributes to matching properties.
/// </summary>
/// <param name="parameters">The constructor parameters.</param>
/// <param name="propertiesObject">The JSON object containing property schemas.</param>
private static void ApplyParameterAttributesToProperties(ParameterInfo[] parameters, JsonObject propertiesObject)
{
foreach (var parameter in parameters)
{
if (parameter.Name is null)
{
continue;
}

// Try different naming conventions to match property names
// 1. Direct match (parameter name to property name)
// 2. Camel case (first char lower case)
// 3. Pascal case (first char upper case)
var paramName = parameter.Name;
var possiblePropertyNames = new[]
{
paramName,
char.ToLowerInvariant(paramName[0]) + paramName[1..],
char.ToUpperInvariant(paramName[0]) + paramName[1..]
};

// Find matching property in the schema
JsonObject? propertySchema = null;
foreach (var propName in possiblePropertyNames)
{
if (propertiesObject[propName] is JsonObject schema)
{
propertySchema = schema;
break;
}
}

if (propertySchema != null)
{
// Apply validation attributes from constructor parameter
// regardless of whether they have the 'property:' prefix or not
var paramAttributes = parameter.GetCustomAttributes().OfType<ValidationAttribute>();
propertySchema.ApplyValidationAttributes(paramAttributes);
}
}
}
// Route constraints are only defined on parameters that are sourced from the path. Since
// they are encoded in the route template, and not in the type information based to the underlying
// schema generator we have to handle them separately here.
if (parameterDescription.RouteInfo?.Constraints is { } constraints)

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token '.' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,9): error CS1519: (NETCORE_ENGINEERING_TELEMETRY=Build) Invalid token 'if' in a member declaration

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS8124: (NETCORE_ENGINEERING_TELEMETRY=Build) Tuple must contain at least two elements.

Check failure on line 415 in src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs#L415

src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs(415,44): error CS1026: (NETCORE_ENGINEERING_TELEMETRY=Build) ) expected
{
schema.ApplyRouteConstraints(constraints);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.DataAnnotations;
using System.Net.Http;
using System.Text.Json;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.OpenApi.Models;

public partial class OpenApiSchemaServiceTests
{
[Theory]
[InlineData(true, true)] // Test with record, property prefix
[InlineData(false, true)] // Test with class, property prefix
[InlineData(true, false)] // Test with record, no property prefix
[InlineData(false, false)] // Test with class, no property prefix
public async Task GetOpenApiSchema_HandlesAttributesOnPrimaryConstructorParameters(bool useRecord, bool usePropertyPrefix)
{
// Arrange
var builder = CreateBuilder();

// Act
if (useRecord)
{
if (usePropertyPrefix)
{
builder.MapPost("/api/record/property-prefix", (RecordWithPropertyPrefix dto) => dto);
}
else
{
builder.MapPost("/api/record/no-prefix", (RecordWithNoPrefix dto) => dto);
}
}
else
{
if (usePropertyPrefix)
{
builder.MapPost("/api/class/property-prefix", (ClassWithPropertyPrefix dto) => dto);
}
else
{
builder.MapPost("/api/class/no-prefix", (ClassWithNoPrefix dto) => dto);
}
}

// Assert
await VerifyOpenApiDocument(builder, document =>
{
string path;
string schemaType;

if (useRecord)
{
path = usePropertyPrefix ? "/api/record/property-prefix" : "/api/record/no-prefix";
schemaType = usePropertyPrefix ? nameof(RecordWithPropertyPrefix) : nameof(RecordWithNoPrefix);
}
else
{
path = usePropertyPrefix ? "/api/class/property-prefix" : "/api/class/no-prefix";
schemaType = usePropertyPrefix ? nameof(ClassWithPropertyPrefix) : nameof(ClassWithNoPrefix);
}

var operation = document.Paths[path].Operations[HttpMethod.Post];
var requestBody = operation.RequestBody.Content["application/json"].Schema;

Assert.Equal(schemaType, requestBody.Reference.Id);

var schemaObject = document.Components.Schemas[schemaType];

// Verify the 'age' property has the min/max constraints from the Range attribute
var ageProperty = schemaObject.Properties["age"];
Assert.Equal(0, ageProperty.Minimum);
Assert.Equal(120, ageProperty.Maximum);

// Verify the 'name' property is required from the Required attribute
var nameProperty = schemaObject.Properties["name"];
Assert.Contains("name", schemaObject.Required);
});
}

public record RecordWithPropertyPrefix(
[Required] string name,
[property: Range(0, 120)] int age);

public class ClassWithPropertyPrefix(
[Required] string name,
[property: Range(0, 120)] int age)
{
public string Name => name;
public int Age => age;
}

public record RecordWithNoPrefix(
[Required] string name,
[Range(0, 120)] int age);

public class ClassWithNoPrefix(
[Required] string name,
[Range(0, 120)] int age)
{
public string Name => name;
public int Age => age;
}
}
Loading