forked from getkin/kin-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix scheme handling in v2->v3 conversion (getkin#441)
- Loading branch information
1 parent
e83ebc0
commit c9ebcda
Showing
11 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package jsoninfo | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
// ExtractObjectKeys extracts the keys of an object in a JSON string. The keys | ||
// are returned in the order they appear in the JSON string. | ||
func ExtractObjectKeys(b []byte) ([]string, error) { | ||
if !bytes.HasPrefix(b, []byte{'{'}) { | ||
return nil, fmt.Errorf("expected '{' at start of JSON object") | ||
} | ||
|
||
dec := json.NewDecoder(bytes.NewReader(b)) | ||
var keys []string | ||
|
||
for dec.More() { | ||
// Read prop name | ||
t, err := dec.Token() | ||
if err != nil { | ||
log.Printf("Err: %v", err) | ||
break | ||
} | ||
|
||
name, ok := t.(string) | ||
if !ok { | ||
continue // May be a delimeter | ||
} | ||
|
||
keys = append(keys, name) | ||
|
||
var whatever nullMessage | ||
dec.Decode(&whatever) | ||
} | ||
|
||
return keys, nil | ||
} | ||
|
||
// nullMessage implements json.Unmarshaler and does nothing with the given | ||
// value. | ||
type nullMessage struct{} | ||
|
||
func (*nullMessage) UnmarshalJSON(data []byte) error { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package jsoninfo | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestExtractObjectKeys(t *testing.T) { | ||
const j = `{ | ||
"foo": {"bar": 1}, | ||
"baz": "qux", | ||
"quux": "quuz" | ||
}` | ||
|
||
keys, err := ExtractObjectKeys([]byte(j)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(keys, []string{"foo", "baz", "quux"}) { | ||
t.Fatalf("expected %v, got %v", []string{"foo", "baz", "quux"}, keys) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../openapi2/testdata/swagger.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters