diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index a1da5cffcb88..efb84f429330 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -4190,7 +4190,8 @@ public CodegenParameter fromParameter(Parameter parameter, Set imports) // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#user-content-parameterexplode codegenParameter.isExplode = parameter.getExplode() == null ? false : parameter.getExplode(); - // TODO revise collectionFormat + // TODO revise collectionFormat, default collection format in OAS 3 appears to multi at least for query parameters + // https://swagger.io/docs/specification/serialization/ String collectionFormat = null; if (ModelUtils.isArraySchema(parameterSchema)) { // for array parameter final ArraySchema arraySchema = (ArraySchema) parameterSchema; @@ -4346,14 +4347,21 @@ public CodegenParameter fromParameter(Parameter parameter, Set imports) if (codegenParameter.isQueryParam && codegenParameter.isDeepObject) { Schema schema = ModelUtils.getSchema(openAPI, codegenParameter.dataType); codegenParameter.items = fromProperty(codegenParameter.paramName, schema); - Map> properties = schema.getProperties(); - codegenParameter.items.vars = - properties.entrySet().stream() - .map(entry -> { - CodegenProperty property = fromProperty(entry.getKey(), entry.getValue()); - property.baseName = codegenParameter.baseName + "[" + entry.getKey() + "]"; - return property; - }).collect(Collectors.toList()); + // TODO Check why schema is actually null for a schema of type object defined inline + // https://swagger.io/docs/specification/serialization/ + if(schema != null) { + Map> properties = schema.getProperties(); + codegenParameter.items.vars = + properties.entrySet().stream() + .map(entry -> { + CodegenProperty property = fromProperty(entry.getKey(), entry.getValue()); + property.baseName = codegenParameter.baseName + "[" + entry.getKey() + "]"; + return property; + }).collect(Collectors.toList()); + } + else { + LOGGER.warn("No object schema found for deepObject parameter" + codegenParameter + " deepObject won't have specific properties"); + } } // set the parameter example value diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/ClientUtils.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/ClientUtils.mustache index e5fb0565fc9c..dd9a5f90fdcd 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/ClientUtils.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/ClientUtils.mustache @@ -63,6 +63,21 @@ namespace {{packageName}}.Client parameters.Add(name, ParameterToString(item)); } } + else if (value is IDictionary dictionary) + { + if(collectionFormat == "deepObject") { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(name + "[" + entry.Key + "]", ParameterToString(entry.Value)); + } + } + else { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(entry.Key.ToString(), ParameterToString(entry.Value)); + } + } + } else { parameters.Add(name, ParameterToString(value)); diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/api.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/api.mustache index 0825406d5530..40d3bfd6596f 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/api.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/api.mustache @@ -280,7 +280,17 @@ namespace {{packageName}}.{{apiPackage}} {{/pathParams}} {{#queryParams}} {{#required}} + {{#isDeepObject}} + {{#items.vars}} + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}.{{name}})); + {{/items.vars}} + {{^items}} + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("deepObject", "{{baseName}}", {{paramName}})); + {{/items}} + {{/isDeepObject}} + {{^isDeepObject}} localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})); + {{/isDeepObject}} {{/required}} {{^required}} if ({{paramName}} != null) @@ -292,6 +302,9 @@ namespace {{packageName}}.{{apiPackage}} localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}.{{name}})); } {{/items.vars}} + {{^items}} + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("deepObject", "{{baseName}}", {{paramName}})); + {{/items}} {{/isDeepObject}} {{^isDeepObject}} localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/ClientUtils.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/ClientUtils.cs index 844e7e9a8f4b..2b6e52d60654 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/ClientUtils.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net47/src/Org.OpenAPITools/Client/ClientUtils.cs @@ -67,6 +67,21 @@ public static Multimap ParameterToMultiMap(string collectionForm parameters.Add(name, ParameterToString(item)); } } + else if (value is IDictionary dictionary) + { + if(collectionFormat == "deepObject") { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(name + "[" + entry.Key + "]", ParameterToString(entry.Value)); + } + } + else { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(entry.Key.ToString(), ParameterToString(entry.Value)); + } + } + } else { parameters.Add(name, ParameterToString(value)); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/ClientUtils.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/ClientUtils.cs index 844e7e9a8f4b..2b6e52d60654 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/ClientUtils.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-net5.0/src/Org.OpenAPITools/Client/ClientUtils.cs @@ -67,6 +67,21 @@ public static Multimap ParameterToMultiMap(string collectionForm parameters.Add(name, ParameterToString(item)); } } + else if (value is IDictionary dictionary) + { + if(collectionFormat == "deepObject") { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(name + "[" + entry.Key + "]", ParameterToString(entry.Value)); + } + } + else { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(entry.Key.ToString(), ParameterToString(entry.Value)); + } + } + } else { parameters.Add(name, ParameterToString(value)); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ClientUtils.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ClientUtils.cs index 844e7e9a8f4b..2b6e52d60654 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ClientUtils.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ClientUtils.cs @@ -67,6 +67,21 @@ public static Multimap ParameterToMultiMap(string collectionForm parameters.Add(name, ParameterToString(item)); } } + else if (value is IDictionary dictionary) + { + if(collectionFormat == "deepObject") { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(name + "[" + entry.Key + "]", ParameterToString(entry.Value)); + } + } + else { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(entry.Key.ToString(), ParameterToString(entry.Value)); + } + } + } else { parameters.Add(name, ParameterToString(value)); diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ClientUtils.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ClientUtils.cs index 844e7e9a8f4b..2b6e52d60654 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ClientUtils.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ClientUtils.cs @@ -67,6 +67,21 @@ public static Multimap ParameterToMultiMap(string collectionForm parameters.Add(name, ParameterToString(item)); } } + else if (value is IDictionary dictionary) + { + if(collectionFormat == "deepObject") { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(name + "[" + entry.Key + "]", ParameterToString(entry.Value)); + } + } + else { + foreach (DictionaryEntry entry in dictionary) + { + parameters.Add(entry.Key.ToString(), ParameterToString(entry.Value)); + } + } + } else { parameters.Add(name, ParameterToString(value));