Skip to content

Commit f1873fa

Browse files
authored
Fix/generator issues (#137)
* fix: reenable cdn service after generator fix * fix: set import paths to fixed values as they are coming from a static, not dynamic, repository * fix: use unique variable name * chore: Add convenience target to download alpha apis * fix: Workaround for a bug in the openapi generator that returns an unspecified type for a path parameter * fix: correctly handle bitsizes and nullability of numeric attributes * fix: add exceptions for float32 and int32 to keep bug-compatibility with previous generated code * fix: introduced specialized types * fix: generation working without compile issues, but broken tests * fix: beta apis compiling now * fix: use getter-id instead of plain name to generate non-conflicting names * fix: Readd json tags * chore: remove debug code * fix: correct testcase generation * fix: fix return type instantiation in testcase * fix: correct instantiation of returntype in testcase * feat: provide service-individual, explicit api versions via json file * fix: switch back to main for service-enablement api * fix: handle non-existing version gracefully by falling back to main * feat: rename file to api-versions-lock.json to emphasize semantics
1 parent cfb20c2 commit f1873fa

12 files changed

+568
-198
lines changed

Diff for: Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ROOT_DIR ?= $(shell git rev-parse --show-toplevel)
22
SCRIPTS_BASE ?= $(ROOT_DIR)/scripts
3-
API_VERSION ?= $(shell cat api_version|grep -v '^\#'|head -n 1)
3+
API_VERSIONS ?= $(ROOT_DIR)/api-versions-lock.json
44
SDK_BRANCH ?= main
55

66
# SETUP AND TOOL INITIALIZATION TASKS
@@ -12,7 +12,7 @@ project-tools:
1212

1313
# GENERATE
1414
download-oas:
15-
@$(SCRIPTS_BASE)/download-oas.sh "$(OAS_REPO_NAME)" "$(OAS_REPO)" "$(ALLOW_ALPHA)" "$(API_VERSION)"
15+
@$(SCRIPTS_BASE)/download-oas.sh "$(OAS_REPO_NAME)" "$(OAS_REPO)" "$(ALLOW_ALPHA)" "$(API_VERSIONS)"
1616

1717
generate-sdk:
1818
@$(SCRIPTS_BASE)/generate-sdk/generate-sdk.sh "$(GIT_HOST)" "$(GIT_USER_ID)" "$(GIT_REPO_ID)" "$(SDK_REPO_URL)" "$(LANGUAGE)" "$(SDK_BRANCH)"

Diff for: api-versions-lock.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"load-balancer": "9c9de238a3e650f4bed9b9d9ade151da9a97e1d6",
3+
"certificates": "c592757243833f9b506a7d23ec5a80a9b3c984ae"
4+
}

Diff for: api_version

-3
This file was deleted.

Diff for: blacklist.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# Transitional file to disable generation for selected services
2-
cdn
1+
# Transitional file to disable generation for selected services

Diff for: scripts/download-oas.sh

+16-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ROOT_DIR=$(git rev-parse --show-toplevel)
66
OAS_REPO_NAME=$1
77
OAS_REPO=$2
88
ALLOW_ALPHA=$3
9-
OAS_API_VERSION=$4
9+
OAS_API_VERSIONS=$4
1010

1111
if [[ -z ${OAS_REPO_NAME} ]]; then
1212
echo "Repo name is empty, default public OAS repo name will be used."
@@ -18,9 +18,9 @@ if [[ ! ${OAS_REPO} || -d ${OAS_REPO} ]]; then
1818
OAS_REPO="https://github.com/stackitcloud/${OAS_REPO_NAME}.git"
1919
fi
2020

21-
if [[ -z ${OAS_API_VERSION} ]]; then
22-
echo "No API version passed, main branch will be used"
23-
OAS_API_VERSION="main"
21+
if [[ -z ${OAS_API_VERSIONS} ]]; then
22+
echo "No API version passed, using ${ROOTDIR}/api-versions-lock.json"
23+
OAS_API_VERSIONS="${ROOT_DIR}/api-versions-lock.json"
2424
fi
2525

2626
# Create temp directory to clone OAS repo
@@ -41,16 +41,23 @@ mkdir ${ROOT_DIR}/oas
4141
cd ${work_dir}
4242
git clone ${OAS_REPO} --quiet
4343

