Skip to content

Commit

Permalink
fix: generation of openapi spec when there are no changes (#1304)
Browse files Browse the repository at this point in the history
* 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 <michael.yang@infrahq.com>
  • Loading branch information
dnephin and mxyng authored Mar 24, 2022
1 parent 7db5da2 commit 67d0240
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .github/release-please.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
".": {
"release-type": "go",
"extra-files": [
"helm/charts/infra/Chart.yaml"
"helm/charts/infra/Chart.yaml",
"internal/version.go"
]
}
}
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
2 changes: 1 addition & 1 deletion docs/api/openapi3.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
52 changes: 13 additions & 39 deletions internal/server/openapi.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"

"github.com/infrahq/infra/api"
"github.com/infrahq/infra/internal"
)

var (
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions internal/version.go
Original file line number Diff line number Diff line change
@@ -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 = ""
)

0 comments on commit 67d0240

Please sign in to comment.