From c6aa9b782c291ae4bdba93a52338cf6388699640 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 20:12:49 +0000 Subject: [PATCH] fix: a field of X or null says what X is A field's comment came from its property or from what the property refers to, but not from what a nullable property holds, so a field of anyOf [{"$ref": ...}, null] went without one. It now takes the description of the alternative to null, or of what that refers to. openapi-flatten moves up to the version that keeps such a property inline. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- go.mod | 2 +- go.sum | 4 +-- ir/schema.go | 7 ++++- ir/schema_test.go | 26 +++++++++++++++++++ .../MarkRosemaker/openapi-flatten/README.md | 4 +++ .../MarkRosemaker/openapi-flatten/schema.go | 23 ++++++++++++++++ vendor/modules.txt | 2 +- 7 files changed, 63 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 7f8db96c..34aed6d2 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/MarkRosemaker/openapi v0.0.0-20261113130942-537594635ea6 github.com/MarkRosemaker/openapi-compress v0.0.0-20261113131228-7b6bdcd65ed0 github.com/MarkRosemaker/openapi-edit v0.0.0-20261113131039-6e51bd94c90a - github.com/MarkRosemaker/openapi-flatten v0.0.0-20261113131039-558dca5cc800 + github.com/MarkRosemaker/openapi-flatten v0.0.0-20261113185414-039b4b0776ce github.com/MarkRosemaker/ordmap v0.0.0-20261113130919-027ff7850b50 github.com/ettle/strcase v0.2.0 github.com/go-api-libs/api v0.0.0-20261113130130-277524695333 diff --git a/go.sum b/go.sum index d2c25bdc..558c0a2f 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ github.com/MarkRosemaker/openapi-edit v0.0.0-20261113131039-6e51bd94c90a h1:mPvS github.com/MarkRosemaker/openapi-edit v0.0.0-20261113131039-6e51bd94c90a/go.mod h1:s1CNo5eZ9SoSjPXct1NozCbVHZy2mzpQ3pjRUaUcqtQ= github.com/MarkRosemaker/openapi-enrich v0.0.0-20261010200623-3b942fefdf03 h1:RHXZfeyefM1V/GaxZJ+RAUVGBpAtdb2u6k6f7grWLiw= github.com/MarkRosemaker/openapi-enrich v0.0.0-20261010200623-3b942fefdf03/go.mod h1:autrmEPWisPks3aJzJHcxKf8K6Mpk1Ph3jg1XywizDk= -github.com/MarkRosemaker/openapi-flatten v0.0.0-20261113131039-558dca5cc800 h1:HfWccZDJLpNFCesFvfBdKwTAXtiCfikX4LD6n86ziIA= -github.com/MarkRosemaker/openapi-flatten v0.0.0-20261113131039-558dca5cc800/go.mod h1:LCd6/pMJnpomikNTX3umKx+p1lUCqoKz08kQrqJTeqk= +github.com/MarkRosemaker/openapi-flatten v0.0.0-20261113185414-039b4b0776ce h1:Zh3CZXvuD3jdI83/2BO9s+q+KV2BECKroNyyK66IKa8= +github.com/MarkRosemaker/openapi-flatten v0.0.0-20261113185414-039b4b0776ce/go.mod h1:LCd6/pMJnpomikNTX3umKx+p1lUCqoKz08kQrqJTeqk= github.com/MarkRosemaker/openapi-merge v0.0.0-20261009220258-57c4bc8ba894 h1:fK6ld3zdSy1tJcVa7S9kixkn5e/IyLVQHowed2yLLOU= github.com/MarkRosemaker/openapi-merge v0.0.0-20261009220258-57c4bc8ba894/go.mod h1:JAAF3M82LuQyQ7+ByxhADsiy9e8ZxJQ8viAOsjrwS0c= github.com/MarkRosemaker/ordmap v0.0.0-20261113130919-027ff7850b50 h1:jcbQ0v5NTZIus/UhQi/YtJT21+9bOMnlqndDBzynnjE= diff --git a/ir/schema.go b/ir/schema.go index 3729d7a0..08f86cc0 100644 --- a/ir/schema.go +++ b/ir/schema.go @@ -576,12 +576,17 @@ func getField(jsonName string, propRef *openapi.Schema, requiredSet map[string]b tag = strings.TrimSuffix(tag, `"`) + `,string"` // written as the string it mostly is } + description := cmp.Or(propRef.Description, v.Description) + if nv := nullableVariant(v); description == "" && nv != nil { + description = cmp.Or(nv.Description, deref(nv).Description) // what X or null holds says what the field is + } + return Field{ Name: fieldName, JSONName: jsonName, Type: goType.String(), JSONTag: tag, - Description: cmp.Or(propRef.Description, v.Description), + Description: description, Required: required, ReadOnly: propRef.ReadOnly || v.ReadOnly, WriteOnly: propRef.WriteOnly || v.WriteOnly, diff --git a/ir/schema_test.go b/ir/schema_test.go index 49209fea..df0cee3b 100644 --- a/ir/schema_test.go +++ b/ir/schema_test.go @@ -258,6 +258,7 @@ func TestFromDocument_FieldTypesAndTags(t *testing.T) { "nullCount": {"oneOf": [{"type": "integer"}, {"type": "null"}]}, "nullName": {"oneOf": [{"type": "string"}, {"type": "null"}]}, "seed": {"anyOf": [{"type": "integer"}, {"type": "null"}], "default": 0}, + "nullLimit": {"anyOf": [{"type": "integer"}, {"type": "null"}]}, "nullArchived": {"anyOf": [{"type": "boolean"}, {"type": "null"}], "default": false}, "limit": {"type": "integer"}, "pageSize": {"type": "integer", "minimum": 1}, @@ -296,6 +297,7 @@ func TestFromDocument_FieldTypesAndTags(t *testing.T) { "tags": `[]string json:"tags"`, "nullCount": `int json:"nullCount"`, "seed": `int json:"seed,omitzero"`, + "nullLimit": `*int json:"nullLimit,omitzero"`, "nullArchived": `bool json:"nullArchived,omitzero"`, "nullName": `string json:"nullName"`, "limit": `*int json:"limit,omitzero"`, @@ -320,6 +322,30 @@ func TestFromDocument_FieldTypesAndTags(t *testing.T) { } } +func TestFromDocument_NullableRefDescription(t *testing.T) { + doc := loadSchemas(t, `{ + "Pet": { + "type": "object", + "properties": { + "owner": {"anyOf": [{"$ref": "#/components/schemas/Owner"}, {"type": "null"}]}, + "sitter": {"description": "Who looks after it", "anyOf": [{"$ref": "#/components/schemas/Owner"}, {"type": "null"}]}, + "vet": {"anyOf": [{"description": "Who treats it", "$ref": "#/components/schemas/Owner"}, {"type": "null"}]} + } + }, + "Owner": {"description": "A person", "type": "object", "required": ["id"], "properties": {"id": {"type": "string"}}} + }`) + + got := map[string]string{} + for _, f := range schemaNamed(t, doc, "Pet").Fields { + got[f.JSONName] = f.Description + } + + // a field of X or null says what X is, unless it or the reference to X says something of its own + if got["owner"] != "A person" || got["sitter"] != "Who looks after it" || got["vet"] != "Who treats it" { + t.Errorf("got %q", got) + } +} + func TestFromDocument_UnionFieldTypes(t *testing.T) { t.Parallel() diff --git a/vendor/github.com/MarkRosemaker/openapi-flatten/README.md b/vendor/github.com/MarkRosemaker/openapi-flatten/README.md index ff18938a..08596556 100644 --- a/vendor/github.com/MarkRosemaker/openapi-flatten/README.md +++ b/vendor/github.com/MarkRosemaker/openapi-flatten/README.md @@ -93,6 +93,10 @@ an object's keys (`propertyNames`) and the schema a value must not match (`not`) `allOf` thus ends up as a list of references, plus any entry with no shape of its own, such as `{"required": ["id"]}`. +A `oneOf` or `anyOf` of one schema and `null` is that schema, which may also be null: +it stays where it is, and only its schema is moved, by the same rules. A nullable +string stays inline, and a nullable object becomes `{"anyOf": [{"$ref": …}, {"type": "null"}]}`. + **Before:** ```json diff --git a/vendor/github.com/MarkRosemaker/openapi-flatten/schema.go b/vendor/github.com/MarkRosemaker/openapi-flatten/schema.go index bdbf07b3..0f80ea04 100644 --- a/vendor/github.com/MarkRosemaker/openapi-flatten/schema.go +++ b/vendor/github.com/MarkRosemaker/openapi-flatten/schema.go @@ -54,6 +54,10 @@ func inlineSchema(d *openapi.Document, s *openapi.Schema, name string, alwaysMov s = moveSchemaToComponents(d, name, s) } case "": // no explicit type — oneOf / anyOf / allOf composition or bare properties + if alt := nonNull(s); alt != nil { + return inlineSchema(d, alt, name, false) // X or null is X that may be null, not a shape of its own + } + v := s hasComposition := len(v.OneOf) > 0 || len(v.AnyOf) > 0 || len(v.AllOf) > 0 if hasComposition || len(v.Properties) > 0 { @@ -166,3 +170,22 @@ func inlineSchemaList(d *openapi.Document, ss openapi.SchemaList, prefix string, return nil } + +// nonNull is the alternative to null of a oneOf or anyOf of two, or nil where s is no such schema. +func nonNull(s *openapi.Schema) *openapi.Schema { + alts := s.AnyOf + if len(alts) == 0 { + alts = s.OneOf + } + + switch { + case len(alts) != 2 || len(s.AnyOf) > 0 && len(s.OneOf) > 0 || len(s.AllOf) > 0 || len(s.Properties) > 0: + return nil + case alts[1].Type == openapi.TypeNull && alts[0].Type != openapi.TypeNull: + return alts[0] + case alts[0].Type == openapi.TypeNull && alts[1].Type != openapi.TypeNull: + return alts[1] + default: + return nil + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index ebb21b6a..c87fe29e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -25,7 +25,7 @@ github.com/MarkRosemaker/openapi-compress # github.com/MarkRosemaker/openapi-edit v0.0.0-20261113131039-6e51bd94c90a ## explicit; go 1.27 github.com/MarkRosemaker/openapi-edit -# github.com/MarkRosemaker/openapi-flatten v0.0.0-20261113131039-558dca5cc800 +# github.com/MarkRosemaker/openapi-flatten v0.0.0-20261113185414-039b4b0776ce ## explicit; go 1.27 github.com/MarkRosemaker/openapi-flatten # github.com/MarkRosemaker/ordmap v0.0.0-20261113130919-027ff7850b50