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

Typescript client - generated parameter not typed as array for dictionary query params #3620

Open
RogueJay opened this issue Sep 6, 2021 · 1 comment

Comments

@RogueJay
Copy link

RogueJay commented Sep 6, 2021

I am raising this as it follow on from #3529, which I have determined in slightly different.

I'm trying to define a swagger file that will allow for URLs for in the following format
?filter[aField]=value&filter[otherField]=otherValue

I have worked out that the following swagger definition allows Swagger.UI to process the inputs properly, and create the correct URL

            "name": "filter",
            "in": "query",
            "style": "deepObject",
            "schema": {
              "type": "object",
              "additionalProperties": {
				  "type": "string"
				},
            }

The deepObject property is required for Swagger.UI to work correctly.

This nicely produces the typescript method definition of
filter: { [key: string]: string; } | undefined

However, the code to parse this query parameter is just defined as
url_ += "filter=" + encodeURIComponent("" + filter) + "&"

This is not how a dictionary for a query parameter. At least not a deepObject type. I think the following code would be required in Client.RequestUrl.liquid to replicate the desired approach

{%     elseif parameter.IsDictionary && parameter.IsDeepObject -%}
	for (let key in {{ parameter.VariableName }}) {
		if (Object.prototype.hasOwnProperty.call({{ parameter.VariableName }}, key)) {
			url_ += "{{ parameter.Name }}[" + key + "]=" + encodeURIComponent("" + {{ parameter.VariableName }}[key]) + "&";
		}
	};
@joskoanicic
Copy link

joskoanicic commented Aug 7, 2022

Was there any work on this?
The C# generator has the same problem it recognises the parameter as Dictionary and ends using this:

{% elsif parameter.IsDictionary -%}
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key) + "=").Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }

What would work is the modified lined used from the IsDeepObject segment:

% elsif parameter.IsDictionary and parameter.IsDeepObject -%}
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString("{{parameter.Name}}[" + item_.Key+ "]") + "=").Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }

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

No branches or pull requests

2 participants