Skip to content

Commit

Permalink
feature: WS-279 fixed issue where environment variables were not bein…
Browse files Browse the repository at this point in the history
…g interpolated into spec
  • Loading branch information
akeemphilbert committed Apr 4, 2024
1 parent 3f99f60 commit 1b00b53
Show file tree
Hide file tree
Showing 3 changed files with 934 additions and 15 deletions.
33 changes: 18 additions & 15 deletions rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@ import (
func Config() (*openapi3.T, error) {
spec := os.Getenv("WEOS_SPEC")
if spec != "" {
turl, err := url.Parse(spec)
if err == nil {
return openapi3.NewLoader().LoadFromURI(turl)
} else {
//read the file
content, err := os.ReadFile(spec)
if err != nil {
return nil, err
//check if the spec is a file or a url
if strings.HasPrefix(spec, "http") {
turl, err := url.Parse(spec)
if err == nil {
return openapi3.NewLoader().LoadFromURI(turl)
}
//change the $ref to another marker so that it doesn't get considered an environment variable WECON-1
tempFile := strings.ReplaceAll(string(content), "$ref", "__ref__")
//replace environment variables in file
tempFile = os.ExpandEnv(string(tempFile))
tempFile = strings.ReplaceAll(string(tempFile), "__ref__", "$ref")
content = []byte(tempFile)
return openapi3.NewLoader().LoadFromData(content)
}

//read the file
content, err := os.ReadFile(spec)
if err != nil {
return nil, err
}
//change the $ref to another marker so that it doesn't get considered an environment variable WECON-1
tempFile := strings.ReplaceAll(string(content), "$ref", "__ref__")
//replace environment variables in file
tempFile = os.ExpandEnv(string(tempFile))
tempFile = strings.ReplaceAll(string(tempFile), "__ref__", "$ref")
content = []byte(tempFile)
return openapi3.NewLoader().LoadFromData(content)

}
return nil, errors.New("spec not found")
}
Expand Down
28 changes: 28 additions & 0 deletions rest/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rest_test

import (
"github.com/wepala/weos/v2/rest"
"os"
"testing"
)

func TestConfig(t *testing.T) {
os.Setenv("WEOS_SPEC", "./fixtures/api_with_environment_variables.yaml")
os.Setenv("DB_DRIVER", "sqlite3")
os.Setenv("BASE_PATH", "/local")
spec, err := rest.Config()
if err != nil {
t.Fatal(err)
}
if spec == nil {
t.Fatal("spec is nil")
}
if data, ok := spec.Extensions[rest.WeOSConfigExtension]; ok {
if basePath, ok := data.(map[string]interface{})["basePath"]; ok {
if basePath != "/local" {
t.Errorf("expected basePath to be /local but got %s", basePath)
}
}
}

}
Loading

0 comments on commit 1b00b53

Please sign in to comment.