Skip to content

Commit

Permalink
Fix misc OAS 2.0 bugs upgrade ndc-sdk-go v1.2.0 (#23)
Browse files Browse the repository at this point in the history
* oas2: fix incorrect PascalCase reference names
* oas2 fix missing json representation in JSON scalars
* cmd: don't use default values in command arguments. They will always replace values in the config file.
* sdk: Upgrade ndc-sdk-go v1.2.0
* sdk: Support NDC spec v0.1.3
  • Loading branch information
hgiasac committed Jun 2, 2024
1 parent a3cbdfc commit cfd0f85
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 40 deletions.
5 changes: 4 additions & 1 deletion command/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ConvertCommandArguments struct {
File string `help:"File path needs to be converted." short:"f"`
Config string `help:"Path of the config file." short:"c"`
Output string `help:"The location where the ndc schema file will be generated. Print to stdout if not set" short:"o"`
Spec string `help:"The API specification of the file, is one of oas3 (openapi3), oas2 (openapi2)" default:"oas3"`
Spec string `help:"The API specification of the file, is one of oas3 (openapi3), oas2 (openapi2)"`
Format string `help:"The output format, is one of json, yaml. If the output is set, automatically detect the format in the output file extension" default:"json"`
Pure bool `help:"Return the pure NDC schema only" default:"false"`
TrimPrefix string `help:"Trim the prefix in URL, e.g. /v1"`
Expand Down Expand Up @@ -194,6 +194,9 @@ func ResolveConvertConfigArguments(config *ConvertConfig, configDir string, args
config.Pure = args.Pure
}
}
if config.Spec == "" {
config.Spec = schema.OAS3Spec
}

if args != nil && args.File != "" {
config.File = args.File
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ require (
github.com/sergi/go-diff v1.3.1 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/exp v0.0.0-20240529005216-23cca8864a10 // indirect
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc // indirect
golang.org/x/sync v0.7.0 // indirect
)
18 changes: 2 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/hasura/ndc-sdk-go v1.1.3 h1:/hKSsJqwoTOmNYUU7p0Gsy+0v2PmWLZvP+u8c2zCFOY=
github.com/hasura/ndc-sdk-go v1.1.3/go.mod h1:KIvlxe72E5+/yarmOzxk2FlFyAC6fHbEL6RdsgWhzj0=
github.com/hasura/ndc-sdk-go v1.1.4 h1:TL72oCUy3zM/8jSjNstn9Za1ILDdXV3WgMavf8gJaNQ=
github.com/hasura/ndc-sdk-go v1.1.4/go.mod h1:KIvlxe72E5+/yarmOzxk2FlFyAC6fHbEL6RdsgWhzj0=
github.com/hasura/ndc-sdk-go v1.1.5-0.20240525054642-d66243e8c144 h1:8Fm7YWdkmKz1BoizhwE1/tvym+/exxqFN8cnIMNRLyw=
github.com/hasura/ndc-sdk-go v1.1.5-0.20240525054642-d66243e8c144/go.mod h1:KIvlxe72E5+/yarmOzxk2FlFyAC6fHbEL6RdsgWhzj0=
github.com/hasura/ndc-sdk-go v1.2.0 h1:7OrELJbn6Vaj1bsG7pgHkn3rhivD5e2gxjWS/4fR21Q=
github.com/hasura/ndc-sdk-go v1.2.0/go.mod h1:2P+xMUuLE4xgFh1bOYGK04+KwLEFH2i+bCy9a8cWnlM=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
Expand Down Expand Up @@ -75,12 +69,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.16.5 h1:jqb/N5nc2zuSUSWDgCXi2vKGxMQfTsWHgtmPWSKQGqc=
github.com/pb33f/libopenapi v0.16.5/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi v0.16.6 h1:w0qrbAziEVvUlzGP7rTjMYS8bv9CfuIOW8OTOBgFGuo=
github.com/pb33f/libopenapi v0.16.6/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi v0.16.7 h1:kgzBpcX+ESF+C/UUlpqd+H4qdWloBfsDJVMgUmbPDts=
github.com/pb33f/libopenapi v0.16.7/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi v0.16.8 h1:EceJ+JTHViwjKNyP3LJ8J39jSQ4F2oOvHw6KfWCcVxM=
github.com/pb33f/libopenapi v0.16.8/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -103,10 +91,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/exp v0.0.0-20240529005216-23cca8864a10 h1:vpzMC/iZhYFAjJzHU0Cfuq+w1vLLsF2vLkDrPjzKYck=
golang.org/x/exp v0.0.0-20240529005216-23cca8864a10/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc h1:O9NuF4s+E/PvMIy+9IUZB9znFwUIXEWSstNjek6VpVg=
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down
10 changes: 6 additions & 4 deletions openapi/internal/oas2.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ func (oc *OAS2Builder) getSchemaTypeFromProxy(schemaProxy *base.SchemaProxy, nul
refName := getSchemaRefTypeNameV2(schemaProxy.GetReference())
// return early object from ref
if refName != "" && len(innerSchema.Type) > 0 && innerSchema.Type[0] == "object" {
ndcType = schema.NewNamedType(utils.ToPascalCase(refName))
refName = utils.ToPascalCase(refName)
ndcType = schema.NewNamedType(refName)
typeSchema = &rest.TypeSchema{Type: refName}
} else {
if innerSchema.Title != "" && !strings.Contains(innerSchema.Title, " ") {
Expand Down Expand Up @@ -282,9 +283,9 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi

var typeResult *rest.TypeSchema
if len(typeSchema.AnyOf) > 0 || typeSchema.AdditionalProperties != nil || len(typeSchema.Type) > 1 {
scalarName := "JSON"
scalarName := string(rest.ScalarJSON)
if _, ok := oc.schema.ScalarTypes[scalarName]; !ok {
oc.schema.ScalarTypes[scalarName] = *schema.NewScalarType()
oc.schema.ScalarTypes[scalarName] = *defaultScalarTypes[rest.ScalarJSON]
}
typeResult = createSchemaFromOpenAPISchema(typeSchema, scalarName)
return schema.NewNamedType(scalarName), typeResult, nil
Expand All @@ -310,7 +311,7 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi

if typeSchema.Properties == nil || typeSchema.Properties.IsZero() {
// treat no-property objects as a JSON scalar
oc.schema.ScalarTypes[refName] = *schema.NewScalarType()
oc.schema.ScalarTypes[refName] = *defaultScalarTypes[rest.ScalarJSON]
} else {
object := schema.ObjectType{
Fields: make(schema.ObjectTypeFields),
Expand Down Expand Up @@ -348,6 +349,7 @@ func (oc *OAS2Builder) getSchemaType(typeSchema *base.Schema, apiPath string, fi

itemName := getSchemaRefTypeNameV2(typeSchema.Items.A.GetReference())
if itemName != "" {
itemName := utils.ToPascalCase(itemName)
result = schema.NewArrayType(schema.NewNamedType(itemName))
} else {
itemSchemaA := typeSchema.Items.A.Schema()
Expand Down
10 changes: 5 additions & 5 deletions openapi/internal/oas2_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (oc *oas2OperationBuilder) convertParameters(params []*v2.Parameter, apiPat
return nil, errors.New("parameter name is empty")
}

var schemaType schema.TypeEncoder
var typeEncoder schema.TypeEncoder
var typeSchema *rest.TypeSchema
var err error

Expand All @@ -164,13 +164,13 @@ func (oc *oas2OperationBuilder) convertParameters(params []*v2.Parameter, apiPat
}

if param.Type != "" {
schemaType, err = oc.builder.getSchemaTypeFromParameter(param, apiPath, fieldPaths)
typeEncoder, err = oc.builder.getSchemaTypeFromParameter(param, apiPath, fieldPaths)
if err != nil {
return nil, err
}
nullable := !paramRequired
typeSchema = &rest.TypeSchema{
Type: getNamedType(schemaType, false, param.Type),
Type: getNamedType(typeEncoder, false, param.Type),
Pattern: param.Pattern,
Nullable: nullable,
}
Expand All @@ -191,7 +191,7 @@ func (oc *oas2OperationBuilder) convertParameters(params []*v2.Parameter, apiPat
typeSchema.MinLength = &minLength
}
} else if param.Schema != nil {
schemaType, typeSchema, err = oc.builder.getSchemaTypeFromProxy(param.Schema, !paramRequired, apiPath, fieldPaths)
typeEncoder, typeSchema, err = oc.builder.getSchemaTypeFromProxy(param.Schema, !paramRequired, apiPath, fieldPaths)
if err != nil {
return nil, err
}
Expand All @@ -203,7 +203,7 @@ func (oc *oas2OperationBuilder) convertParameters(params []*v2.Parameter, apiPat
}

argument := schema.ArgumentInfo{
Type: schemaType.Encode(),
Type: typeEncoder.Encode(),
}
if param.Description != "" {
argument.Description = &param.Description
Expand Down
4 changes: 2 additions & 2 deletions openapi/oas2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestOpenAPIv2ToRESTSchema(t *testing.T) {
Options ConvertOptions
Expected string
}{
// go run . convert -f ./openapi/testdata/jsonplaceholder/swagger.json -o ./openapi/testdata/jsonplaceholder/expected.json --spec openapi2 --trim-prefix /v1
// go run . convert -f ./openapi/testdata/jsonplaceholder/swagger.json -o ./openapi/testdata/jsonplaceholder/expected.json --spec oas2 --trim-prefix /v1
{
Name: "jsonplaceholder",
Source: "testdata/jsonplaceholder/swagger.json",
Expand All @@ -26,7 +26,7 @@ func TestOpenAPIv2ToRESTSchema(t *testing.T) {
TrimPrefix: "/v1",
},
},
// go run . convert -f ./openapi/testdata/petstore2/swagger.json -o ./openapi/testdata/petstore2/expected.json --spec openapi2
// go run . convert -f ./openapi/testdata/petstore2/swagger.json -o ./openapi/testdata/petstore2/expected.json --spec oas2
{
Name: "petstore2",
Source: "testdata/petstore2/swagger.json",
Expand Down
5 changes: 4 additions & 1 deletion openapi/testdata/jsonplaceholder/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,10 @@
},
"NotFoundError": {
"aggregate_functions": {},
"comparison_operators": {}
"comparison_operators": {},
"representation": {
"type": "json"
}
},
"String": {
"aggregate_functions": {},
Expand Down
Loading

0 comments on commit cfd0f85

Please sign in to comment.