diff --git a/.gitignore b/.gitignore index 81cc4e4..f639faf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ obj/ *.user artifacts/ TestResults/ +.DS_Store diff --git a/README.md b/README.md index f80bc16..1118822 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,14 @@ schemas then do not match the JSON that FSharp.SystemTextJson writes. This package adds schema and document transformers that read your `JsonFSharpOptions` and describe the real JSON: -- `option` and `voption`: nullable values +- `option` and `voption`: the inner value, and `null` for `option` record + fields - records: required and optional properties, including `Skippable<'T>` fields -- unions: tags, internal and external encodings, named fields, unwrapped cases, - fieldless cases as strings, and one component for each case -- tuples, F# collections (`list`, `Set`, `Map`), and `JsonNode` +- unions: tags, internal, adjacent and external encodings, named fields, + unwrapped cases, fieldless cases as strings, unions of only fieldless cases + as string enums, and one component for each case (except string-enum and + unwrapped single-case unions) +- tuples, F# collections (`list`, `Set`, `Map`), and `JsonObject` - enums: string and number encodings, also when nullable - per-type options from `[]` @@ -43,9 +46,9 @@ app.MapOpenApi() |> ignore `AddOpenApiForSystemTextJson` does three things: -1. It adds `fsOptions` to the JSON options of minimal APIs - (`Microsoft.AspNetCore.Http.Json.JsonOptions`). MVC controllers and Giraffe - use their own serializer options: add `fsOptions` there yourself. +1. It adds `fsOptions` to `Microsoft.AspNetCore.Http.Json.JsonOptions`. The + OpenAPI generator and minimal APIs read these options. MVC controllers and + Giraffe use their own serializer options: give them the same settings. 2. It registers the OpenAPI document (`v1` by default) with all transformers. 3. It sets the OpenAPI version to 3.1. @@ -54,11 +57,22 @@ name or to add your own transformers. To use only some transformers, add them to your own `AddOpenApi` call, for example `options.AddSchemaTransformer(UnionSchemaTransformer(fsOptions))`. -Then also add `fsOptions` to your serializer options yourself, and add the two -document transformers: `UnionCaseComponentsDocumentTransformer()` gives one +Then also add `fsOptions` to +`Microsoft.AspNetCore.Http.Json.JsonOptions`, and add the two document +transformers last: `UnionCaseComponentsDocumentTransformer()` gives one component for each union case, and `NullableEnumDocumentTransformer()` fixes nullable enums. +## Agent skill + +The repository has an [Agent Skills](https://agentskills.io) skill that teaches +coding agents (Claude Code, Codex, GitHub Copilot, Cursor and others) how to +use this package: + +```sh +npx skills add SelectViewData/SelectView.FSharp.SystemTextJson.OpenApi +``` + ## Build ```sh diff --git a/skills/fsharp-systemtextjson-openapi/SKILL.md b/skills/fsharp-systemtextjson-openapi/SKILL.md new file mode 100644 index 0000000..0247d47 --- /dev/null +++ b/skills/fsharp-systemtextjson-openapi/SKILL.md @@ -0,0 +1,99 @@ +--- +name: fsharp-systemtextjson-openapi +description: Use when an ASP.NET Core app with F# types serialized by FSharp.SystemTextJson needs an OpenAPI document that matches its JSON - setting up SelectView.FSharp.SystemTextJson.OpenApi, adding or changing JsonFSharpOptions or a [] attribute, or fixing an OpenAPI schema (or generated client) that differs from the JSON the app sends for options, records, unions, tuples, enums or F# collections. +--- + +# FSharp.SystemTextJson OpenAPI schemas + +`Microsoft.AspNetCore.OpenApi` describes F# types as plain .NET classes. The +package `SelectView.FSharp.SystemTextJson.OpenApi` adds schema and document +transformers that read your `JsonFSharpOptions`, so the OpenAPI schema +describes the JSON that FSharp.SystemTextJson writes. Requirements: .NET 10, +OpenAPI 3.1. + +## Set up + +```sh +dotnet add package SelectView.FSharp.SystemTextJson.OpenApi +``` + +```fsharp +open System.Text.Json.Serialization +open Microsoft.AspNetCore.Builder +open SelectView.FSharp.SystemTextJson.OpenApi + +let fsOptions = + JsonFSharpOptions.Default().WithUnionTagName("kind").WithUnionInternalTag() + +let builder = WebApplication.CreateBuilder() +builder.Services.AddOpenApiForSystemTextJson(fsOptions) |> ignore +let app = builder.Build() +app.MapOpenApi() |> ignore +``` + +`AddOpenApiForSystemTextJson(fsOptions, ?documentName, ?setup)` registers the +OpenAPI document (default `v1`, served at `/openapi/{documentName}.json`, +OpenAPI 3.1) with all transformers. It also adds `fsOptions` to +`Microsoft.AspNetCore.Http.Json.JsonOptions`, which the OpenAPI generator +reads. + +## Rules + +1. The serializer that writes the JSON and + `Microsoft.AspNetCore.Http.Json.JsonOptions` must have the same settings: + the same `fsOptions`, naming policy and ignore condition. If they differ, + the schema does not match the JSON. + - Minimal APIs: nothing more to do. + - MVC: in `AddControllers().AddJsonOptions(fun o -> ...)`, call + `fsOptions.AddToJsonSerializerOptions(o.JsonSerializerOptions)` and set + the same naming policy and ignore condition as in `JsonOptions`. + - Giraffe: register + `services.AddSingleton(Json.Serializer(jsonOptions))`, + where `jsonOptions` has the same settings and `fsOptions` added. +2. To change one type, put `[]` on that type and + call `WithAllowOverride()` on the global options. The transformers read + this attribute. Do not use `JsonFSharpOptions.WithOverrides`: the + serializer applies it, but the transformers do not, so the schema stays + wrong. +3. The attribute does not inherit the global options. Set every option that + the type needs (for example the union tag name) in the attribute. +4. If you add the transformers yourself with `AddOpenApi`, add `fsOptions` to + `JsonOptions`, and add the two document transformers + `UnionCaseComponentsDocumentTransformer()` and + `NullableEnumDocumentTransformer()` last, after your own document + transformers. +5. The transformers do not model `WithSkippableOptionFields` or `MapFormat`. + With these options the schema does not match the JSON. + +## What the schema shows + +- `option` record field: the inner schema plus `null`. Top-level `option` and + every `voption`: the inner schema only. +- With `UnwrapOption` turned off: `option` is `null` or a tagged `Some` + object; `voption` is a tagged `ValueSome` or `ValueNone` object. +- Records: a `Skippable<'T>` field is optional. With + `JsonIgnoreCondition.WhenWritingNull`, every field is optional. + `[]` makes a field required. +- Unions: the encoding from the options (internal tag, adjacent tag, external + tag, untagged, named fields, unwrapped record cases, unwrapped single-field + cases, unwrapped single-case unions). With `UnwrapFieldlessTags`, a + fieldless case becomes a string, and a union of only fieldless cases + becomes a string enum. Every other union gets one + component for each case, except a single-case union under + `UnwrapSingleCaseUnions`. +- A tuple becomes a fixed-length array. Its items schema allows any of the + element types; it does not fix the type of each position. +- `list` and `Set` become arrays. `Map` with `string` keys (or single-case + unions that wrap a string) becomes an object; other `Map` keys become an + array of `[key, value]` pairs. `JsonObject` becomes an object with any + properties. +- Enums use their string or number values, also when they are nullable. + +## Check + +When you change options or a type, compare the runtime JSON with the schema: + +1. Serialize a sample value with the app's serializer options. +2. Read `/openapi/{documentName}.json` and resolve the `$ref` of that type. +3. Every property and union tag in the JSON must be in the schema, and every + required property in the schema must be in the JSON.