Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed missing tags in expanded spec #1369

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 4 additions & 4 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions openapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions openapi/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions openapi/parser/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change.

Instead, let's add tags to the openapi.API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tdakkota нужно будет продублировать половину / все структуры ogen.Spec в openapi.Spec, чтобы на выходе получилась снова ogen.Spec. Оправдан ли такой подход?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Фактически, мы создаем копию спеки, чтобы мутировать ее, но не потерять при этом остальные данные.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для исправления todo нам нужно продублировать структуру Info и все связные с ней

type Info struct {
	// REQUIRED. The title of the API.
	Title string `json:"title" yaml:"title"`
	// A short summary of the API.
	Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
	// A short description of the API.
	// CommonMark syntax MAY be used for rich text representation.
	Description string `json:"description,omitempty" yaml:"description,omitempty"`
	// A URL to the Terms of Service for the API. MUST be in the format of a URL.
	TermsOfService string `json:"termsOfService,omitempty" yaml:"termsOfService,omitempty"`
	// The contact information for the exposed API.
	Contact *Contact `json:"contact,omitempty" yaml:"contact,omitempty"`
	// The license information for the exposed API.
	License *License `json:"license,omitempty" yaml:"license,omitempty"`
	// REQUIRED. The version of the OpenAPI document.
	Version string `json:"version" yaml:"version"`

	// Specification extensions.
	Extensions Extensions `json:"-" yaml:",inline"`
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавил версию только с тегами #1370

e := expander{
localToRemote: map[string]localToRemote{},
}
return e.Spec(api)
return e.Spec(api, spec)
}

type localToRemote struct {
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions openapi/parser/parse_path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (p *parser) parseOp(
}()

op := &openapi.Operation{
Tags: spec.Tags,
OperationID: spec.OperationID,
Summary: spec.Summary,
Description: spec.Description,
Expand Down
33 changes: 32 additions & 1 deletion openapi/parser/parse_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -28,14 +33,35 @@ 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)

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{
Expand All @@ -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)
}
7 changes: 7 additions & 0 deletions openapi/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package openapi

// Tag represents parsed OpenAPI tags.
type Tag struct {
Name string
Description string
}
Loading