From 67d02408bb155d450e883b179d22f932bafae96c Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 24 Mar 2022 16:53:14 -0400 Subject: [PATCH] fix: generation of openapi spec when there are no changes (#1304) * fix: generation of openapi spec when there are no changes Using reflect.Equal does not appear to work, there must be differences between how the spec is decoded from a file, and generated from types. This commit goes back to comparing the encoded version of the spec. That works better, but requires that we don't append servers, otherwise the old spec will always have different servers. All of the other fields we set are from writeOpenAPISpec replace any existing values, so it seems appropriate to do the same for Servers. * fix(openapi): replace date with product version This allows us to remove the comparison logic and simply re-generate the spec file every time. Co-authored-by: Michael Yang --- .github/release-please.json | 3 ++- Makefile | 1 + docs/api/openapi3.json | 2 +- internal/server/openapi.go | 52 ++++++++++--------------------------- internal/version.go | 10 ++++--- 5 files changed, 23 insertions(+), 45 deletions(-) diff --git a/.github/release-please.json b/.github/release-please.json index 6867f5d3e2..387f27a9b5 100644 --- a/.github/release-please.json +++ b/.github/release-please.json @@ -5,7 +5,8 @@ ".": { "release-type": "go", "extra-files": [ - "helm/charts/infra/Chart.yaml" + "helm/charts/infra/Chart.yaml", + "internal/version.go" ] } } diff --git a/Makefile b/Makefile index 2faad8929a..07dcdcffdc 100644 --- a/Makefile +++ b/Makefile @@ -94,5 +94,6 @@ openapi-lint: docs/api/openapi3.json @command -v openapi --version >/dev/null || { echo "openapi missing, try: npm install -g @redocly/openapi-cli" && exit 1; } openapi lint $< +.PHONY: docs/api/openapi3.json docs/api/openapi3.json: go run ./internal/openapigen $@ diff --git a/docs/api/openapi3.json b/docs/api/openapi3.json index 94dbe8113a..98b38ffc98 100644 --- a/docs/api/openapi3.json +++ b/docs/api/openapi3.json @@ -409,7 +409,7 @@ "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, "title": "Infra API", - "version": "2022-03-24" + "version": "0.6.1" }, "paths": { "/v1/access-keys": { diff --git a/internal/server/openapi.go b/internal/server/openapi.go index 96b4a860db..61d4441791 100644 --- a/internal/server/openapi.go +++ b/internal/server/openapi.go @@ -1,7 +1,6 @@ package server import ( - "bytes" "encoding/json" "fmt" "io" @@ -17,6 +16,7 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/infrahq/infra/api" + "github.com/infrahq/infra/internal" ) var ( @@ -216,62 +216,36 @@ func buildProperty(f reflect.StructField, t, parent reflect.Type, parentSchema * } } -func writeOpenAPISpec(version string, out io.Writer) error { - openAPISchema.OpenAPI = "3.0.0" - openAPISchema.Info = &openapi3.Info{ +func writeOpenAPISpec(spec openapi3.T, out io.Writer) error { + spec.OpenAPI = "3.0.0" + spec.Info = &openapi3.Info{ Title: "Infra API", - Version: version, + Version: internal.Version, Description: "Infra API", License: &openapi3.License{Name: "Apache 2.0", URL: "https://www.apache.org/licenses/LICENSE-2.0.html"}, } - openAPISchema.Servers = append(openAPISchema.Servers, &openapi3.Server{ - URL: "https://api.infrahq.com", - }) + spec.Servers = []*openapi3.Server{ + {URL: "https://api.infrahq.com"}, + } encoder := json.NewEncoder(out) encoder.SetIndent("", " ") - if err := encoder.Encode(openAPISchema); err != nil { + if err := encoder.Encode(spec); err != nil { return fmt.Errorf("failed to write schema: %w", err) } return nil } func WriteOpenAPISpecToFile(filename string) error { - old, err := readOpenAPISpec(filename) + fh, err := os.Create(filename) if err != nil { return err } - - version := time.Now().Format("2006-01-02") - old.Info.Version = version - - var buf bytes.Buffer - if err := writeOpenAPISpec(version, &buf); err != nil { - return err - } - - if reflect.DeepEqual(openAPISchema, old) { - // no changes to the schema - return nil - } - - // nolint: gosec // 0644 is the right mode - return os.WriteFile(filename, buf.Bytes(), 0o644) -} - -func readOpenAPISpec(filename string) (openapi3.T, error) { - spec := openapi3.T{} - - fh, err := os.Open(filename) - if err != nil { - return spec, fmt.Errorf("failed to create file: %w", err) - } defer fh.Close() - - if err := json.NewDecoder(fh).Decode(&spec); err != nil { - return spec, fmt.Errorf("failed to parse last openapi schema from %s: %w", filename, err) + if err := writeOpenAPISpec(openAPISchema, fh); err != nil { + return err } - return spec, nil + return nil } func setTagInfo(f reflect.StructField, t, parent reflect.Type, schema, parentSchema *openapi3.Schema) { diff --git a/internal/version.go b/internal/version.go index be52db4db3..f1d4037307 100644 --- a/internal/version.go +++ b/internal/version.go @@ -1,8 +1,10 @@ package internal var ( - Branch = "main" - Version = "0.0.0-development" - Commit = "" - Date = "" + Branch = "main" + // {x-release-please-start-version} + Version = "0.6.1" + // {x-release-please-end} + Commit = "" + Date = "" )