json-api-dotnet / JsonApiDotNetCore

A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.
https://www.jsonapi.net
MIT License
662 stars 160 forks source link

Surface type hints in 'id' properties and parameters #1572

Open bkoelman opened 2 weeks ago

bkoelman commented 2 weeks ago

Currently, all id parameters are declared as type: string in the emitted openapi.json. Adding type hints like format: uuid enables client generators to produce more specific types.

Implementation notes

Because all id properties are declared as string in the generic models, we need a custom schema generator that calls the default schema generator on the actual ID type in the resource graph, ie:

OpenApiSchema idSchema = _defaultSchemaGenerator.GenerateSchema(identityClrType, schemaRepository);

fullSchemaForDerivedType.Properties[JsonApiPropertyName.Id].Format = idSchema.Format;
// Possibly take more fields, but Type needs to remain `string`.

The custom schema generator needs to be called from all places where IDs are used, which includes resource objects, resource identifier objects, and action method parameters.

For atomic operations, the same applies to lid. The replacement additionally needs to run on relationship identifier objects (ref).