diff --git a/README.md b/README.md index 9052de3f..2ea85c46 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ can import packages from `gnostic-models` instead of `gnostic`. [google/gnostic-grpc](https://github.com/google/gnostic-grpc) contains a gnostic plugin that can generate an annotated Protocol Buffer description of an -API that, when transcode, produces an API that conforms to a specified OpenAPI +API that, when transcoded, produces an API that conforms to a specified OpenAPI document. To go from protobuf to OpenAPI, see the [protoc-gen-openapi](cmd/protoc-gen-openapi) tool in this project. @@ -69,12 +69,12 @@ and later. git clone https://github.com/google/gnostic cd gnostic -2. Verify that you have a local installation of `protoc`. You can get protoc +2. Verify that you have a local installation of `protoc`. You can get `protoc` [here](https://github.com/protocolbuffers/protobuf). 3. Build **gnostic** with `make`. This uses [go generate](https://blog.golang.org/generate) to build support code - including code generated by `protoc` and the Go protoc plugin, which is + including code generated by `protoc` and the Go `protoc` plugin, which is automatically downloaded from [github.com/golang/protobuf](https://github.com/golang/protobuf) by the [COMPILE-PROTOS.sh](COMPILE-PROTOS.sh) script. This also builds all plugins diff --git a/generate-gnostic/generate-extension.go b/generate-gnostic/generate-extension.go index b8456185..27270d30 100644 --- a/generate-gnostic/generate-extension.go +++ b/generate-gnostic/generate-extension.go @@ -281,6 +281,9 @@ func generateExtension(schemaFile string, outDir string) error { return err } err = exec.Command(runtime.GOROOT()+"/bin/gofmt", "-w", goFilename).Run() + if err != nil { + return err + } // generate the main file. diff --git a/generate-gnostic/generate-extension_test.go b/generate-gnostic/generate-extension_test.go index 9ec62be5..344848f8 100644 --- a/generate-gnostic/generate-extension_test.go +++ b/generate-gnostic/generate-extension_test.go @@ -16,6 +16,9 @@ func TestErrorExtensionGeneratorUnsupportedPrimitive(t *testing.T) { "test/x-unsupportedprimitives.json", "--out_dir=/tmp", ).Output() + if err != nil { + t.Fatalf("error executing generate-gnostic: %v", err) + } outputFile := "x-unsupportedprimitives.errors" _ = ioutil.WriteFile(outputFile, output, 0644) @@ -38,6 +41,9 @@ func TestErrorExtensionGeneratorNameCollision(t *testing.T) { "test/x-extension-name-collision.json", "--out_dir=/tmp", ).Output() + if err != nil { + t.Fatalf("error executing generate-gnostic: %v", err) + } outputFile := "x-extension-name-collision.errors" _ = ioutil.WriteFile(outputFile, output, 0644) diff --git a/jsonwriter/writer.go b/jsonwriter/writer.go index cc0eaccb..7da769c5 100644 --- a/jsonwriter/writer.go +++ b/jsonwriter/writer.go @@ -18,19 +18,15 @@ import ( "bytes" "errors" "fmt" - "strings" + "strconv" "gopkg.in/yaml.v3" ) -const indentation = " " - -// basic escaping, will need to be improved or replaced -func escape(s string) string { - s = strings.Replace(s, "\n", "\\n", -1) - s = strings.Replace(s, "\"", "\\\"", -1) - return s -} +const ( + indentation = " " + null = "null" +) type writer struct { b bytes.Buffer @@ -85,15 +81,15 @@ func (w *writer) writeScalar(node *yaml.Node, indent string) { } switch node.Tag { case "!!str": - w.writeString("\"") - w.writeString(escape(node.Value)) - w.writeString("\"") + w.writeString(strconv.Quote(node.Value)) case "!!int": w.writeString(node.Value) case "!!float": w.writeString(node.Value) case "!!bool": w.writeString(node.Value) + case "!!null": + w.writeString(null) } } diff --git a/jsonwriter/writer_test.go b/jsonwriter/writer_test.go index b714e609..28f28a29 100644 --- a/jsonwriter/writer_test.go +++ b/jsonwriter/writer_test.go @@ -37,6 +37,8 @@ func TestMarshal(t *testing.T) { scalarBoolTestCase(), scalarFloatTestCase(), scalarIntTestCase(), + scalarStringTestCase(), + scalarNullTestCase(), sequenceStringArrayTestCase(), sequenceBoolArrayTestCase(), sequenceFloatArrayTestCase(), @@ -88,6 +90,22 @@ func scalarFloatTestCase() *MarshalTestCase { } } +func scalarStringTestCase() *MarshalTestCase { + return &MarshalTestCase{ + Name: "scalar string", + Node: compiler.NewScalarNodeForString("a\\b\nc\""), + Expected: "\"a\\\\b\\nc\\\"\"\n", + } +} + +func scalarNullTestCase() *MarshalTestCase { + return &MarshalTestCase{ + Name: "scalar null", + Node: compiler.NewNullNode(), + Expected: "null\n", + } +} + func scalarIntTestCase() *MarshalTestCase { return &MarshalTestCase{ Name: "scalar int", diff --git a/metrics/vocabulary/vocabulary.go b/metrics/vocabulary/vocabulary.go index cbca53f9..a0b01aa7 100644 --- a/metrics/vocabulary/vocabulary.go +++ b/metrics/vocabulary/vocabulary.go @@ -43,10 +43,11 @@ func WriteCSV(v *metrics.Vocabulary, filename string) error { filename = "vocabulary-operation.csv" } f4, ferror := os.Create(filename) - defer f4.Close() if ferror != nil { return ferror } + defer f4.Close() + for _, s := range v.Schemas { temp := fmt.Sprintf("%s,\"%s\",%d\n", "schemas", s.Word, int(s.Count)) f4.WriteString(temp) diff --git a/openapiv3/README.md b/openapiv3/README.md index 0b015bce..5184f7fa 100644 --- a/openapiv3/README.md +++ b/openapiv3/README.md @@ -7,13 +7,13 @@ Gnostic applications and plugins can use OpenAPIv3.proto to generate Protocol Buffer support code for their preferred languages. OpenAPIv3.go is used by Gnostic to read JSON and YAML OpenAPI descriptions into -the Protocol Buffer-based datastructures generated from OpenAPIv3.proto. +the Protocol Buffer-based data structures generated from OpenAPIv3.proto. OpenAPIv3.proto and OpenAPIv3.go are generated by the Gnostic compiler -generator, and OpenAPIv3.pb.go is generated by protoc, the Protocol Buffer -compiler, and protoc-gen-go, the Protocol Buffer Go code generation plugin. +generator, and OpenAPIv3.pb.go is generated by `protoc`, the Protocol Buffer +compiler, and `protoc-gen-go`, the Protocol Buffer Go code generation plugin. -openapi-3.1.json is a JSON schema for OpenAPI 3.1 that is automatically +`openapi-3.1.json` is a JSON schema for OpenAPI 3.1 that is automatically generated from the OpenAPI 3.1 specification. It is not an official JSON Schema for OpenAPI. diff --git a/plugins/gnostic-analyze/summarize/main.go b/plugins/gnostic-analyze/summarize/main.go index 7056ee82..746d320f 100644 --- a/plugins/gnostic-analyze/summarize/main.go +++ b/plugins/gnostic-analyze/summarize/main.go @@ -32,6 +32,9 @@ var stats []statistics.DocumentStatistics // walker is called for each summary file found. func walker(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } basename := path.Base(p) if basename != "summary.json" { return nil