Skip to content
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

[go-server] Set default values in object properties #18532

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rledisez
Copy link
Contributor

When a default value is set for an object property, ensure it is set into the struct before decoding the JSON body.

Fix #4579

@rledisez
Copy link
Contributor Author

By applying the default values, this make the usage of default values of a post body consistent with the default values of query parameters

Any feedbacks appreciated @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5

@lwj5
Copy link
Contributor

lwj5 commented May 6, 2024

Does the default work when there are nested objects?

such as embedded struct, and when a struct is a type for one of the fields.

@rledisez
Copy link
Contributor Author

Does the default work when there are nested objects?

such as embedded struct, and when a struct is a type for one of the fields.

The previous version was not working in that case. It was just handling basic types (string, int, bool). I took inspiration of the function NewObjectWithDefaults from the "go" templates to create a function that create a struct with defaults for each type and call it when required. I think it does what you asked for.

When a default value is set for an object property, ensure it is set
into the struct before decoding the JSON body.

Fix OpenAPITools#4579
@lwj5
Copy link
Contributor

lwj5 commented May 19, 2024

could you add some tests that generates as such:

struct A {
S1 string
}

struct B {
A
S2 string
}

struct C {
SomeA A
S3 string
}

This is B embeds A, and C has a field of type A. Make sure the code is generated correctly if S1 has a default.

@wing328 wing328 modified the milestones: 7.6.0, 7.7.0 May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Default values, min, max, etc. are missing from generated Go models for object properties
3 participants