44-
echo "Using api version ${OAS_API_VERSION}"
45-
cd ${OAS_REPO_NAME}
46-
git checkout --quiet ${OAS_API_VERSION}
47-
cd -
48-
4944
for service_dir in ${work_dir}/${OAS_REPO_NAME}/services/*; do
45+
5046
max_version_dir=""
5147
max_version=-1
5248
service=$(basename "$service_dir")
5349

50+
apiVersion=$(jq -r -f <(cat <<EOF
51+
if has("${service}") then ."${service}" else "main" end
52+
EOF
53+
) ${OAS_API_VERSIONS})
54+
if [ "${apiVersion}" != "main" ]; then
55+
echo "Using ${apiVersion} for ${service}"
56+
fi
57+
cd ${work_dir}/${OAS_REPO_NAME} > /dev/null
58+
git checkout -q $apiVersion || (echo "version ${apiVersion} does not exist, using main instead" && git checkout -q main)
59+
cd - > /dev/null
60+
5461
# Prioritize GA over Beta over Alpha versions
5562
# GA priority = 3, Beta priority = 2, Alpha priority = 1
5663
max_version_priority=1

Diff for: templates/go/api.mustache

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/config"
12-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/oapierror"
11+
"github.com/stackitcloud/stackit-sdk-go/core/config"
12+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1313
{{#imports}} "{{import}}"
1414
{{/imports}}
1515
)

Diff for: templates/go/api_test.mustache

+18-7
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ import (
1919
"net/url"
2020
"strings"
2121
"testing"
22-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/config"
22+
"github.com/stackitcloud/stackit-sdk-go/core/config"
2323
)
2424

2525
func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
2626
2727
{{#operations}}
2828
{{#operation}}
2929
t.Run("Test {{classname}}Service {{{nickname}}}", func(t *testing.T) {
30-
path := "{{{path}}}"{{#pathParams}}
31-
{{paramName}}Value := {{#isString}}"{{paramName}}"{{/isString}}{{#isNumber}}123{{/isNumber}}{{#isFloat}}float32(123){{/isFloat}}{{#isDouble}}float64(123){{/isDouble}}{{#isInteger}}int32(123){{/isInteger}}{{#isLong}}int64(123){{/isLong}}{{^isString}}{{^isInteger}}{{defaultValue}}{{/isInteger}}{{/isString}}
32-
path = strings.Replace(path, "{"+"{{baseName}}"+"}", url.PathEscape(ParameterValueToString({{paramName}}Value, "{{paramName}}")), -1){{/pathParams}}
30+
_apiUrlPath := "{{{path}}}"{{#pathParams}}
31+
{{paramName}}Value := {{#isAnyType}}"unspecified type"{{/isAnyType}}{{#isString}}{{#isUuid}}uuid.NewString(){{/isUuid}}{{^isUuid}}"{{paramName}}"{{/isUuid}}{{/isString}}{{#isNumber}}123{{/isNumber}}{{#isFloat}}float32(123){{/isFloat}}{{#isDouble}}float64(123){{/isDouble}}{{#isInteger}}int32(123){{/isInteger}}{{#isLong}}int64(123){{/isLong}}{{^isString}}{{^isInteger}}{{defaultValue}}{{/isInteger}}{{/isString}}
32+
_apiUrlPath = strings.Replace(_apiUrlPath, "{"+"{{baseName}}"+"}", url.PathEscape(ParameterValueToString({{paramName}}Value, "{{paramName}}")), -1){{/pathParams}}
3333

3434
test{{classname}}ServeMux := http.NewServeMux()
35-
test{{classname}}ServeMux.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
35+
test{{classname}}ServeMux.HandleFunc(_apiUrlPath, func(w http.ResponseWriter, req *http.Request) {
3636
{{! Only binary data sets a ReturnFormat}}
3737
{{#returnFormat}}
3838
w.Header().Add("Content-Type", "application/octet-stream")
@@ -41,7 +41,18 @@ func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
4141
{{/returnFormat}}
4242
{{^returnFormat}}
4343
{{#returnType}}
44+
{{#returnProperty.isPrimitiveType}}
45+
{{! according to the model, freeform objects might be primitive...}}
46+
{{#returnProperty.isFreeFormObject}}
4447
data := {{{.}}}{}
48+
{{/returnProperty.isFreeFormObject}}
49+
{{^returnProperty.isFreeFormObject}}
50+
var data {{{.}}}
51+
{{/returnProperty.isFreeFormObject}}
52+
{{/returnProperty.isPrimitiveType}}
53+
{{^returnProperty.isPrimitiveType}}
54+
data := {{{.}}}{}
55+
{{/returnProperty.isPrimitiveType}}
4556
w.Header().Add("Content-Type", "application/json")
4657
json.NewEncoder(w).Encode(data)
4758
{{/returnType}}
@@ -79,7 +90,7 @@ func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
7990
{{#allParams}}
8091
{{#required}}
8192
{{#isPathParam}}
82-
{{paramName}} := {{#isString}}"{{paramName}}"{{/isString}}{{#isNumber}}123{{/isNumber}}{{#isFloat}}float32(123){{/isFloat}}{{#isDouble}}float64(123){{/isDouble}}{{#isInteger}}int32(123){{/isInteger}}{{#isLong}}int64(123){{/isLong}}{{^isString}}{{^isInteger}}{{defaultValue}}{{/isInteger}}{{/isString}}
93+
{{paramName}} := {{paramName}}Value
8394
{{/isPathParam}}
8495
{{^isPathParam}}
8596
{{#isPrimitiveType}}
@@ -98,7 +109,7 @@ func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
98109
t.Fatalf("error in call: %v", reqErr)
99110
}
100111
{{#returnType}}
101-
if resp == nil {
112+
if IsNil(resp) {
102113
t.Fatalf("response not present")
103114
}
104115
{{/returnType}}

Diff for: templates/go/client.mustache

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"strings"
2222
"time"
2323
"unicode/utf8"
24-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/config"
25-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/auth"
24+
"github.com/stackitcloud/stackit-sdk-go/core/config"
25+
"github.com/stackitcloud/stackit-sdk-go/core/auth"
2626

2727
{{#hasOAuthMethods}}
2828
"golang.org/x/oauth2"

Diff for: templates/go/configuration.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package {{packageName}}
33

44
import (
5-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/config"
5+
"github.com/stackitcloud/stackit-sdk-go/core/config"
66
)
77

88
// NewConfiguration returns a new Configuration object

Diff for: templates/go/go.mod.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module {{gitHost}}/{{gitUserId}}/{{gitRepoId}}/services{{#isGoSubmodule}}/{{pack
33
go 1.21
44

55
require (
6-
{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core v0.16.0
6+
github.com/stackitcloud/stackit-sdk-go/core v0.16.0
77
)

0 commit comments

Comments
 (0)