From 5731ab26c34ef6f3cf3ce054e67f324fff078893 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 01:46:49 +0000 Subject: [PATCH] feat: bump openapi-flatten, which names a list of a component Flatten now moves an inline array of a component to the components as the component's plural, so the generated code says Entries, Members, PropertyFilters and ScheduleFormats rather than []Entry, []Member, []PropertyFilter and []ScheduleFormat. The golden and debug outputs are updated to match. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- go.mod | 2 +- go.sum | 4 +- testdata/debug/pkg/debug/client.gen.go | 7 +-- testdata/debug/pkg/debug/client.gen_test.go | 3 +- testdata/debug/pkg/debug/ir.json | 26 ++++++-- testdata/debug/pkg/debug/server.gen.go | 2 +- testdata/debug/pkg/debug/types.gen.go | 22 ++++++- testdata/golden/pkg/golden/client.gen.go | 4 +- testdata/golden/pkg/golden/client.gen_test.go | 2 +- testdata/golden/pkg/golden/ir.json | 56 ++++++++++++++--- testdata/golden/pkg/golden/server.gen.go | 2 +- testdata/golden/pkg/golden/types.gen.go | 30 +++++++-- .../MarkRosemaker/openapi-flatten/README.md | 3 + .../MarkRosemaker/openapi-flatten/schema.go | 61 ++++++++++++++++++- vendor/modules.txt | 2 +- 15 files changed, 186 insertions(+), 40 deletions(-) diff --git a/go.mod b/go.mod index d1a13906..497a94ec 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/MarkRosemaker/openapi v0.0.0-20261125230352-82ba4731490f github.com/MarkRosemaker/openapi-compress v0.0.0-20261125230939-3463d8d36c19 github.com/MarkRosemaker/openapi-edit v0.0.0-20261125230638-5edf4d1ada4d - github.com/MarkRosemaker/openapi-flatten v0.0.0-20261125230637-977f667d3678 + github.com/MarkRosemaker/openapi-flatten v0.0.0-20261126014520-55785d534cc0 github.com/MarkRosemaker/ordmap v0.0.0-20261124050216-f3096eb875be github.com/ettle/strcase v0.2.0 github.com/go-api-libs/api v0.0.0-20261125182139-0f507257dcd7 diff --git a/go.sum b/go.sum index a9c3d786..013bc0ce 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ github.com/MarkRosemaker/openapi-edit v0.0.0-20261125230638-5edf4d1ada4d h1:mv2v github.com/MarkRosemaker/openapi-edit v0.0.0-20261125230638-5edf4d1ada4d/go.mod h1:3d13fyJymG4ynXmgRkhmeGloZshaJUlH32giNB+m+nM= 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-20261125230637-977f667d3678 h1:VwwRIao7pLHzLcOiY2+FZ7nD5pQZRYvkTYi+I6n1t9o= -github.com/MarkRosemaker/openapi-flatten v0.0.0-20261125230637-977f667d3678/go.mod h1:r/shu1ltq5ZRcbrj1T+u3nQNq8VhANp4i5k8HulMl7E= +github.com/MarkRosemaker/openapi-flatten v0.0.0-20261126014520-55785d534cc0 h1:avLToMMtZraXvqmrDWImDS8cOJBDj7s1Z0hwUFFqf/w= +github.com/MarkRosemaker/openapi-flatten v0.0.0-20261126014520-55785d534cc0/go.mod h1:r/shu1ltq5ZRcbrj1T+u3nQNq8VhANp4i5k8HulMl7E= 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-20261124050216-f3096eb875be h1:yz+tf9ONFOoo2hDWkRSjP9NZSvWyS986bs/WAy+jNus= diff --git a/testdata/debug/pkg/debug/client.gen.go b/testdata/debug/pkg/debug/client.gen.go index 76530c93..bd414c88 100644 --- a/testdata/debug/pkg/debug/client.gen.go +++ b/testdata/debug/pkg/debug/client.gen.go @@ -13,7 +13,6 @@ import ( "net/http" "net/url" "os" - "slices" "strings" "github.com/MarkRosemaker/cassette" @@ -183,7 +182,7 @@ func (c *Client) GetItem(ctx context.Context) (*Item, error) { } // GET /items -func (c *Client) ListItems(ctx context.Context) ([]Item, error) { +func (c *Client) ListItems(ctx context.Context) (Items, error) { u := c.baseURL.JoinPath("items") req := (&http.Request{ Header: http.Header{ @@ -210,7 +209,7 @@ func (c *Client) ListItems(ctx context.Context) ([]Item, error) { // ok switch mt, _, _ := strings.Cut(rsp.Header.Get("Content-Type"), ";"); mt { case "application/json": - var out []Item + var out Items if err := json.UnmarshalRead(rsp.Body, &out, jsonOpts); err != nil { if err := api.DecodeLenient(rsp, err, ia, cmp.Or(c.debugFile, defaultDebugFile), &out, jsonOpts); err != nil { return nil, err @@ -218,7 +217,7 @@ func (c *Client) ListItems(ctx context.Context) ([]Item, error) { } // a value of an enum the specification does not list decodes, but is news - if ia != nil && slices.ContainsFunc(out, func(x Item) bool { return x.unknownEnum() }) { + if ia != nil && out.unknownEnum() { if err := c.keep(ia); err != nil { return nil, fmt.Errorf("recording the call: %w", err) } diff --git a/testdata/debug/pkg/debug/client.gen_test.go b/testdata/debug/pkg/debug/client.gen_test.go index 32f8a8fe..139c4042 100644 --- a/testdata/debug/pkg/debug/client.gen_test.go +++ b/testdata/debug/pkg/debug/client.gen_test.go @@ -15,7 +15,6 @@ import ( "net/url" "os" "path/filepath" - "slices" "testing" "github.com/MarkRosemaker/cassette" @@ -728,7 +727,7 @@ func TestClient_Interactions(t *testing.T) { } else { apitest.DecodedAsSent(t, "ListItems", out, last.Body, jsonOpts) - if slices.ContainsFunc(out, func(x Item) bool { return x.unknownEnum() }) { + if out.unknownEnum() { t.Errorf("ListItems: holds a value of an enum the specification does not list") } } diff --git a/testdata/debug/pkg/debug/ir.json b/testdata/debug/pkg/debug/ir.json index ed145f52..acbe1969 100644 --- a/testdata/debug/pkg/debug/ir.json +++ b/testdata/debug/pkg/debug/ir.json @@ -57,15 +57,15 @@ "description": "ok", "contentType": "application/json", "goType": { - "name": "Item", - "isSlice": true + "name": "Items", + "isNilable": true }, "isSuccess": true } ], "successReturn": { - "name": "Item", - "isSlice": true + "name": "Items", + "isNilable": true } }, { @@ -282,6 +282,13 @@ } ] }, + { + "name": "Events", + "description": "Events defines a model", + "kind": 2, + "type": "[]Event", + "unknownEnum": "slices.ContainsFunc(v, func(x Event) bool { return x.unknownEnum() })" + }, { "name": "Item", "description": "Item defines a model", @@ -311,7 +318,7 @@ { "name": "Events", "jsonName": "events", - "type": "[]Event", + "type": "Events", "jsonTag": "json:\"events,omitzero\"" }, { @@ -321,7 +328,14 @@ "jsonTag": "json:\"labels,omitzero\"" } ], - "unknownEnum": "v.Status != \"\" && !v.Status.Valid() ||\nslices.ContainsFunc(v.Events, func(x Event) bool { return x.unknownEnum() }) ||\nfunc() bool {\nfor _, x := range v.Labels {\nif !x.Valid() {\nreturn true\n}\n}\n\nreturn false\n}()" + "unknownEnum": "v.Status != \"\" && !v.Status.Valid() ||\nv.Events.unknownEnum() ||\nfunc() bool {\nfor _, x := range v.Labels {\nif !x.Valid() {\nreturn true\n}\n}\n\nreturn false\n}()" + }, + { + "name": "Items", + "description": "Items defines a model", + "kind": 2, + "type": "[]Item", + "unknownEnum": "slices.ContainsFunc(v, func(x Item) bool { return x.unknownEnum() })" }, { "name": "Pet", diff --git a/testdata/debug/pkg/debug/server.gen.go b/testdata/debug/pkg/debug/server.gen.go index 211e54d9..9267f61e 100644 --- a/testdata/debug/pkg/debug/server.gen.go +++ b/testdata/debug/pkg/debug/server.gen.go @@ -17,7 +17,7 @@ import ( // Service defines the operations the server must implement. type Service interface { GetItem(ctx context.Context) (*Item, error) - ListItems(ctx context.Context) ([]Item, error) + ListItems(ctx context.Context) (Items, error) GetBlock(ctx context.Context) (*Block, error) GetPet(ctx context.Context) (*Pet, error) Ping(ctx context.Context) error diff --git a/testdata/debug/pkg/debug/types.gen.go b/testdata/debug/pkg/debug/types.gen.go index 0b7ef24e..ac8a42eb 100644 --- a/testdata/debug/pkg/debug/types.gen.go +++ b/testdata/debug/pkg/debug/types.gen.go @@ -185,6 +185,15 @@ func (e EventKind) Valid() bool { } } +// Events defines a model +type Events []Event + +// unknownEnum reports whether v holds a value of an enum the specification does not list, a response debug mode +// records. +func (v Events) unknownEnum() bool { + return slices.ContainsFunc(v, func(x Event) bool { return x.unknownEnum() }) +} + // Item defines a model type Item struct { ID string `json:"id"` @@ -192,7 +201,7 @@ type Item struct { Updated civil.Date `json:"updated,omitzero"` // A value the specification does not list decodes, and debug mode records it. Status Status `json:"status,omitzero"` - Events []Event `json:"events,omitzero"` + Events Events `json:"events,omitzero"` Labels map[string]Status `json:"labels,omitzero"` } @@ -200,7 +209,7 @@ type Item struct { // records. func (v *Item) unknownEnum() bool { return v.Status != "" && !v.Status.Valid() || - slices.ContainsFunc(v.Events, func(x Event) bool { return x.unknownEnum() }) || + v.Events.unknownEnum() || func() bool { for _, x := range v.Labels { if !x.Valid() { @@ -212,6 +221,15 @@ func (v *Item) unknownEnum() bool { }() } +// Items defines a model +type Items []Item + +// unknownEnum reports whether v holds a value of an enum the specification does not list, a response debug mode +// records. +func (v Items) unknownEnum() bool { + return slices.ContainsFunc(v, func(x Item) bool { return x.unknownEnum() }) +} + // Pet defines a model // Pet is an untagged oneOf union: exactly one field is set after unmarshaling. type Pet struct { diff --git a/testdata/golden/pkg/golden/client.gen.go b/testdata/golden/pkg/golden/client.gen.go index 12f659cd..f79781a0 100644 --- a/testdata/golden/pkg/golden/client.gen.go +++ b/testdata/golden/pkg/golden/client.gen.go @@ -153,7 +153,7 @@ func (c *Client) GetPage(ctx context.Context, id string) (*Page, error) { } // POST /search -func (c *Client) Search(ctx context.Context, body PropertyFilter) ([]PageOrPartial, error) { +func (c *Client) Search(ctx context.Context, body PropertyFilter) (PageOrPartials, error) { u := c.baseURL.JoinPath("search") pr, pw := io.Pipe() req := (&http.Request{ @@ -186,7 +186,7 @@ func (c *Client) Search(ctx context.Context, body PropertyFilter) ([]PageOrParti // ok switch mt, _, _ := strings.Cut(rsp.Header.Get("Content-Type"), ";"); mt { case "application/json": - var out []PageOrPartial + var out PageOrPartials if err := json.UnmarshalRead(rsp.Body, &out, jsonOpts); err != nil { return nil, api.WrapDecodingError(rsp, err) } diff --git a/testdata/golden/pkg/golden/client.gen_test.go b/testdata/golden/pkg/golden/client.gen_test.go index bbcb6484..ce5b0d83 100644 --- a/testdata/golden/pkg/golden/client.gen_test.go +++ b/testdata/golden/pkg/golden/client.gen_test.go @@ -2256,7 +2256,7 @@ func TestClient_Params(t *testing.T) { IDs: []int{2}, Verbose: true, Format: ScheduleFormatCsv, - Formats: []ScheduleFormat{ScheduleFormatCsv}, + Formats: ScheduleFormats{ScheduleFormatCsv}, Tags: []string{""}, Cursor: "cursor", Order: "order", diff --git a/testdata/golden/pkg/golden/ir.json b/testdata/golden/pkg/golden/ir.json index 41221c95..22981a57 100644 --- a/testdata/golden/pkg/golden/ir.json +++ b/testdata/golden/pkg/golden/ir.json @@ -99,15 +99,15 @@ "description": "ok", "contentType": "application/json", "goType": { - "name": "PageOrPartial", - "isSlice": true + "name": "PageOrPartials", + "isNilable": true }, "isSuccess": true } ], "successReturn": { - "name": "PageOrPartial", - "isSlice": true + "name": "PageOrPartials", + "isNilable": true } }, { @@ -561,7 +561,7 @@ "goName": "formats", "fieldName": "Formats", "jsonName": "formats", - "type": "[]ScheduleFormat", + "type": "ScheduleFormats", "description": "A list of an enum.", "item": { "varName": "v", @@ -1899,7 +1899,7 @@ { "name": "Or", "jsonName": "or", - "type": "[]PropertyFilter", + "type": "PropertyFilters", "jsonTag": "json:\"or\"", "required": true } @@ -2186,6 +2186,12 @@ "name": "EmptyObject", "description": "EmptyObject defines a model" }, + { + "name": "Entries", + "description": "Entries defines a model", + "kind": 2, + "type": "[]Entry" + }, { "name": "Entry", "description": "Entry defines a model", @@ -2715,7 +2721,7 @@ { "name": "Results", "jsonName": "results", - "type": "[]Entry", + "type": "Entries", "jsonTag": "json:\"results\"", "required": true }, @@ -2742,7 +2748,7 @@ { "name": "Results", "jsonName": "results", - "type": "[]PeopleArrayBasedPropertyValueResponsePeopleItem", + "type": "PeopleArrayBasedPropertyValueResponsePeopleItems", "jsonTag": "json:\"results,omitzero\"" }, { @@ -2762,7 +2768,7 @@ { "name": "Members", "jsonName": "members", - "type": "[]Member", + "type": "Members", "jsonTag": "json:\"members,omitzero\"" } ] @@ -3138,6 +3144,12 @@ ], "memberDecoder": true }, + { + "name": "Members", + "description": "Members defines a model", + "kind": 2, + "type": "[]Member" + }, { "name": "MembershipType", "description": "MembershipType defines a model", @@ -3819,6 +3831,12 @@ } ] }, + { + "name": "PageOrPartials", + "description": "PageOrPartials defines a model", + "kind": 2, + "type": "[]PageOrPartial" + }, { "name": "Pair", "description": "Pair defines a model", @@ -4032,6 +4050,12 @@ ], "isOneOf": true }, + { + "name": "PeopleArrayBasedPropertyValueResponsePeopleItems", + "description": "PeopleArrayBasedPropertyValueResponsePeopleItems defines a model", + "kind": 2, + "type": "[]PeopleArrayBasedPropertyValueResponsePeopleItem" + }, { "name": "PersonGroup", "description": "PersonGroup defines a model", @@ -4197,6 +4221,12 @@ ] } }, + { + "name": "PropertyFilters", + "description": "PropertyFilters defines a model", + "kind": 2, + "type": "[]PropertyFilter" + }, { "name": "PropertyValue", "description": "PropertyValue defines a model", @@ -4297,7 +4327,7 @@ { "name": "Items", "jsonName": "items", - "type": "[]Entry", + "type": "Entries", "jsonTag": "json:\"items\"", "required": true }, @@ -4555,6 +4585,12 @@ } ] }, + { + "name": "ScheduleFormats", + "description": "ScheduleFormats defines a model", + "kind": 2, + "type": "[]ScheduleFormat" + }, { "name": "ScheduleOptions", "description": "Leaving it out already means {}: no pointer.", diff --git a/testdata/golden/pkg/golden/server.gen.go b/testdata/golden/pkg/golden/server.gen.go index 97bd5479..699f69b7 100644 --- a/testdata/golden/pkg/golden/server.gen.go +++ b/testdata/golden/pkg/golden/server.gen.go @@ -24,7 +24,7 @@ import ( // Service defines the operations the server must implement. type Service interface { GetPage(ctx context.Context, id string) (*Page, error) - Search(ctx context.Context, body PropertyFilter) ([]PageOrPartial, error) + Search(ctx context.Context, body PropertyFilter) (PageOrPartials, error) GetTrial(ctx context.Context, id string) (*Trial, error) UpdateUser(ctx context.Context, id string, body User) (*User, error) PatchUser(ctx context.Context, id string, body *User) error diff --git a/testdata/golden/pkg/golden/types.gen.go b/testdata/golden/pkg/golden/types.gen.go index 0970d97f..71e4efb7 100644 --- a/testdata/golden/pkg/golden/types.gen.go +++ b/testdata/golden/pkg/golden/types.gen.go @@ -117,7 +117,7 @@ type ListReportsParams struct { // An enum. Format ScheduleFormat // A list of an enum. - Formats []ScheduleFormat + Formats ScheduleFormats // A union of an array and its items is sent as the array. Tags []string // A query string cannot carry null, so the string is all that is left. @@ -530,7 +530,7 @@ type Color struct { // CompoundFilter defines a model type CompoundFilter struct { - Or []PropertyFilter `json:"or"` + Or PropertyFilters `json:"or"` } // Condition defines a model @@ -766,6 +766,9 @@ type Emoji struct { // EmptyObject defines a model type EmptyObject struct{} +// Entries defines a model +type Entries []Entry + // Entry defines a model // Entry is an untagged oneOf union: exactly one field is set after unmarshaling. type Entry struct { @@ -1190,19 +1193,19 @@ type Link struct { // ListEntriesOk defines a model type ListEntriesOk struct { - Results []Entry `json:"results"` + Results Entries `json:"results"` NextCursor uuid.UUID `json:"next_cursor"` HasMore bool `json:"has_more"` } // ListPeopleOk defines a model type ListPeopleOk struct { - Results []PeopleArrayBasedPropertyValueResponsePeopleItem `json:"results,omitzero"` + Results PeopleArrayBasedPropertyValueResponsePeopleItems `json:"results,omitzero"` // A union that can be null of alternatives a discriminator tells apart: being null comes first. Owner Owner `json:"owner,omitzero"` // A union that can be null, of a tagged union and an object: no field means null, so no discriminator can decide first. Value MaybeMember `json:"value,omitzero"` - Members []Member `json:"members,omitzero"` + Members Members `json:"members,omitzero"` } // A union that can be null, of a tagged union and an object: no field means null, so no discriminator can decide first. @@ -1505,6 +1508,9 @@ func (v *MemberPerson) unmarshalJSONMember(dec *jsontext.Decoder, name string) ( return false, nil } +// Members defines a model +type Members []Member + // MembershipType defines a model type MembershipType string @@ -2017,6 +2023,9 @@ func (v *PageOrPartial) MarshalJSONTo(enc *jsontext.Encoder) error { return enc.WriteToken(jsontext.Null) } +// PageOrPartials defines a model +type PageOrPartials []PageOrPartial + // Pair defines a model type Pair struct { Item0 string @@ -2235,6 +2244,9 @@ func (v *PeopleArrayBasedPropertyValueResponsePeopleItem) MarshalJSONTo(enc *jso return enc.WriteToken(jsontext.Null) } +// PeopleArrayBasedPropertyValueResponsePeopleItems defines a model +type PeopleArrayBasedPropertyValueResponsePeopleItems []PeopleArrayBasedPropertyValueResponsePeopleItem + // PersonGroup defines a model type PersonGroup struct { Type PersonGroupType `json:"type"` @@ -2425,6 +2437,9 @@ func (v *PropertyFilter) MarshalJSONTo(enc *jsontext.Encoder) error { return json.MarshalEncode(enc, (*plain)(&out), jsonOptsTo(enc)) } +// PropertyFilters defines a model +type PropertyFilters []PropertyFilter + // PropertyValue defines a model type PropertyValue struct { Type PropertyValueType `json:"type"` @@ -2523,7 +2538,7 @@ type QueryEntries struct { // QueryEntriesOk defines a model type QueryEntriesOk struct { - Items []Entry `json:"items"` + Items Entries `json:"items"` NextPageToken string `json:"next_page_token"` } @@ -2607,6 +2622,9 @@ func (e ScheduleFormat) Valid() bool { } } +// ScheduleFormats defines a model +type ScheduleFormats []ScheduleFormat + // Leaving it out already means {}: no pointer. type ScheduleOptions struct { Timezone string `json:"timezone,omitzero"` diff --git a/vendor/github.com/MarkRosemaker/openapi-flatten/README.md b/vendor/github.com/MarkRosemaker/openapi-flatten/README.md index 006ae148..e415bccf 100644 --- a/vendor/github.com/MarkRosemaker/openapi-flatten/README.md +++ b/vendor/github.com/MarkRosemaker/openapi-flatten/README.md @@ -86,6 +86,9 @@ Inline schemas are moved to `components/schemas` when they contain meaningful st - an **object** with properties - a **string** or **array of strings** with `enum` values - an **array of objects** +- an **array of a component**, named the component's plural: a list of `Order` becomes + `Orders`, or refers to an `Orders` that already lists `Order` and nothing else, the + list's description then staying beside the reference The entries of an `allOf` and the alternatives of a `oneOf` or `anyOf` are moved by the same rules, so each shaped one gets a name of its own, and so are the schema of diff --git a/vendor/github.com/MarkRosemaker/openapi-flatten/schema.go b/vendor/github.com/MarkRosemaker/openapi-flatten/schema.go index 7ffabc03..92874b54 100644 --- a/vendor/github.com/MarkRosemaker/openapi-flatten/schema.go +++ b/vendor/github.com/MarkRosemaker/openapi-flatten/schema.go @@ -1,7 +1,10 @@ package flatten import ( + "bytes" + "encoding/json/v2" "fmt" + "strings" "github.com/MarkRosemaker/errpath" "github.com/MarkRosemaker/openapi" @@ -29,7 +32,12 @@ func inlineSchema(d *openapi.Document, s *openapi.Schema, name string, alwaysMov // a tuple has a defined shape, positional but no less real than // an object's properties: give it a name of its own the same way. s = moveSchemaToComponents(d, name, s) - } else if items := s.Items; items != nil { // no items when only ever seen empty + } else if items := s.Items; items != nil && items.Ref != nil { + // a list of a component is named after it, so the component's list reads the same wherever it is + if item, ok := strings.CutPrefix(items.Ref.Identifier, "#/components/schemas/"); ok { + s = moveListToComponents(d, s, item) + } + } else if items != nil { // no items when only ever seen empty switch items.Type { case openapi.TypeInteger: // do nothing, just []int case openapi.TypeNumber: // do nothing, just []float32 or []float64 @@ -157,6 +165,57 @@ func moveSchemaToComponents(d *openapi.Document, name string, s *openapi.Schema) return &moved } +// moveListToComponents makes s, a list of the component schema item, a reference to the component listing it, which is +// named the plural of item: an existing one, if it lists item and nothing else, or else a new one. It returns the +// component. +func moveListToComponents(d *openapi.Document, s *openapi.Schema, item string) *openapi.Schema { + name := plural(item) + + if existing, ok := d.Components.Schemas[name]; ok && isListOf(existing, newRef("schemas", item).Identifier) { + s.Replace(&openapi.Schema{ + Ref: &openapi.SchemaRef{Identifier: newRef("schemas", name).Identifier, Value: existing}, + Description: s.Description, + }) + + return existing + } + + return moveSchemaToComponents(d, name, s) +} + +// isListOf reports whether s is a list of the component schema at ref and nothing else, but for what it says of itself: +// its title, description, examples and extensions. +func isListOf(s *openapi.Schema, ref string) bool { + if s.Type != openapi.TypeArray || s.Items == nil || s.Items.Ref == nil || s.Items.Ref.Identifier != ref { + return false + } + + rest := *s + rest.Title, rest.Description, rest.Example, rest.Examples, rest.Extensions = "", "", nil, nil, nil + + got, err := json.Marshal(&rest) + if err != nil { + return false + } + + want, err := json.Marshal(&openapi.Schema{Type: s.Type, Items: s.Items}) + + return err == nil && bytes.Equal(got, want) +} + +// plural is the English plural of a name, such as Orders of Order, Entries of Entry and Statuses of Status. +func plural(name string) string { + switch { + case strings.HasSuffix(name, "s"), strings.HasSuffix(name, "x"), strings.HasSuffix(name, "z"), + strings.HasSuffix(name, "ch"), strings.HasSuffix(name, "sh"): + return name + "es" + case strings.HasSuffix(name, "y") && len(name) > 1 && !strings.ContainsRune("aeiouAEIOU", rune(name[len(name)-2])): + return name[:len(name)-1] + "ies" + default: + return name + "s" + } +} + // inlineBranches flattens the alternatives of a oneOf or anyOf, naming each by its title, or else by prefix and position. func inlineBranches(d *openapi.Document, ss openapi.SchemaList, prefix string) error { for i, s := range ss { diff --git a/vendor/modules.txt b/vendor/modules.txt index a25f3a2c..3172f392 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-20261125230638-5edf4d1ada4d ## explicit; go 1.27 github.com/MarkRosemaker/openapi-edit -# github.com/MarkRosemaker/openapi-flatten v0.0.0-20261125230637-977f667d3678 +# github.com/MarkRosemaker/openapi-flatten v0.0.0-20261126014520-55785d534cc0 ## explicit; go 1.27 github.com/MarkRosemaker/openapi-flatten # github.com/MarkRosemaker/ordmap v0.0.0-20261124050216-f3096eb875be