Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

feat: upgrade openapi-generator to v7.1.0 #172

Merged
merged 4 commits into from
Nov 14, 2023
Merged

feat: upgrade openapi-generator to v7.1.0 #172

merged 4 commits into from
Nov 14, 2023

Conversation

ctreatma
Copy link
Contributor

Upgrading the generator to v7.1.0 brings in required field validation, which will enable the SDK to determine the schema of an object in an API response based on the presence or absence of required fields. In addition, v7.1.0 has enum validation built in, so we no longer need a custom template to implement that.

In order to generate valid code I had to remove some new defaultValue code from api.mustache, since that template code does not correctly handle array properties.

Comment on lines +290 to +312
// This validates that all required properties are included in the JSON object
// by unmarshalling the object into a generic map with string keys and checking
// that every required field exists as a key in the generic map.
requiredProperties := []string{
"address",
"country",
"zip_code",
}

allProperties := make(map[string]interface{})

err = json.Unmarshal(bytes, &allProperties)

if err != nil {
return err
}

for _, requiredProperty := range requiredProperties {
if _, exists := allProperties[requiredProperty]; !exists {
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of the required property validation that is added in v7.1.0. Models with required properties are updated to check that the required property names appear in the JSON object that is being unmarshalled.

@@ -207,14 +207,14 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", s.Index(i).Interface(), "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", s.Index(i).Interface(), "{{collectionFormat}}")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an upstream change to use the unescaped value of baseName (see https://mustache.github.io/mustache.5.html#Variables for the difference between {{ and {{{).

@ctreatma ctreatma merged commit d09946e into main Nov 14, 2023
3 checks passed
@ctreatma ctreatma deleted the generator-upgrade branch November 14, 2023 16:53
Copy link
Contributor

This PR is included in version 0.27.0 🎉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants