From d71192c8aba7ae0a62e168be9446cdf5e4b3e9dd Mon Sep 17 00:00:00 2001 From: Maksim Falaleev Date: Tue, 17 Dec 2024 17:17:36 +0300 Subject: [PATCH 1/5] fix(expand): fix missing tags and todo --- gen/generator.go | 8 +++---- openapi/operation.go | 9 ++++---- openapi/parser/expand.go | 14 ++++++------ openapi/parser/parse_path_item.go | 1 + openapi/parser/parse_server_test.go | 33 ++++++++++++++++++++++++++++- 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/gen/generator.go b/gen/generator.go index c32595413..489d3aaa1 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -39,7 +39,7 @@ type Generator struct { log *zap.Logger } -func expandSpec(api *openapi.API, p string) (err error) { +func expandSpec(api *openapi.API, spec *ogen.Spec, p string) (err error) { p, err = filepath.Abs(filepath.Clean(p)) if err != nil { return err @@ -50,12 +50,12 @@ func expandSpec(api *openapi.API, p string) (err error) { return err } - spec, err := parser.Expand(api) + mergedSpec, err := parser.Expand(api, spec) if err != nil { return errors.Wrap(err, "expand") } - data, err := yaml.Marshal(spec) + data, err := yaml.Marshal(mergedSpec) if err != nil { return errors.Wrap(err, "marshal") } @@ -86,7 +86,7 @@ func NewGenerator(spec *ogen.Spec, opts Options) (*Generator, error) { } if p := opts.ExpandSpec; p != "" { - if err := expandSpec(api, p); err != nil { + if err := expandSpec(api, spec, p); err != nil { return nil, errors.Wrap(err, "expand spec") } } diff --git a/openapi/operation.go b/openapi/operation.go index dc20be03f..8ec5c5140 100644 --- a/openapi/operation.go +++ b/openapi/operation.go @@ -10,10 +10,11 @@ import ( // Operation is an OpenAPI Operation. type Operation struct { - OperationID string // optional - Summary string // optional - Description string // optional - Deprecated bool // optional + Tags []string // optional + OperationID string // optional + Summary string // optional + Description string // optional + Deprecated bool // optional HTTPMethod string Path Path diff --git a/openapi/parser/expand.go b/openapi/parser/expand.go index b858e059f..218504f03 100644 --- a/openapi/parser/expand.go +++ b/openapi/parser/expand.go @@ -15,11 +15,11 @@ import ( ) // Expand generates an expanded ogen.Spec from given api. -func Expand(api *openapi.API) (*ogen.Spec, error) { +func Expand(api *openapi.API, spec *ogen.Spec) (*ogen.Spec, error) { e := expander{ localToRemote: map[string]localToRemote{}, } - return e.Spec(api) + return e.Spec(api, spec) } type localToRemote struct { @@ -32,17 +32,14 @@ type expander struct { localToRemote map[string]localToRemote } -func (e *expander) Spec(api *openapi.API) (spec *ogen.Spec, err error) { +func (e *expander) Spec(api *openapi.API, srcSpec *ogen.Spec) (spec *ogen.Spec, err error) { spec = new(ogen.Spec) spec.Init() e.components = spec.Components spec.OpenAPI = api.Version.String() - // FIXME(tdakkota): store actual information - spec.Info = ogen.Info{ - Title: "Expanded spec", - Version: "v0.1.0", - } + spec.Info = srcSpec.Info + spec.Tags = srcSpec.Tags if servers := api.Servers; len(servers) > 0 { expanded := make([]ogen.Server, len(servers)) @@ -263,6 +260,7 @@ func (e *expander) OAuthFlow(flow *openapi.OAuthFlow) (expanded *ogen.OAuthFlow, func (e *expander) Operation(op *openapi.Operation) (expanded *ogen.Operation, err error) { expanded = new(ogen.Operation) + expanded.Tags = op.Tags expanded.OperationID = op.OperationID expanded.Summary = op.Summary expanded.Description = op.Description diff --git a/openapi/parser/parse_path_item.go b/openapi/parser/parse_path_item.go index 260a7b67d..13c1de41d 100644 --- a/openapi/parser/parse_path_item.go +++ b/openapi/parser/parse_path_item.go @@ -113,6 +113,7 @@ func (p *parser) parseOp( }() op := &openapi.Operation{ + Tags: spec.Tags, OperationID: spec.OperationID, Summary: spec.Summary, Description: spec.Description, diff --git a/openapi/parser/parse_server_test.go b/openapi/parser/parse_server_test.go index 56d5f970a..924e3f1f7 100644 --- a/openapi/parser/parse_server_test.go +++ b/openapi/parser/parse_server_test.go @@ -15,6 +15,11 @@ info: description: test title: test version: 1.0.0 +tags: + - name: demo1 + description: demo1 description + - name: demo2 + description: demo2 description openapi: "3.0.0" servers: - url: "{protocol}://{host}:{port}" @@ -28,6 +33,27 @@ servers: enum: - http - https +paths: + /signup: + post: + tags: + - demo1 + operationId: signup + responses: + '204': + description: Successful operation + 'default': + Error: + description: Some error during request processing + content: + application/json: + schema: + type: object + required: + - error + properties: + error: + type: string' ` spec, err := ogen.Parse([]byte(data)) require.NoError(t, err) @@ -35,7 +61,7 @@ servers: api, err := parser.Parse(spec, parser.Settings{}) require.NoError(t, err) - expandSpec, err := parser.Expand(api) + expandSpec, err := parser.Expand(api, spec) require.NoError(t, err) require.Equal(t, []ogen.Server{ @@ -55,4 +81,9 @@ servers: }, }, }, expandSpec.Servers) + require.Equal(t, "demo1", expandSpec.Tags[0].Name) + require.Equal(t, "demo2", expandSpec.Tags[1].Name) + require.Equal(t, "demo1 description", expandSpec.Tags[0].Description) + require.Equal(t, "demo2 description", expandSpec.Tags[1].Description) + require.Equal(t, []string{"demo1"}, expandSpec.Paths["/signup"].Post.Tags) } From 15bee5c0886faad686b0ad29f283dde4aa0aa03e Mon Sep 17 00:00:00 2001 From: Maksim Falaleev Date: Tue, 17 Dec 2024 20:01:49 +0300 Subject: [PATCH 2/5] fix(expand): fix missing tags --- openapi/api.go | 1 + openapi/operation.go | 9 +++++---- openapi/parser/expand.go | 11 +++++++++++ openapi/parser/parse_path_item.go | 1 + openapi/tag.go | 7 +++++++ 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 openapi/tag.go diff --git a/openapi/api.go b/openapi/api.go index 5b237a09b..552a3625b 100644 --- a/openapi/api.go +++ b/openapi/api.go @@ -12,6 +12,7 @@ type Ref = jsonpointer.RefKey // API represents parsed OpenAPI spec. type API struct { Version Version + Tags []Tag Servers []Server Operations []*Operation Webhooks []Webhook diff --git a/openapi/operation.go b/openapi/operation.go index dc20be03f..8ec5c5140 100644 --- a/openapi/operation.go +++ b/openapi/operation.go @@ -10,10 +10,11 @@ import ( // Operation is an OpenAPI Operation. type Operation struct { - OperationID string // optional - Summary string // optional - Description string // optional - Deprecated bool // optional + Tags []string // optional + OperationID string // optional + Summary string // optional + Description string // optional + Deprecated bool // optional HTTPMethod string Path Path diff --git a/openapi/parser/expand.go b/openapi/parser/expand.go index b858e059f..a500bd467 100644 --- a/openapi/parser/expand.go +++ b/openapi/parser/expand.go @@ -38,6 +38,16 @@ func (e *expander) Spec(api *openapi.API) (spec *ogen.Spec, err error) { e.components = spec.Components spec.OpenAPI = api.Version.String() + + tags := make([]ogen.Tag, len(api.Tags)) + for i, tag := range api.Tags { + tags[i] = ogen.Tag{ + Name: tag.Name, + Description: tag.Description, + } + } + spec.Tags = tags + // FIXME(tdakkota): store actual information spec.Info = ogen.Info{ Title: "Expanded spec", @@ -263,6 +273,7 @@ func (e *expander) OAuthFlow(flow *openapi.OAuthFlow) (expanded *ogen.OAuthFlow, func (e *expander) Operation(op *openapi.Operation) (expanded *ogen.Operation, err error) { expanded = new(ogen.Operation) + expanded.Tags = op.Tags expanded.OperationID = op.OperationID expanded.Summary = op.Summary expanded.Description = op.Description diff --git a/openapi/parser/parse_path_item.go b/openapi/parser/parse_path_item.go index 260a7b67d..13c1de41d 100644 --- a/openapi/parser/parse_path_item.go +++ b/openapi/parser/parse_path_item.go @@ -113,6 +113,7 @@ func (p *parser) parseOp( }() op := &openapi.Operation{ + Tags: spec.Tags, OperationID: spec.OperationID, Summary: spec.Summary, Description: spec.Description, diff --git a/openapi/tag.go b/openapi/tag.go new file mode 100644 index 000000000..9b1000cdd --- /dev/null +++ b/openapi/tag.go @@ -0,0 +1,7 @@ +package openapi + +// Tag represents parsed OpenAPI tags. +type Tag struct { + Name string + Description string +} From 783d367a6a5126eeef8451f7bb9a8fcef4535ad7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 19 Dec 2024 03:53:56 +0000 Subject: [PATCH 3/5] chore: bump golang.org/x/net from 0.32.0 to 0.33.0 Bumps [golang.org/x/net](https://github.com/golang/net) from 0.32.0 to 0.33.0. - [Commits](https://github.com/golang/net/compare/v0.32.0...v0.33.0) --- updated-dependencies: - dependency-name: golang.org/x/net dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d606036d5..8556ac229 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( go.uber.org/multierr v1.11.0 go.uber.org/zap v1.27.0 golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 - golang.org/x/net v0.32.0 + golang.org/x/net v0.33.0 golang.org/x/sync v0.10.0 golang.org/x/text v0.21.0 golang.org/x/tools v0.28.0 diff --git a/go.sum b/go.sum index b9bdbd952..018cd4e1e 100644 --- a/go.sum +++ b/go.sum @@ -70,8 +70,8 @@ golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiAp golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= -golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI= -golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs= +golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= +golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From 0f6016ea9da08ece5533b359c037acf2fbbfd1ea Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 19 Dec 2024 03:54:59 +0000 Subject: [PATCH 4/5] chore: go mod tidy [dependabot skip] --- examples/go.mod | 2 +- examples/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/go.mod b/examples/go.mod index b94fd0a68..345fed96a 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -33,7 +33,7 @@ require ( github.com/segmentio/asm v1.2.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect golang.org/x/mod v0.22.0 // indirect - golang.org/x/net v0.32.0 // indirect + golang.org/x/net v0.33.0 // indirect golang.org/x/sync v0.10.0 // indirect golang.org/x/sys v0.28.0 // indirect golang.org/x/text v0.21.0 // indirect diff --git a/examples/go.sum b/examples/go.sum index ef3c9de89..24b074c49 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -56,8 +56,8 @@ golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiAp golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= -golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI= -golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs= +golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= +golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From 7ecfd9169a85cd7d216271a17779da5c61265c09 Mon Sep 17 00:00:00 2001 From: Maksim Falaleev Date: Thu, 26 Dec 2024 14:43:51 +0300 Subject: [PATCH 5/5] fix(expand): fix missing tags and todo --- gen/generator.go | 8 +++---- openapi/parser/expand.go | 23 +++++--------------- openapi/parser/parse_server_test.go | 33 ++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/gen/generator.go b/gen/generator.go index c32595413..489d3aaa1 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -39,7 +39,7 @@ type Generator struct { log *zap.Logger } -func expandSpec(api *openapi.API, p string) (err error) { +func expandSpec(api *openapi.API, spec *ogen.Spec, p string) (err error) { p, err = filepath.Abs(filepath.Clean(p)) if err != nil { return err @@ -50,12 +50,12 @@ func expandSpec(api *openapi.API, p string) (err error) { return err } - spec, err := parser.Expand(api) + mergedSpec, err := parser.Expand(api, spec) if err != nil { return errors.Wrap(err, "expand") } - data, err := yaml.Marshal(spec) + data, err := yaml.Marshal(mergedSpec) if err != nil { return errors.Wrap(err, "marshal") } @@ -86,7 +86,7 @@ func NewGenerator(spec *ogen.Spec, opts Options) (*Generator, error) { } if p := opts.ExpandSpec; p != "" { - if err := expandSpec(api, p); err != nil { + if err := expandSpec(api, spec, p); err != nil { return nil, errors.Wrap(err, "expand spec") } } diff --git a/openapi/parser/expand.go b/openapi/parser/expand.go index a500bd467..218504f03 100644 --- a/openapi/parser/expand.go +++ b/openapi/parser/expand.go @@ -15,11 +15,11 @@ import ( ) // Expand generates an expanded ogen.Spec from given api. -func Expand(api *openapi.API) (*ogen.Spec, error) { +func Expand(api *openapi.API, spec *ogen.Spec) (*ogen.Spec, error) { e := expander{ localToRemote: map[string]localToRemote{}, } - return e.Spec(api) + return e.Spec(api, spec) } type localToRemote struct { @@ -32,27 +32,14 @@ type expander struct { localToRemote map[string]localToRemote } -func (e *expander) Spec(api *openapi.API) (spec *ogen.Spec, err error) { +func (e *expander) Spec(api *openapi.API, srcSpec *ogen.Spec) (spec *ogen.Spec, err error) { spec = new(ogen.Spec) spec.Init() e.components = spec.Components spec.OpenAPI = api.Version.String() - - tags := make([]ogen.Tag, len(api.Tags)) - for i, tag := range api.Tags { - tags[i] = ogen.Tag{ - Name: tag.Name, - Description: tag.Description, - } - } - spec.Tags = tags - - // FIXME(tdakkota): store actual information - spec.Info = ogen.Info{ - Title: "Expanded spec", - Version: "v0.1.0", - } + spec.Info = srcSpec.Info + spec.Tags = srcSpec.Tags if servers := api.Servers; len(servers) > 0 { expanded := make([]ogen.Server, len(servers)) diff --git a/openapi/parser/parse_server_test.go b/openapi/parser/parse_server_test.go index 56d5f970a..924e3f1f7 100644 --- a/openapi/parser/parse_server_test.go +++ b/openapi/parser/parse_server_test.go @@ -15,6 +15,11 @@ info: description: test title: test version: 1.0.0 +tags: + - name: demo1 + description: demo1 description + - name: demo2 + description: demo2 description openapi: "3.0.0" servers: - url: "{protocol}://{host}:{port}" @@ -28,6 +33,27 @@ servers: enum: - http - https +paths: + /signup: + post: + tags: + - demo1 + operationId: signup + responses: + '204': + description: Successful operation + 'default': + Error: + description: Some error during request processing + content: + application/json: + schema: + type: object + required: + - error + properties: + error: + type: string' ` spec, err := ogen.Parse([]byte(data)) require.NoError(t, err) @@ -35,7 +61,7 @@ servers: api, err := parser.Parse(spec, parser.Settings{}) require.NoError(t, err) - expandSpec, err := parser.Expand(api) + expandSpec, err := parser.Expand(api, spec) require.NoError(t, err) require.Equal(t, []ogen.Server{ @@ -55,4 +81,9 @@ servers: }, }, }, expandSpec.Servers) + require.Equal(t, "demo1", expandSpec.Tags[0].Name) + require.Equal(t, "demo2", expandSpec.Tags[1].Name) + require.Equal(t, "demo1 description", expandSpec.Tags[0].Description) + require.Equal(t, "demo2 description", expandSpec.Tags[1].Description) + require.Equal(t, []string{"demo1"}, expandSpec.Paths["/signup"].Post.Tags) }