Skip to content

Commit

Permalink
Keep spec order for swagger mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
BupycHuk authored and casualjim committed Oct 2, 2019
1 parent 5344aef commit f3076be
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cmd/swagger/commands/mixin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
flags "github.com/jessevdk/go-flags"

"github.com/go-swagger/go-swagger/generator"
)

const (
Expand All @@ -24,6 +26,7 @@ type MixinSpec struct {
ExpectedCollisionCount uint `short:"c" description:"expected # of rejected mixin paths, defs, etc due to existing key. Non-zero exit if does not match actual."`
Compact bool `long:"compact" description:"applies to JSON formatted specs. When present, doesn't prettify the json"`
Output flags.Filename `long:"output" short:"o" description:"the file to write to"`
KeepSpecOrder bool `long:"keep-spec-order" description:"Keep schema properties order identical to spec file"`
Format string `long:"format" description:"the format for the spec document" default:"json" choice:"yaml" choice:"json"`
}

Expand Down Expand Up @@ -89,6 +92,9 @@ func (c *MixinSpec) MixinFiles(primaryFile string, mixinFiles []string, w io.Wri

var mixins []*spec.Swagger
for _, mixinFile := range mixinFiles {
if c.KeepSpecOrder {
mixinFile = generator.WithAutoXOrder(mixinFile)
}
mixin, lerr := loads.Spec(mixinFile)
if lerr != nil {
return nil, lerr
Expand Down
2 changes: 1 addition & 1 deletion generator/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,7 @@ func TestGenModel_KeepSpecPropertiesOrder(t *testing.T) {

specDoc, err := loads.Spec(ymlFile)
assert.NoError(t, err)
orderedSpecDoc, err := loads.Spec(withAutoXOrder(ymlFile))
orderedSpecDoc, err := loads.Spec(WithAutoXOrder(ymlFile))
assert.NoError(t, err)

definitions := specDoc.Spec().Definitions
Expand Down
4 changes: 2 additions & 2 deletions generator/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func newAppGenerator(name string, modelNames, operationIDs []string, opts *GenOp
}

if opts.PropertiesSpecOrder {
opts.Spec = withAutoXOrder(opts.Spec)
opts.Spec = WithAutoXOrder(opts.Spec)
}

opts.Spec, specDoc, err = loadSpec(opts.Spec)
Expand Down Expand Up @@ -178,7 +178,7 @@ type appGenerator struct {
GenOpts *GenOpts
}

func withAutoXOrder(specPath string) string {
func WithAutoXOrder(specPath string) string {
lookFor := func(ele interface{}, key string) (yaml.MapSlice, bool) {
if slice, ok := ele.(yaml.MapSlice); ok {
for _, v := range slice {
Expand Down

0 comments on commit f3076be

Please sign in to comment.