Skip to main content

Adding Schema Constraint Attributes and Constraint-Aware AnyOf in C# v4 SDKs (Beta)

· 2 min read

Generated C# v4 SDKs now express your OpenAPI schema constraints directly on the generated models as declarative .NET validation attributes, so you can validate requests and responses using standard .NET validation. In addition, anyOf schemas that share a base type but differ by their constraints now generate a distinct arm for each schema.

Details

Schema constraint attributes

Constraints defined in your OpenAPI schemas, such as string lengths, patterns, numeric bounds, array rules, object property counts, and string formats, are now carried onto the generated model properties as attributes. Constraints covered by the .NET Base Class Library use the standard System.ComponentModel.DataAnnotations attributes, while the remaining constraints use attributes provided by the SDK under its Core.Validation.Attributes namespace.

The attributes are declarative: the SDK decorates the models for you, and you decide when to run validation using Validator.TryValidateObject or Validator.ValidateObject. This gives you a single, contract-driven source of truth for validating data before sending a request or after receiving a response, while keeping the models lightweight.

Learn more in the Schema Constraints feature guide.

Constraint-aware anyOf arms

When an anyOf lists multiple schemas that share the same base type but differ by their constraints, the SDK now represents each schema as its own distinct arm of the union, so every variant is preserved and available to work with individually. During (de)serialization, the SDK selects the arm whose constraints the value satisfies, making the handling of constrained union members precise and predictable. This behavior is enforced by the SDK.

Learn more in the OneOf and AnyOf feature guide.

This feature is available in the C# v4 SDK (beta). Generate it from the APIMatic CLI:

apimatic sdk generate --language=csharp --codegen-version=v4 --stability=beta

For the full set of flags and options, see the APIMatic CLI documentation.