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

[REQ][Rust] Add additionalProperties to the generated code #20965

Open
Vardaleb opened this issue Mar 25, 2025 · 3 comments
Open

[REQ][Rust] Add additionalProperties to the generated code #20965

Vardaleb opened this issue Mar 25, 2025 · 3 comments

Comments

@Vardaleb
Copy link

Is your feature request related to a problem? Please describe.

Some openAPI specs do not contain every property they actually return. Right now it is not possible to access those properties

Describe the solution you'd like

The solution would be to include all undefined properties in the generated code, by modifying the model.mustache file like this:

--- /proc/self/fd/14	2025-03-25 10:57:36.081920827 +0100
+++ templates/model.mustache	2025-03-25 09:53:47.536340813 +0100
@@ -148,6 +148,8 @@
     ### Option End (and trailing comma)
     }}{{#isNullable}}>{{/isNullable}}{{^required}}>{{/required}},
 {{/vars}}
+    #[serde(flatten)]
+    pub additional_properties: std::collections::HashMap<String, serde_json::Value>,
 }
 
 impl {{{classname}}} {
@@ -169,6 +171,7 @@
             {{#vars}}
             {{{name}}}{{^required}}: None{{/required}}{{#required}}{{#isModel}}{{^avoidBoxedModels}}: {{^isNullable}}Box::new({{{name}}}){{/isNullable}}{{#isNullable}}if let Some(x) = {{{name}}} {Some(Box::new(x))} else {None}{{/isNullable}}{{/avoidBoxedModels}}{{/isModel}}{{/required}},
             {{/vars}}
+            additional_properties: std::collections::HashMap::new(),
         }
     }
 }
@Vardaleb
Copy link
Author

For now, I have copied the original file and modified it in my project. But this is not very sustainable, when the original file changes in he future

@JMLX42
Copy link

JMLX42 commented Mar 27, 2025

@Vardaleb would you mind opening a PR for this?

Vardaleb added a commit to Vardaleb/openapi-generator that referenced this issue Mar 28, 2025
@NoxTav
Copy link

NoxTav commented Apr 4, 2025

@Vardaleb About your PR, I've been working on the same issue on my side, and your fix adds the field to all the models, whereas we should know from the openapi.json file which have additionalProperties set.

I encountered 2 types of additionalProperties, so here is my proposed fix:

{{!### in the struct}}
{{#composedSchemas.allOf}}
{{#additionalProperties}}
    #[serde(flatten)]
    pub additional_properties: std::collections::HashMap<String, serde_json::Value>,
{{/additionalProperties}}
{{/composedSchemas.allOf}}
{{^composedSchemas.allOf}}
{{#isAdditionalPropertiesTrue}}
    #[serde(flatten)]
    pub additional_properties: std::collections::HashMap<String, serde_json::Value>,
{{/isAdditionalPropertiesTrue}}
{{/composedSchemas.allOf}}

{{!### in new()}}
{{!### allOf is an array}}
{{#composedSchemas.allOf}}
{{#additionalProperties}}
           additional_properties: std::collections::HashMap::new(),
{{/additionalProperties}}
{{/composedSchemas.allOf}}
{{^composedSchemas.allOf}}
{{#isAdditionalPropertiesTrue}}
           additional_properties: std::collections::HashMap::new(),
{{/isAdditionalPropertiesTrue}}
{{/composedSchemas.allOf}}

In my test case, I have a map of String to String and a map of String to JSON.

Maybe in the second case, we could use additionalPropertyType to set the type dynamically, but I didn't test it and wouldn't know how to set the value by default.

And there might be other ways to set additionalProperties, I haven't encountered.

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

No branches or pull requests

3 participants