Skip to content

Commit e9b36da

Browse files
authored
Fix scheme handling in v2->v3 conversion (#441)
1 parent e83ebc0 commit e9b36da

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

openapi2conv/issue440_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package openapi2conv
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"os"
7+
"testing"
8+
9+
"github.com/getkin/kin-openapi/openapi2"
10+
"github.com/getkin/kin-openapi/openapi3"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestIssue440(t *testing.T) {
15+
doc2file, err := os.Open("testdata/swagger.json")
16+
require.NoError(t, err)
17+
defer doc2file.Close()
18+
var doc2 openapi2.T
19+
err = json.NewDecoder(doc2file).Decode(&doc2)
20+
require.NoError(t, err)
21+
22+
doc3, err := ToV3(&doc2)
23+
require.NoError(t, err)
24+
err = doc3.Validate(context.Background())
25+
require.NoError(t, err)
26+
require.Equal(t, openapi3.Servers{
27+
{URL: "https://petstore.swagger.io/v2"},
28+
{URL: "http://petstore.swagger.io/v2"},
29+
}, doc3.Servers)
30+
31+
doc2.Host = "your-bot-domain.de"
32+
doc2.Schemes = nil
33+
doc2.BasePath = ""
34+
doc3, err = ToV3(&doc2)
35+
require.NoError(t, err)
36+
err = doc3.Validate(context.Background())
37+
require.NoError(t, err)
38+
require.Equal(t, openapi3.Servers{
39+
{URL: "https://your-bot-domain.de/"},
40+
}, doc3.Servers)
41+
42+
doc2.Host = "https://your-bot-domain.de"
43+
doc2.Schemes = nil
44+
doc2.BasePath = ""
45+
doc3, err = ToV3(&doc2)
46+
require.Error(t, err)
47+
require.Contains(t, err.Error(), `invalid host`)
48+
}

openapi2conv/openapi2_conv.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ func ToV3(doc2 *openapi2.T) (*openapi3.T, error) {
2626
}
2727

2828
if host := doc2.Host; host != "" {
29+
if strings.Contains(host, "/") {
30+
err := fmt.Errorf("invalid host %q. This MUST be the host only and does not include the scheme nor sub-paths.", host)
31+
return nil, err
32+
}
2933
schemes := doc2.Schemes
3034
if len(schemes) == 0 {
31-
schemes = []string{"https://"}
35+
schemes = []string{"https"}
3236
}
3337
basePath := doc2.BasePath
38+
if basePath == "" {
39+
basePath = "/"
40+
}
3441
for _, scheme := range schemes {
3542
u := url.URL{
3643
Scheme: scheme,

openapi2conv/openapi2_conv_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func TestConvOpenAPIV3ToV2(t *testing.T) {
2323
require.NoError(t, err)
2424
}
2525

26-
spec2, err := FromV3(&doc3)
26+
doc2, err := FromV3(&doc3)
2727
require.NoError(t, err)
28-
data, err := json.Marshal(spec2)
28+
data, err := json.Marshal(doc2)
2929
require.NoError(t, err)
3030
require.JSONEq(t, exampleV2, string(data))
3131
}
@@ -35,11 +35,11 @@ func TestConvOpenAPIV2ToV3(t *testing.T) {
3535
err := json.Unmarshal([]byte(exampleV2), &doc2)
3636
require.NoError(t, err)
3737

38-
spec3, err := ToV3(&doc2)
38+
doc3, err := ToV3(&doc2)
3939
require.NoError(t, err)
40-
err = spec3.Validate(context.Background())
40+
err = doc3.Validate(context.Background())
4141
require.NoError(t, err)
42-
data, err := json.Marshal(spec3)
42+
data, err := json.Marshal(doc3)
4343
require.NoError(t, err)
4444
require.JSONEq(t, exampleV3, string(data))
4545
}

openapi2conv/testdata/swagger.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../openapi2/testdata/swagger.json

openapi3/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
11761176
Value: value,
11771177
Schema: schema,
11781178
SchemaField: "pattern",
1179-
Reason: fmt.Sprintf("string doesn't match the regular expression \"%s\"", schema.Pattern),
1179+
Reason: fmt.Sprintf(`string doesn't match the regular expression "%s"`, schema.Pattern),
11801180
}
11811181
if !settings.multiError {
11821182
return err
@@ -1191,7 +1191,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
11911191
switch {
11921192
case f.regexp != nil && f.callback == nil:
11931193
if cp := f.regexp; !cp.MatchString(value) {
1194-
formatErr = fmt.Sprintf("string doesn't match the format %q (regular expression \"%s\")", format, cp.String())
1194+
formatErr = fmt.Sprintf(`string doesn't match the format %q (regular expression "%s")`, format, cp.String())
11951195
}
11961196
case f.regexp == nil && f.callback != nil:
11971197
if err := f.callback(value); err != nil {

0 commit comments

Comments
 (0)