Skip to content

Commit

Permalink
Support referencing other schemas that have no output file.
Browse files Browse the repository at this point in the history
  • Loading branch information
atombender committed Oct 5, 2018
1 parent d61cc2a commit caafad8
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
2 changes: 0 additions & 2 deletions cmd/gojsonschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ var rootCmd = &cobra.Command{
}
if s, ok := schemaOutputMap[id]; ok {
mapping.OutputName = s
} else {
mapping.OutputName = defaultOutput
}
if s, ok := schemaRootTypeMap[id]; ok {
mapping.RootType = s
Expand Down
8 changes: 5 additions & 3 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ func New(config Config) (*Generator, error) {
config: config,
outputs: map[string]*output{},
schemaCacheByFileName: map[string]*schemas.Schema{},
inScope: map[qualifiedDefinition]struct{}{},
warner: config.Warner,
}, nil
}

func (g *Generator) Sources() map[string][]byte {
sources := make(map[string]*strings.Builder, len(g.outputs))
for _, output := range g.outputs {
if output.file.FileName == "" {
continue
}
emitter := codegen.NewEmitter(80)
output.file.Generate(emitter)

Expand Down Expand Up @@ -155,9 +160,6 @@ func (g *Generator) findOutputFileForSchemaID(id string) (*output, error) {
func (g *Generator) beginOutput(
id string,
outputName, packageName string) (*output, error) {
if outputName == "" {
return nil, fmt.Errorf("unable to map schema URI %q to a file name", id)
}
if packageName == "" {
return nil, fmt.Errorf("unable to map schema URI %q to a Go package name", id)
}
Expand Down
14 changes: 14 additions & 0 deletions tests/data/crossPackageNoOutput/other.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/other",
"definitions": {
"Thing": {
"type": "object",
"properties": {
"s": {
"type": "string"
}
}
}
}
}
18 changes: 18 additions & 0 deletions tests/data/crossPackageNoOutput/schema.go.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.

package schema

import other "github.com/example/other"

type Thing struct {
// S corresponds to the JSON schema field "s".
S *string `json:"s,omitempty"`
}

type Schema struct {
// DefInOtherSchema corresponds to the JSON schema field "defInOtherSchema".
DefInOtherSchema *other.Thing `json:"defInOtherSchema,omitempty"`

// DefInSameSchema corresponds to the JSON schema field "defInSameSchema".
DefInSameSchema *Thing `json:"defInSameSchema,omitempty"`
}
23 changes: 23 additions & 0 deletions tests/data/crossPackageNoOutput/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/schema",
"type": "object",
"properties": {
"defInSameSchema": {
"$ref": "#/Definitions/Thing"
},
"defInOtherSchema": {
"$ref": "other.json#/Definitions/Thing"
}
},
"definitions": {
"Thing": {
"type": "object",
"properties": {
"s": {
"type": "string"
}
}
}
}
}
16 changes: 16 additions & 0 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ func TestCrossPackage(t *testing.T) {
testExampleFile(t, cfg, "./data/crossPackage/schema.json")
}

func TestCrossPackageNoOutput(t *testing.T) {
cfg := basicConfig
cfg.SchemaMappings = []generator.SchemaMapping{
{
SchemaID: "https://example.com/schema",
PackageName: "github.com/example/schema",
OutputName: "schema.go",
},
{
SchemaID: "https://example.com/other",
PackageName: "github.com/example/other",
},
}
testExampleFile(t, cfg, "./data/crossPackageNoOutput/schema.json")
}

func TestCapitalization(t *testing.T) {
cfg := basicConfig
cfg.Capitalizations = []string{"ID", "URL", "HtMl"}
Expand Down

0 comments on commit caafad8

Please sign in to comment.