Skip to content

Commit

Permalink
fix: add guard against loading non-json files when reading references…
Browse files Browse the repository at this point in the history
… in schemas
  • Loading branch information
omissis committed Nov 8, 2023
1 parent e9cfe6c commit d31aeab
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"go/format"
"os"
"path/filepath"
"strings"

"github.com/atombender/go-jsonschema/internal/x/text"
Expand Down Expand Up @@ -58,6 +59,7 @@ var (
errMapURIToPackageName = errors.New("unable to map schema URI to Go package name")
errExpectedNamedType = errors.New("expected named type")
errUnsupportedRefFormat = errors.New("unsupported $ref format")
ErrUnsupportedRefExtension = errors.New("unsupported $ref extension")
errConflictSameFile = errors.New("conflict: same file")
errDefinitionDoesNotExistInSchema = errors.New("definition does not exist in schema")
errCannotGenerateReferencedType = errors.New("cannot generate referenced type")
Expand Down Expand Up @@ -321,6 +323,11 @@ func (g *schemaGenerator) generateReferencedType(ref string) (codegen.Type, erro
sg := g

if fileName != "" {
fileExt := filepath.Ext(fileName)
if fileExt != ".json" {
return nil, fmt.Errorf("%w: '%s', only json schema files are allowed", ErrUnsupportedRefExtension, fileExt)
}

var serr error

schema, serr = g.fileLoader.Load(fileName, g.schemaFileName)
Expand Down

0 comments on commit d31aeab

Please sign in to comment.