From ca61997814c1abc833c51925c7632f8d4d21143f Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 25 Aug 2020 17:03:14 +0800 Subject: [PATCH 1/2] add x-is-json to csharp generators --- .../openapitools/codegen/DefaultCodegen.java | 24 +++++++++++++++++++ .../languages/AbstractCSharpCodegen.java | 13 ++++++++++ .../languages/HaskellHttpClientCodegen.java | 4 ---- .../codegen/languages/JavaClientCodegen.java | 22 ----------------- .../src/main/resources/csharp/api.mustache | 4 ++-- 5 files changed, 39 insertions(+), 28 deletions(-) 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 007ce53c713e..b15c8c11459b 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 @@ -6507,4 +6507,28 @@ protected Schema getAdditionalProperties(Schema schema) { return ModelUtils.getAdditionalProperties(openAPI, schema); } + final protected static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application\\/json(;.*)?"); + final protected static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application\\/vnd.(.*)+json(;.*)?"); + + /** + * Check if the given MIME is a JSON MIME. + * JSON MIME examples: + * application/json + * application/json; charset=UTF8 + * APPLICATION/JSON + */ + protected static boolean isJsonMimeType(String mime) { + return mime != null && (JSON_MIME_PATTERN.matcher(mime).matches()); + } + + /** + * Check if the given MIME is a JSON Vendor MIME. + * JSON MIME examples: + * application/vnd.mycompany+json + * application/vnd.mycompany.resourceA.version1+json + */ + protected static boolean isJsonVendorMimeType(String mime) { + return mime != null && JSON_VENDOR_MIME_PATTERN.matcher(mime).matches(); + } + } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java index 7941bb05f4c8..a59c2175f35c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java @@ -657,6 +657,19 @@ public Map postProcessOperationsWithModels(Map o } } + + // check if the payload is json and set x-is-json accordingly + if (operation.consumes != null) { + for (Map consume : operation.consumes) { + if (consume.containsKey("mediaType")) { + if (isJsonMimeType(consume.get("mediaType"))) { + operation.vendorExtensions.put("x-is-json", true); + break; + } + } + } + } + if (operation.examples != null) { for (Map example : operation.examples) { for (Map.Entry entry : example.entrySet()) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellHttpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellHttpClientCodegen.java index 00fc4e981756..b7dbc6ec53a4 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellHttpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellHttpClientCodegen.java @@ -1239,10 +1239,6 @@ public String escapeIdentifier(String prefix, String name) { return name; } - static boolean isJsonMimeType(String mime) { - return mime != null && JSON_MIME_PATTERN.matcher(mime).matches(); - } - static boolean isWildcardMimeType(String mime) { return mime != null && mime.equals("*/*"); } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index 664a65ffb472..a5cb4bc28dac 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -926,29 +926,7 @@ public void forceSerializationLibrary(String serializationLibrary) { setSerializationLibrary(serializationLibrary); } - final private static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application\\/json(;.*)?"); - final private static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application\\/vnd.(.*)+json(;.*)?"); - /** - * Check if the given MIME is a JSON MIME. - * JSON MIME examples: - * application/json - * application/json; charset=UTF8 - * APPLICATION/JSON - */ - static boolean isJsonMimeType(String mime) { - return mime != null && (JSON_MIME_PATTERN.matcher(mime).matches()); - } - - /** - * Check if the given MIME is a JSON Vendor MIME. - * JSON MIME examples: - * application/vnd.mycompany+json - * application/vnd.mycompany.resourceA.version1+json - */ - static boolean isJsonVendorMimeType(String mime) { - return mime != null && JSON_VENDOR_MIME_PATTERN.matcher(mime).matches(); - } @Override public Map postProcessSupportingFileData(Map objs) { diff --git a/modules/openapi-generator/src/main/resources/csharp/api.mustache b/modules/openapi-generator/src/main/resources/csharp/api.mustache index f57926ade1dd..99592122f31a 100644 --- a/modules/openapi-generator/src/main/resources/csharp/api.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/api.mustache @@ -251,7 +251,7 @@ namespace {{packageName}}.{{apiPackage}} if ({{paramName}} != null) localVarHeaderParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToString({{paramName}})); // header parameter {{/headerParams}} {{#formParams}} - if ({{paramName}} != null) {{#isFile}}localVarFileParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToFile("{{baseName}}", {{paramName}}));{{/isFile}}{{^isFile}}localVarFormParams.Add("{{baseName}}", this.Configuration.ApiClient.{{#isPrimitiveType}}ParameterToString{{/isPrimitiveType}}{{^isPrimitiveType}}Serialize{{/isPrimitiveType}}({{paramName}})); // form parameter{{/isFile}} + if ({{paramName}} != null) {{#isFile}}localVarFileParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToFile("{{baseName}}", {{paramName}}));{{/isFile}}{{^isFile}}localVarFormParams.Add("{{baseName}}", {{#vendorExtensions.x-is-json}}{{paramName}}.ToJson(){{/vendorExtensions.x-is-json}}{{^vendorExtensions.x-is-json}}this.Configuration.ApiClient.{{#isPrimitiveType}}ParameterToString{{/isPrimitiveType}}{{^isPrimitiveType}}Serialize{{/isPrimitiveType}}({{paramName}}){{/vendorExtensions.x-is-json}}); // form parameter{{/isFile}} {{/formParams}} {{#bodyParam}} if ({{paramName}} != null && {{paramName}}.GetType() != typeof(byte[])) @@ -398,7 +398,7 @@ namespace {{packageName}}.{{apiPackage}} if ({{paramName}} != null) localVarHeaderParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToString({{paramName}})); // header parameter {{/headerParams}} {{#formParams}} - if ({{paramName}} != null) {{#isFile}}localVarFileParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToFile("{{baseName}}", {{paramName}}));{{/isFile}}{{^isFile}}localVarFormParams.Add("{{baseName}}", this.Configuration.ApiClient.{{#isPrimitiveType}}ParameterToString{{/isPrimitiveType}}{{^isPrimitiveType}}Serialize{{/isPrimitiveType}}({{paramName}})); // form parameter{{/isFile}} + if ({{paramName}} != null) {{#isFile}}localVarFileParams.Add("{{baseName}}", this.Configuration.ApiClient.ParameterToFile("{{baseName}}", {{paramName}}));{{/isFile}}{{^isFile}}localVarFormParams.Add("{{baseName}}", {{#vendorExtensions.x-is-json}}{{paramName}}.ToJson(){{/vendorExtensions.x-is-json}}{{^vendorExtensions.x-is-json}}this.Configuration.ApiClient.{{#isPrimitiveType}}ParameterToString{{/isPrimitiveType}}{{^isPrimitiveType}}Serialize{{/isPrimitiveType}}({{paramName}}){{/vendorExtensions.x-is-json}}); // form parameter{{/isFile}} {{/formParams}} {{#bodyParam}} if ({{paramName}} != null && {{paramName}}.GetType() != typeof(byte[])) From be55f4fab4438f43364eb222563f49b35939b57c Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 25 Aug 2020 17:24:09 +0800 Subject: [PATCH 2/2] add docstring --- .../main/java/org/openapitools/codegen/DefaultCodegen.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 b15c8c11459b..191262d26597 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 @@ -6516,6 +6516,9 @@ protected Schema getAdditionalProperties(Schema schema) { * application/json * application/json; charset=UTF8 * APPLICATION/JSON + * + * @param mime MIME string + * @return true if the input matches the JSON MIME */ protected static boolean isJsonMimeType(String mime) { return mime != null && (JSON_MIME_PATTERN.matcher(mime).matches()); @@ -6526,9 +6529,11 @@ protected static boolean isJsonMimeType(String mime) { * JSON MIME examples: * application/vnd.mycompany+json * application/vnd.mycompany.resourceA.version1+json + * + * @param mime MIME string + * @return true if the input matches the JSON vendor MIME */ protected static boolean isJsonVendorMimeType(String mime) { return mime != null && JSON_VENDOR_MIME_PATTERN.matcher(mime).matches(); } - }