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

Add x-is-json to csharp generators #7293

Merged
merged 2 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6507,4 +6507,33 @@ 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
*
* @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());
}

/**
* Check if the given MIME is a JSON Vendor 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,19 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}
}


// check if the payload is json and set x-is-json accordingly
if (operation.consumes != null) {
for (Map<String, String> 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<String, String> example : operation.examples) {
for (Map.Entry<String, String> entry : example.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("*/*");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]))
Expand Down Expand Up @@ -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[]))
Expand Down