Skip to content

fix(go): prevent infinite recursion during json unmarshal of enums #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions openapi-generator-config-go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
templateDir: templates/go
files:
custom/model_test.mustache:
# TODO: this should be 'ModelTests' instead of 'Model'
# 'Model' was used because 'ModelTests' didn't stick to golangs file naming conventions
templateType: Model
destinationFilename: _test.go
7 changes: 4 additions & 3 deletions scripts/generate-sdk/languages/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,16 @@ generate_go_sdk() {
--input-spec ${service_json} \
--output ${SERVICES_FOLDER}/${service} \
--package-name ${service} \
--template-dir ${ROOT_DIR}/templates/go/ \
--enable-post-process-file \
--git-host ${GIT_HOST} \
--git-user-id ${GIT_USER_ID} \
--git-repo-id ${GIT_REPO_ID} \
--global-property apis,models,modelTests=true,modelDocs=false,apiDocs=false,supportingFiles \
--additional-properties=isGoSubmodule=true,enumClassPrefix=true,generateInterfaces=true,$regional_api \
--http-user-agent stackit-sdk-go/${service} \
--reserved-words-mappings type=types
--http-user-agent stackit-sdk-go/${service} \
--reserved-words-mappings type=types \
--config openapi-generator-config-go.yml

# Remove unnecessary files
rm ${SERVICES_FOLDER}/${service}/.openapi-generator-ignore
rm ${SERVICES_FOLDER}/${service}/.openapi-generator/FILES
Expand Down
49 changes: 49 additions & 0 deletions templates/go/custom/model_simple_test.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{{! NOTE: This is a custom STACKIT template which is not present in upsteam to support testing of enum models}}

{{#vars}}
{{! special handling for enums}}
{{#isEnumRef}}
{{#isEnum}}
// isEnum

func Test{{{classname}}}{{#lambda.titlecase}}{{nameInCamelCase}}{{/lambda.titlecase}}_UnmarshalJSON(t *testing.T) {
type args struct {
src []byte
}
tests := []struct {
name string
args args
wantErr bool
}{
{{#allowableValues}}
{{#enumVars}}
{
name: `success - possible enum value no. {{-index}}`,
args: args{
src: []byte(`{{{value}}}`),
},
wantErr: false,
},
{{/enumVars}}
{{/allowableValues}}
{
name: "fail",
args: args{
src: []byte("\"FOOBAR\""),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := {{{classname}}}{{#lambda.titlecase}}{{nameInCamelCase}}{{/lambda.titlecase}}({{#isNumeric}}-1{{/isNumeric}}{{^isNumeric}}""{{/isNumeric}})
if err := v.UnmarshalJSON(tt.args.src); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

{{/isEnum}}
{{/isEnumRef}}
{{/vars}}
20 changes: 20 additions & 0 deletions templates/go/custom/model_test.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{#models}}

{{>partial_header}}
package {{packageName}}

import (
"testing"
{{#imports}}
"{{import}}"
{{/imports}}
)

{{#model}}
{{^isEnum}}
{{^oneOf}}{{^anyOf}}
{{>custom/model_simple_test}}
{{/anyOf}}{{/oneOf}}
{{/isEnum}}
{{/model}}
{{/models}}
7 changes: 5 additions & 2 deletions templates/go/model_simple.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,16 @@ var Allowed{{{classname}}}{{#lambda.titlecase}}{{nameInCamelCase}}{{/lambda.titl
}

func (v *{{{classname}}}{{#lambda.titlecase}}{{nameInCamelCase}}{{/lambda.titlecase}}) UnmarshalJSON(src []byte) error {
var value {{{classname}}}{{#lambda.titlecase}}{{nameInCamelCase}}{{/lambda.titlecase}}
// use a type alias to prevent infinite recursion during unmarshal,
// see https://biscuit.ninja/posts/go-avoid-an-infitine-loop-with-custom-json-unmarshallers
type TmpJson {{{classname}}}{{#lambda.titlecase}}{{nameInCamelCase}}{{/lambda.titlecase}}
var value TmpJson
err := json.Unmarshal(src, &value)
if err != nil {
return err
}
// Allow unmarshalling zero value for testing purposes
var zeroValue {{{classname}}}{{#lambda.titlecase}}{{nameInCamelCase}}{{/lambda.titlecase}}
var zeroValue TmpJson
if value == zeroValue {
return nil
}
Expand Down
Loading