Skip to content

Commit

Permalink
Fix scheme handling in v2->v3 conversion (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp authored Oct 21, 2021
1 parent e83ebc0 commit e9b36da
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
48 changes: 48 additions & 0 deletions openapi2conv/issue440_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package openapi2conv

import (
"context"
"encoding/json"
"os"
"testing"

"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

func TestIssue440(t *testing.T) {
doc2file, err := os.Open("testdata/swagger.json")
require.NoError(t, err)
defer doc2file.Close()
var doc2 openapi2.T
err = json.NewDecoder(doc2file).Decode(&doc2)
require.NoError(t, err)

doc3, err := ToV3(&doc2)
require.NoError(t, err)
err = doc3.Validate(context.Background())
require.NoError(t, err)
require.Equal(t, openapi3.Servers{
{URL: "https://petstore.swagger.io/v2"},
{URL: "http://petstore.swagger.io/v2"},
}, doc3.Servers)

doc2.Host = "your-bot-domain.de"
doc2.Schemes = nil
doc2.BasePath = ""
doc3, err = ToV3(&doc2)
require.NoError(t, err)
err = doc3.Validate(context.Background())
require.NoError(t, err)
require.Equal(t, openapi3.Servers{
{URL: "https://your-bot-domain.de/"},
}, doc3.Servers)

doc2.Host = "https://your-bot-domain.de"
doc2.Schemes = nil
doc2.BasePath = ""
doc3, err = ToV3(&doc2)
require.Error(t, err)
require.Contains(t, err.Error(), `invalid host`)
}
9 changes: 8 additions & 1 deletion openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ func ToV3(doc2 *openapi2.T) (*openapi3.T, error) {
}

if host := doc2.Host; host != "" {
if strings.Contains(host, "/") {
err := fmt.Errorf("invalid host %q. This MUST be the host only and does not include the scheme nor sub-paths.", host)
return nil, err
}
schemes := doc2.Schemes
if len(schemes) == 0 {
schemes = []string{"https://"}
schemes = []string{"https"}
}
basePath := doc2.BasePath
if basePath == "" {
basePath = "/"
}
for _, scheme := range schemes {
u := url.URL{
Scheme: scheme,
Expand Down
10 changes: 5 additions & 5 deletions openapi2conv/openapi2_conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func TestConvOpenAPIV3ToV2(t *testing.T) {
require.NoError(t, err)
}

spec2, err := FromV3(&doc3)
doc2, err := FromV3(&doc3)
require.NoError(t, err)
data, err := json.Marshal(spec2)
data, err := json.Marshal(doc2)
require.NoError(t, err)
require.JSONEq(t, exampleV2, string(data))
}
Expand All @@ -35,11 +35,11 @@ func TestConvOpenAPIV2ToV3(t *testing.T) {
err := json.Unmarshal([]byte(exampleV2), &doc2)
require.NoError(t, err)

spec3, err := ToV3(&doc2)
doc3, err := ToV3(&doc2)
require.NoError(t, err)
err = spec3.Validate(context.Background())
err = doc3.Validate(context.Background())
require.NoError(t, err)
data, err := json.Marshal(spec3)
data, err := json.Marshal(doc3)
require.NoError(t, err)
require.JSONEq(t, exampleV3, string(data))
}
Expand Down
1 change: 1 addition & 0 deletions openapi2conv/testdata/swagger.json
4 changes: 2 additions & 2 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
Value: value,
Schema: schema,
SchemaField: "pattern",
Reason: fmt.Sprintf("string doesn't match the regular expression \"%s\"", schema.Pattern),
Reason: fmt.Sprintf(`string doesn't match the regular expression "%s"`, schema.Pattern),
}
if !settings.multiError {
return err
Expand All @@ -1191,7 +1191,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
switch {
case f.regexp != nil && f.callback == nil:
if cp := f.regexp; !cp.MatchString(value) {
formatErr = fmt.Sprintf("string doesn't match the format %q (regular expression \"%s\")", format, cp.String())
formatErr = fmt.Sprintf(`string doesn't match the format %q (regular expression "%s")`, format, cp.String())
}
case f.regexp == nil && f.callback != nil:
if err := f.callback(value); err != nil {
Expand Down

0 comments on commit e9b36da

Please sign in to comment.