diff --git a/go.mod b/go.mod index 1240b95e..c06bfbbe 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,9 @@ require ( github.com/MarkRosemaker/cassette v0.0.0-20261114134839-b89997ee7b14 github.com/MarkRosemaker/errpath v0.0.0-20261114134757-f7304bd9c564 github.com/MarkRosemaker/openapi v0.0.0-20261114134905-7ab2d5f6532c - github.com/MarkRosemaker/openapi-compress v0.0.0-20261114135233-1664a6a03857 + github.com/MarkRosemaker/openapi-compress v0.0.0-20261114230256-181235402941 github.com/MarkRosemaker/openapi-edit v0.0.0-20261114135009-67d93f3a1aef - github.com/MarkRosemaker/openapi-flatten v0.0.0-20261114135009-b46b792b06be + github.com/MarkRosemaker/openapi-flatten v0.0.0-20261114170301-47338bc4c722 github.com/MarkRosemaker/ordmap v0.0.0-20261114134829-166766214acc github.com/ettle/strcase v0.2.0 github.com/go-api-libs/api v0.0.0-20261115030148-02c29b673d3a diff --git a/go.sum b/go.sum index 60016d4b..54d28c06 100644 --- a/go.sum +++ b/go.sum @@ -12,14 +12,14 @@ github.com/MarkRosemaker/openapi v0.0.0-20261114134905-7ab2d5f6532c h1:H6SznVuwV github.com/MarkRosemaker/openapi v0.0.0-20261114134905-7ab2d5f6532c/go.mod h1:UK917yVQxe7KdjnDNyIUnqjzYx2WC4BGdV7tamna2/4= github.com/MarkRosemaker/openapi-compare v0.0.0-20261114135007-8a5d11532780 h1:7mZn+4zdsmDj7WhXavjHM1Jy4zRYHVEYiCgl/AzSnq8= github.com/MarkRosemaker/openapi-compare v0.0.0-20261114135007-8a5d11532780/go.mod h1:tBYuD2tpqGnSB0QgVcwosrp7hWcA6fCcVQAoY2kVzOs= -github.com/MarkRosemaker/openapi-compress v0.0.0-20261114135233-1664a6a03857 h1:K3IPv3R77oJjgV9A0NqLTH2d1GAvz6o4G0Tyo/8AMXw= -github.com/MarkRosemaker/openapi-compress v0.0.0-20261114135233-1664a6a03857/go.mod h1:ZD8fuqC34RgVdEbuVEYf6yaMKlxF+fY+f0qRQ8leTSs= +github.com/MarkRosemaker/openapi-compress v0.0.0-20261114230256-181235402941 h1:1OnwQOFiblqmFyq8kkxyzAyL41oHWyHuz5INMhcM9SQ= +github.com/MarkRosemaker/openapi-compress v0.0.0-20261114230256-181235402941/go.mod h1:ZD8fuqC34RgVdEbuVEYf6yaMKlxF+fY+f0qRQ8leTSs= github.com/MarkRosemaker/openapi-edit v0.0.0-20261114135009-67d93f3a1aef h1:jrKZE+ewedAdXeyEdmirhWdgeccnWjcElN/6iKQO1HA= github.com/MarkRosemaker/openapi-edit v0.0.0-20261114135009-67d93f3a1aef/go.mod h1:VCze+ZF/ijSBeFmRCMqqq4Cz/LznKzhIBm8HnmELvFs= 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-20261114135009-b46b792b06be h1:e0F/kOxYif6ElT7SuUH3wRbGGv0lRPnAcgXpF1so45Q= -github.com/MarkRosemaker/openapi-flatten v0.0.0-20261114135009-b46b792b06be/go.mod h1:Daak+tyOP/WoTzXwynjYwWCuxt3eRyYyA1NpP2TphiM= +github.com/MarkRosemaker/openapi-flatten v0.0.0-20261114170301-47338bc4c722 h1:ndo7ZjdiTZ1uqQh5hnjHNy7sz6/x92tCLpdMTcryh08= +github.com/MarkRosemaker/openapi-flatten v0.0.0-20261114170301-47338bc4c722/go.mod h1:Daak+tyOP/WoTzXwynjYwWCuxt3eRyYyA1NpP2TphiM= 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-20261114134829-166766214acc h1:Iqp9B+mAhGaa7smOg6+jkRV5U6SC4mqVW3Z5wSEFV7w= diff --git a/testdata/golden/pkg/golden/ir.json b/testdata/golden/pkg/golden/ir.json index 545467a0..ee7d9d6f 100644 --- a/testdata/golden/pkg/golden/ir.json +++ b/testdata/golden/pkg/golden/ir.json @@ -3062,7 +3062,7 @@ { "name": "MembershipType", "jsonName": "membership_type", - "type": "MemberPersonMembershipType", + "type": "MembershipType", "jsonTag": "json:\"membership_type\"", "required": true }, @@ -3077,18 +3077,18 @@ "memberDecoder": true }, { - "name": "MemberPersonMembershipType", - "description": "MemberPersonMembershipType defines a model", + "name": "MembershipType", + "description": "MembershipType defines a model", "kind": 1, "type": "string", "enumValues": [ { - "goName": "MemberPersonMembershipTypeMember", + "goName": "MembershipTypeMember", "value": "member", "literal": "\"member\"" }, { - "goName": "MemberPersonMembershipTypeGuest", + "goName": "MembershipTypeGuest", "value": "guest", "literal": "\"guest\"" } diff --git a/testdata/golden/pkg/golden/types.gen.go b/testdata/golden/pkg/golden/types.gen.go index 422feeb1..42798061 100644 --- a/testdata/golden/pkg/golden/types.gen.go +++ b/testdata/golden/pkg/golden/types.gen.go @@ -1478,9 +1478,9 @@ func (v *MemberAllOf1) MarshalJSONTo(enc *jsontext.Encoder) error { // MemberPerson defines a model type MemberPerson struct { - Type string `json:"type"` - MembershipType MemberPersonMembershipType `json:"membership_type"` - Person UserOneOfPerson `json:"person"` + Type string `json:"type"` + MembershipType MembershipType `json:"membership_type"` + Person UserOneOfPerson `json:"person"` } // unmarshalJSONMember decodes the value of the member name into its field, reporting whether MemberPerson declares it. @@ -1497,18 +1497,18 @@ func (v *MemberPerson) unmarshalJSONMember(dec *jsontext.Decoder, name string) ( return false, nil } -// MemberPersonMembershipType defines a model -type MemberPersonMembershipType string +// MembershipType defines a model +type MembershipType string const ( - MemberPersonMembershipTypeMember MemberPersonMembershipType = "member" - MemberPersonMembershipTypeGuest MemberPersonMembershipType = "guest" + MembershipTypeMember MembershipType = "member" + MembershipTypeGuest MembershipType = "guest" ) -// Valid indicates whether the value is a known member of the MemberPersonMembershipType enum. -func (e MemberPersonMembershipType) Valid() bool { +// Valid indicates whether the value is a known member of the MembershipType enum. +func (e MembershipType) Valid() bool { switch e { - case MemberPersonMembershipTypeMember, MemberPersonMembershipTypeGuest: + case MembershipTypeMember, MembershipTypeGuest: return true default: return false diff --git a/testdata/golden/pkg/golden/types.gen_test.go b/testdata/golden/pkg/golden/types.gen_test.go index 5f484d99..35c77c35 100644 --- a/testdata/golden/pkg/golden/types.gen_test.go +++ b/testdata/golden/pkg/golden/types.gen_test.go @@ -62,14 +62,14 @@ func TestEnums_Valid(t *testing.T) { t.Errorf("Level(%v): valid, though no member", 0) } - for _, v := range []MemberPersonMembershipType{MemberPersonMembershipTypeMember, MemberPersonMembershipTypeGuest} { + for _, v := range []MembershipType{MembershipTypeMember, MembershipTypeGuest} { if !v.Valid() { - t.Errorf("MemberPersonMembershipType %v: not valid", v) + t.Errorf("MembershipType %v: not valid", v) } } - if MemberPersonMembershipType("not a member").Valid() { - t.Errorf("MemberPersonMembershipType(%v): valid, though no member", "not a member") + if MembershipType("not a member").Valid() { + t.Errorf("MembershipType(%v): valid, though no member", "not a member") } for _, v := range []Module{ModulePrice, ModuleSummaryDetail} { diff --git a/vendor/github.com/MarkRosemaker/openapi-compress/README.md b/vendor/github.com/MarkRosemaker/openapi-compress/README.md index 16a79698..a0966edf 100644 --- a/vendor/github.com/MarkRosemaker/openapi-compress/README.md +++ b/vendor/github.com/MarkRosemaker/openapi-compress/README.md @@ -82,6 +82,11 @@ Within the schema kept, a property, item or alternative that has no description takes one from those merged into it, as examples do: what a property is for is the same in both. Where both describe it, the one kept says what it does. +A default is the same: what a value is when left out is the operation's. Where +the schemas that merge disagree on it, as two enums of views one request +defaults to `high top-down` and another to `low top-down`, each one's default +moves beside the `$ref`s that pointed at it, and the schema kept has none. + ## Usage ```bash diff --git a/vendor/github.com/MarkRosemaker/openapi-compress/devtool.json b/vendor/github.com/MarkRosemaker/openapi-compress/devtool.json index ba3d7ec8..23c17cc7 100644 --- a/vendor/github.com/MarkRosemaker/openapi-compress/devtool.json +++ b/vendor/github.com/MarkRosemaker/openapi-compress/devtool.json @@ -12,8 +12,8 @@ "schema", "swagger" ], - "coverage": 86.3, - "devtoolVersion": "v0.0.0-20261114134623-f0f0252835dc", + "coverage": 86.8, + "devtoolVersion": "v0.0.0-20261114214457-9b68a6eb5335", "kind": "cli", "tools": { "golangci-lint": "v2.15.0", diff --git a/vendor/github.com/MarkRosemaker/openapi-compress/document.go b/vendor/github.com/MarkRosemaker/openapi-compress/document.go index 83ba43ea..0e8e069e 100644 --- a/vendor/github.com/MarkRosemaker/openapi-compress/document.go +++ b/vendor/github.com/MarkRosemaker/openapi-compress/document.go @@ -2,6 +2,7 @@ package compress import ( "cmp" + "encoding/json/jsontext" "encoding/json/v2" "math" "reflect" @@ -159,6 +160,8 @@ func deduplicateSchemasAtThreshold(d *openapi.Document, threshold float64, deriv return nil, err } + defaultReferences(d, defaultWhereUsed(schemas, replacements)) + if err := edit.RedirectSchemas(d, replacements); err != nil { return nil, err } @@ -408,29 +411,67 @@ func sharedNames(a, b openapi.Schemas) int { // A description says what a schema is used for in one place, not what shape it has, so after the merge it belongs // beside the references to each schema rather than on the one kept, where every reference would show it. func describeWhereUsed(schemas openapi.Schemas, replacements map[string]string) map[string]string { + return whereUsed(schemas, replacements, + func(s *openapi.Schema) string { return s.Description }, + func(s *openapi.Schema) { s.Description = "" }) +} + +// defaultWhereUsed returns the default of each schema merging into another, or being merged into, wherever the +// schemas merging into one disagree on it, and takes it off the schema kept: what a value is when left out is the +// operation's, as an enum of views one request defaults to "high top-down" and another to "low top-down". +func defaultWhereUsed(schemas openapi.Schemas, replacements map[string]string) map[string]string { + return whereUsed(schemas, replacements, + func(s *openapi.Schema) string { return string(s.Default) }, + func(s *openapi.Schema) { s.Default = nil }) +} + +// whereUsed returns what get reads of each schema merging into another, or being merged into, wherever the schemas +// merging into one disagree on it, and clears it on the schema kept. +func whereUsed(schemas openapi.Schemas, replacements map[string]string, get func(*openapi.Schema) string, clear func(*openapi.Schema)) map[string]string { groups := map[string][]string{} for name, canonical := range replacements { groups[canonical] = append(groups[canonical], name) } - describe := map[string]string{} + used := map[string]string{} for canonical, merged := range groups { members := append(merged, canonical) - desc := schemas[canonical].Description - if !slices.ContainsFunc(members, func(name string) bool { return schemas[name].Description != desc }) { + kept := get(schemas[canonical]) + if !slices.ContainsFunc(members, func(name string) bool { return get(schemas[name]) != kept }) { continue } for _, name := range members { - if d := schemas[name].Description; d != "" { - describe[name] = d + if v := get(schemas[name]); v != "" { + used[name] = v } } - schemas[canonical].Description = "" + clear(schemas[canonical]) } - return describe + return used +} + +// schemaRefPrefix starts a reference to a schema of components.schemas. +const schemaRefPrefix = "#/components/schemas/" + +// defaultReferences gives every reference to a schema in defaults the default listed for it, beside the $ref, unless +// the reference has a default of its own already. +func defaultReferences(d *openapi.Document, defaults map[string]string) { + if len(defaults) == 0 { + return + } + + edit.WalkSchemas(d, func(s *openapi.Schema) { + if s.Ref == nil || len(s.Default) > 0 { + return + } + + if v, ok := defaults[strings.TrimPrefix(s.Ref.Identifier, schemaRefPrefix)]; ok { + s.Default = jsontext.Value(v) + } + }) } diff --git a/vendor/github.com/MarkRosemaker/openapi-flatten/README.md b/vendor/github.com/MarkRosemaker/openapi-flatten/README.md index 12344394..006ae148 100644 --- a/vendor/github.com/MarkRosemaker/openapi-flatten/README.md +++ b/vendor/github.com/MarkRosemaker/openapi-flatten/README.md @@ -213,6 +213,11 @@ repeating themselves. All names are converted to Go-style PascalCase (e.g., `create pet bad request` → `CreatePetBadRequest`). If the generated name is already taken, a numeric suffix is appended (`Name2`, `Name3`, …) to avoid collisions. +A property's schema is named by its parent plus the property (`PetOwner`), or by the property alone where its name +has two words or more and is free (`motion_adjustments` → `MotionAdjustments`): such a name says what the schema is +without its parent. A union keeps its parent (`PetLastRunAt`), as its property says what the value means rather than +what it is. + An `allOf` entry is named by its parent, the keyword and its position (`PetAllOf1`). A `oneOf` or `anyOf` alternative is named by its `title` if it has one (`"A person"` → `APerson`), and otherwise the same way (`PetOneOf1`). The schema of an object's keys is named after the object plus `Key` (`PetKey`), and a `not` schema plus `Not` (`PetNot`). ## Error reporting diff --git a/vendor/github.com/MarkRosemaker/openapi-flatten/devtool.json b/vendor/github.com/MarkRosemaker/openapi-flatten/devtool.json index aad58bb9..f959dfd3 100644 --- a/vendor/github.com/MarkRosemaker/openapi-flatten/devtool.json +++ b/vendor/github.com/MarkRosemaker/openapi-flatten/devtool.json @@ -12,8 +12,8 @@ "schema", "swagger" ], - "coverage": 95.7, - "devtoolVersion": "v0.0.0-20261114134623-f0f0252835dc", + "coverage": 95.5, + "devtoolVersion": "v0.0.0-20261114151213-459d3b01ab20", "kind": "cli", "tools": { "golangci-lint": "v2.15.0", diff --git a/vendor/github.com/MarkRosemaker/openapi-flatten/schema_refs.go b/vendor/github.com/MarkRosemaker/openapi-flatten/schema_refs.go index 1ec5ed22..0e6cf572 100644 --- a/vendor/github.com/MarkRosemaker/openapi-flatten/schema_refs.go +++ b/vendor/github.com/MarkRosemaker/openapi-flatten/schema_refs.go @@ -1,17 +1,44 @@ package flatten import ( + "regexp" + "github.com/MarkRosemaker/errpath" "github.com/MarkRosemaker/openapi" ) func inlineSchemas(d *openapi.Document, ss openapi.Schemas, prefix string) error { for name, s := range ss.ByIndex() { - if err := inlineSchema(d, s, - joinName(prefix, name), false); err != nil { + if err := inlineSchema(d, s, propertyName(d, s, prefix, name), false); err != nil { return &errpath.ErrKey{Key: name, Err: err} } } return nil } + +// propertyName is the name of s, what the property name of the schema named prefix holds: the property's own, as +// MotionAdjustments for motion_adjustments, where it says what s is in two words or more and no schema has it, or +// else prefixed, as JobData for data, which says little alone. A union of values keeps the prefix too: what its +// property says, as has_unpublished_changes or last_run_at, is what the value means, not what kind of thing it is. +func propertyName(d *openapi.Document, s *openapi.Schema, prefix, name string) string { + own := goName(name) + if _, taken := d.Components.Schemas[own]; !taken && reWords.MatchString(name) && !isUnion(s) { + return own + } + + return joinName(prefix, name) +} + +// isUnion reports whether s, or what s holds where it may also be null, is a oneOf or anyOf of alternatives. +func isUnion(s *openapi.Schema) bool { + if alt := nonNull(s); alt != nil { + s = alt + } + + return len(s.OneOf) > 0 || len(s.AnyOf) > 0 +} + +// reWords matches a property name of two words or more, as motion_adjustments, north-east or motionAdjustments, not +// an ID such as CasdiSFqTw7i8FtJGuQyFEq0. +var reWords = regexp.MustCompile(`^[a-z][a-z0-9]*([_-][a-z][a-z0-9]*)+$|^[a-z]+([A-Z][a-z]+)+$`) diff --git a/vendor/modules.txt b/vendor/modules.txt index 8e4574f5..2dbcdc16 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -19,13 +19,13 @@ github.com/MarkRosemaker/openapi # github.com/MarkRosemaker/openapi-compare v0.0.0-20261114135007-8a5d11532780 ## explicit; go 1.27 github.com/MarkRosemaker/openapi-compare/schema -# github.com/MarkRosemaker/openapi-compress v0.0.0-20261114135233-1664a6a03857 +# github.com/MarkRosemaker/openapi-compress v0.0.0-20261114230256-181235402941 ## explicit; go 1.27 github.com/MarkRosemaker/openapi-compress # github.com/MarkRosemaker/openapi-edit v0.0.0-20261114135009-67d93f3a1aef ## explicit; go 1.27 github.com/MarkRosemaker/openapi-edit -# github.com/MarkRosemaker/openapi-flatten v0.0.0-20261114135009-b46b792b06be +# github.com/MarkRosemaker/openapi-flatten v0.0.0-20261114170301-47338bc4c722 ## explicit; go 1.27 github.com/MarkRosemaker/openapi-flatten # github.com/MarkRosemaker/ordmap v0.0.0-20261114134829-166766214acc