Skip to content

Commit 9f3d7b9

Browse files
feat: update the merge logic to generate an OAS where the fields are in the same order as the Atlas OAS (#35)
1 parent 126cef1 commit 9f3d7b9

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

tools/cli/internal/cli/merge/merge.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package merge
1616

1717
import (
18-
"bytes"
1918
"encoding/json"
2019
"fmt"
2120
"log"
@@ -41,13 +40,14 @@ func (o *Opts) Run() error {
4140
return err
4241
}
4342

44-
federatedBytes, err := federated.Spec.MarshalJSON()
43+
federatedBytes, err := json.MarshalIndent(*federated, "", " ")
4544
if err != nil {
4645
return err
4746
}
4847

4948
if o.outputPath == "" {
50-
return prettyPrintJSON(federatedBytes)
49+
fmt.Println(string(federatedBytes))
50+
return nil
5151
}
5252

5353
return o.saveFile(federatedBytes)
@@ -67,15 +67,6 @@ func (o *Opts) PreRunE(_ []string) error {
6767
return err
6868
}
6969

70-
func prettyPrintJSON(jsonBytes []byte) error {
71-
var prettyJSON bytes.Buffer
72-
if err := json.Indent(&prettyJSON, jsonBytes, "", " "); err != nil {
73-
return err
74-
}
75-
fmt.Println(prettyJSON.String())
76-
return nil
77-
}
78-
7970
func (o *Opts) saveFile(data []byte) error {
8071
if err := afero.WriteFile(o.fs, o.outputPath, data, 0o600); err != nil {
8172
return err

tools/cli/internal/cli/merge/merge_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/mongodb/openapi/tools/cli/internal/openapi"
2424
"github.com/spf13/afero"
2525
"github.com/stretchr/testify/require"
26-
"github.com/tufin/oasdiff/load"
2726
"go.uber.org/mock/gomock"
2827
)
2928

@@ -40,10 +39,11 @@ func TestSuccessfulMerge_Run(t *testing.T) {
4039
fs: fs,
4140
}
4241

43-
response := &load.SpecInfo{
44-
Spec: &openapi3.T{},
45-
Url: "test",
46-
Version: "3.0.1",
42+
response := &openapi.Spec{
43+
OpenAPI: "v3.0.1",
44+
Info: &openapi3.Info{},
45+
Servers: nil,
46+
Tags: openapi3.Tags{},
4747
}
4848

4949
mockMergerStore.

tools/cli/internal/openapi/mock_openapi.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/cli/internal/openapi/openapi.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package openapi
1818
import (
1919
"log"
2020

21+
"github.com/getkin/kin-openapi/openapi3"
2122
"github.com/tufin/oasdiff/diff"
2223
"github.com/tufin/oasdiff/load"
2324
)
@@ -27,10 +28,21 @@ type Parser interface {
2728
}
2829

2930
type Merger interface {
30-
MergeOpenAPISpecs([]string) (*load.SpecInfo, error)
31+
MergeOpenAPISpecs([]string) (*Spec, error)
3132
}
3233

33-
func (o *OasDiff) MergeOpenAPISpecs(paths []string) (*load.SpecInfo, error) {
34+
type Spec struct {
35+
OpenAPI string `json:"openapi" yaml:"openapi"`
36+
Security openapi3.SecurityRequirements `json:"security,omitempty" yaml:"security,omitempty"`
37+
Servers openapi3.Servers `json:"servers,omitempty" yaml:"servers,omitempty"`
38+
Tags openapi3.Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
39+
Info *openapi3.Info `json:"info" yaml:"info"`
40+
Paths *openapi3.Paths `json:"paths" yaml:"paths"`
41+
Components *openapi3.Components `json:"components,omitempty" yaml:"components,omitempty"`
42+
ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
43+
}
44+
45+
func (o *OasDiff) MergeOpenAPISpecs(paths []string) (*Spec, error) {
3446
for _, p := range paths {
3547
spec, err := o.parser.CreateOpenAPISpecFromPath(p)
3648
if err != nil {
@@ -51,7 +63,7 @@ func (o *OasDiff) MergeOpenAPISpecs(paths []string) (*load.SpecInfo, error) {
5163
}
5264
}
5365

54-
return o.base, nil
66+
return newSpec(o.base), nil
5567
}
5668

5769
func NewOasDiff(base string) (*OasDiff, error) {
@@ -69,3 +81,16 @@ func NewOasDiff(base string) (*OasDiff, error) {
6981
},
7082
}, nil
7183
}
84+
85+
func newSpec(specInfo *load.SpecInfo) *Spec {
86+
return &Spec{
87+
OpenAPI: specInfo.Spec.OpenAPI,
88+
Components: specInfo.Spec.Components,
89+
Info: specInfo.Spec.Info,
90+
Paths: specInfo.Spec.Paths,
91+
Security: specInfo.Spec.Security,
92+
Servers: specInfo.Spec.Servers,
93+
Tags: specInfo.Spec.Tags,
94+
ExternalDocs: specInfo.Spec.ExternalDocs,
95+
}
96+
}

0 commit comments

Comments
 (0)