Skip to content

Commit

Permalink
add LoadFromStdin (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
const-tmp authored Jul 16, 2023
1 parent 4ba7452 commit b81a83e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ func main() {
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = *ext

doc, err := loader.LoadFromFile(filename)
var doc *openapi3.T
if filename == "-" {
doc, err = loader.LoadFromStdin()
} else {
doc, err = loader.LoadFromFile(filename)
}
if err != nil {
log.Fatalln("Loading error:", err)
}
Expand Down
11 changes: 11 additions & 0 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -130,6 +132,15 @@ func (loader *Loader) readURL(location *url.URL) ([]byte, error) {
return DefaultReadFromURI(loader, location)
}

// LoadFromStdin loads a spec from stdin
func (loader *Loader) LoadFromStdin() (*T, error) {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, fmt.Errorf("read from stdin: %w", err)
}
return loader.LoadFromData(data)
}

// LoadFromData loads a spec from a byte array
func (loader *Loader) LoadFromData(data []byte) (*T, error) {
loader.resetVisitedPathItemRefs()
Expand Down

0 comments on commit b81a83e

Please sign in to comment.