From e2279a3f353f9c643373f61a99e67175837e1fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Krzywizna?= Date: Tue, 24 Nov 2026 10:32:47 +0100 Subject: [PATCH 1/4] [SEL-138] Add an agent skill for the package Co-Authored-By: Claude Opus 5.5 --- README.md | 10 +++ skills/fsharp-systemtextjson-openapi/SKILL.md | 88 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 skills/fsharp-systemtextjson-openapi/SKILL.md diff --git a/README.md b/README.md index f80bc16..0ccc20d 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,16 @@ document transformers: `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..b22e726 --- /dev/null +++ b/skills/fsharp-systemtextjson-openapi/SKILL.md @@ -0,0 +1,88 @@ +--- +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`, OpenAPI 3.1) with all transformers. It also +adds `fsOptions` to `Microsoft.AspNetCore.Http.Json.JsonOptions`, which only +minimal APIs read. + +## Rules + +1. Use one `JsonFSharpOptions` value for the serializer and for the OpenAPI + document. Two different values give a schema that does not match the JSON. +2. MVC controllers and Giraffe use their own serializer options. Add the same + `fsOptions` there too, for example + `fsOptions.AddToJsonSerializerOptions(options.JsonSerializerOptions)` in + `AddControllers().AddJsonOptions(...)`, or Giraffe's + `SystemTextJson.Serializer` built from options that include `fsOptions`. +3. 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. +4. The attribute does not inherit the global options. Set every option that + the type needs (for example the union tag name) in the attribute. +5. If you add the transformers yourself with `AddOpenApi`, also add the two + document transformers `UnionCaseComponentsDocumentTransformer()` and + `NullableEnumDocumentTransformer()`, and add `fsOptions` to the serializer. + +## What the schema shows + +- `option` and `voption`: the inner schema plus `null`. With + `UnwrapOption` turned off, a tagged union with `Some` and `None`. +- 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-case + unions). Fieldless cases become strings with `UnwrapFieldlessTags`. Each + case gets its own component. +- 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. `JsonNode` accepts any value. +- 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/v1.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. + +Keep a committed snapshot of the OpenAPI document and fail the build when it +changes without review. Generated clients (for example openapi-typescript) +then change only on purpose. From 8bd2c5d89bb3f252735b00f2f2fcd0796ac6c14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Krzywizna?= Date: Tue, 24 Nov 2026 10:45:21 +0100 Subject: [PATCH 2/4] [SEL-138] Fix the agent skill and README after review Correct option and voption shapes, per-case components, the Giraffe 8 serializer API, which options the OpenAPI generator reads, the document route, and list the options the transformers do not model. Co-Authored-By: Claude Opus 5.5 --- README.md | 9 +-- skills/.DS_Store | Bin 0 -> 6148 bytes skills/fsharp-systemtextjson-openapi/SKILL.md | 65 +++++++++++------- 3 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 skills/.DS_Store diff --git a/README.md b/README.md index 0ccc20d..eb27b40 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ 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 @@ -43,9 +44,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. diff --git a/skills/.DS_Store b/skills/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1df8486cd5538ac721d34544cbe111042fe4c520 GIT binary patch literal 6148 zcmeHKQA^xF5S~3xO%UN86htTl`fBOrK~M1|oaY~CO8d~3GvQK=#_Vy)c`e65et`a= z{*eBjezUt*Dz;BTwPglozS-TG-Q=6FnMJ4iR}!#+DEB8{u)*2hwsqjLOV0 zp)tLpioQZc{sjiOb}#5N>UBX0{aU}8GP)+VUtlj0p{Yc5HENu}OMrPgtHx=eQ_S)$ z9T$03ce~$JYo)!qw(fb}hW9DH(6hKs>S@(aCLg%3bBrmgx zNytYTLO#CF%aNY-^|TySCO5VNp6~m^&hC63?st28!BKdy*bC-iFFZt}+gmJrZ~Nuz z<2S>r;=0tgwh#*6sF6*FOSpsc2o7%INm=Of9J4G}Sk5dc3 ignore ``` `AddOpenApiForSystemTextJson(fsOptions, ?documentName, ?setup)` registers the -OpenAPI document (default `v1`, OpenAPI 3.1) with all transformers. It also -adds `fsOptions` to `Microsoft.AspNetCore.Http.Json.JsonOptions`, which only -minimal APIs read. +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`. + +The OpenAPI generator builds every schema from +`Microsoft.AspNetCore.Http.Json.JsonOptions` (naming policy, ignore condition, +`fsOptions`). Minimal APIs serialize with the same options. MVC controllers +and Giraffe serialize with their own options. ## Rules -1. Use one `JsonFSharpOptions` value for the serializer and for the OpenAPI - document. Two different values give a schema that does not match the JSON. -2. MVC controllers and Giraffe use their own serializer options. Add the same - `fsOptions` there too, for example - `fsOptions.AddToJsonSerializerOptions(options.JsonSerializerOptions)` in - `AddControllers().AddJsonOptions(...)`, or Giraffe's - `SystemTextJson.Serializer` built from options that include `fsOptions`. -3. To change one type, put `[]` on that type and +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. -4. The attribute does not inherit the global options. Set every option that +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. -5. If you add the transformers yourself with `AddOpenApi`, also add the two - document transformers `UnionCaseComponentsDocumentTransformer()` and - `NullableEnumDocumentTransformer()`, and add `fsOptions` to the serializer. +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` and `voption`: the inner schema plus `null`. With - `UnwrapOption` turned off, a tagged union with `Some` and `None`. +- `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-case - unions). Fieldless cases become strings with `UnwrapFieldlessTags`. Each - case gets its own component. + tag, untagged, named fields, unwrapped record cases, unwrapped single-field + cases, unwrapped single-case unions). With `UnwrapFieldlessTags`, a union of + only fieldless cases becomes a string enum. Other unions with more than one + case get one component for each case. - 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. `JsonNode` accepts any value. + 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 @@ -79,10 +96,6 @@ minimal APIs read. 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/v1.json` and resolve the `$ref` of that type. +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. - -Keep a committed snapshot of the OpenAPI document and fail the build when it -changes without review. Generated clients (for example openapi-typescript) -then change only on purpose. From 182afb260ec6df82842fb416e2c636b06d2ef8a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Krzywizna?= Date: Tue, 24 Nov 2026 10:47:06 +0100 Subject: [PATCH 3/4] [SEL-138] Align the README with the skill; correct the union component rule Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + README.md | 11 ++++++----- skills/.DS_Store | Bin 6148 -> 0 bytes skills/fsharp-systemtextjson-openapi/SKILL.md | 10 ++++------ 4 files changed, 11 insertions(+), 11 deletions(-) delete mode 100644 skills/.DS_Store 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 eb27b40..23383fb 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,10 @@ package adds schema and document transformers that read your - `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, unions of fieldless cases as string enums, and one component + for each case +- tuples, F# collections (`list`, `Set`, `Map`), and `JsonObject` - enums: string and number encodings, also when nullable - per-type options from `[]` @@ -55,8 +56,8 @@ 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. diff --git a/skills/.DS_Store b/skills/.DS_Store deleted file mode 100644 index 1df8486cd5538ac721d34544cbe111042fe4c520..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKQA^xF5S~3xO%UN86htTl`fBOrK~M1|oaY~CO8d~3GvQK=#_Vy)c`e65et`a= z{*eBjezUt*Dz;BTwPglozS-TG-Q=6FnMJ4iR}!#+DEB8{u)*2hwsqjLOV0 zp)tLpioQZc{sjiOb}#5N>UBX0{aU}8GP)+VUtlj0p{Yc5HENu}OMrPgtHx=eQ_S)$ z9T$03ce~$JYo)!qw(fb}hW9DH(6hKs>S@(aCLg%3bBrmgx zNytYTLO#CF%aNY-^|TySCO5VNp6~m^&hC63?st28!BKdy*bC-iFFZt}+gmJrZ~Nuz z<2S>r;=0tgwh#*6sF6*FOSpsc2o7%INm=Of9J4G}Sk5dc3 Date: Tue, 24 Nov 2026 10:48:03 +0100 Subject: [PATCH 4/4] [SEL-138] Skill and README wording from the last review notes Co-Authored-By: Claude Opus 5.5 --- README.md | 10 ++++++---- skills/fsharp-systemtextjson-openapi/SKILL.md | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 23383fb..1118822 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,9 @@ package adds schema and document transformers that read your fields - records: required and optional properties, including `Skippable<'T>` fields - unions: tags, internal, adjacent and external encodings, named fields, - unwrapped cases, unions of fieldless cases as string enums, and one component - for each case + 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 `[]` @@ -56,8 +57,9 @@ 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 `Microsoft.AspNetCore.Http.Json.JsonOptions`, and -add the two document transformers last: `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. diff --git a/skills/fsharp-systemtextjson-openapi/SKILL.md b/skills/fsharp-systemtextjson-openapi/SKILL.md index f5cd986..0247d47 100644 --- a/skills/fsharp-systemtextjson-openapi/SKILL.md +++ b/skills/fsharp-systemtextjson-openapi/SKILL.md @@ -34,9 +34,8 @@ 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`. - -The OpenAPI generator reads `Microsoft.AspNetCore.Http.Json.JsonOptions`. +`Microsoft.AspNetCore.Http.Json.JsonOptions`, which the OpenAPI generator +reads. ## Rules @@ -77,8 +76,9 @@ The OpenAPI generator reads `Microsoft.AspNetCore.Http.Json.JsonOptions`. `[]` 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 union of - only fieldless cases becomes a string enum. Every other union gets one + 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