From f5adbec1ffce37e605f59d072a51a07d234e4c43 Mon Sep 17 00:00:00 2001 From: Sunrin SHIMURA <3han5chou7@gmail.com> Date: Mon, 8 Jan 2018 01:00:12 +0900 Subject: [PATCH] [Rust-Server] Use string literal based mime initialization (#7212) * Use string literal based mime initialization to avoid invalid token error * canonicalize mimetypes before checking --- .../codegen/languages/RustServerCodegen.java | 75 ++++--------------- .../resources/rust-server/mimetypes.mustache | 4 +- samples/server/petstore/rust-server/README.md | 2 +- .../petstore/rust-server/src/mimetypes.rs | 54 ++++++------- 4 files changed, 44 insertions(+), 91 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RustServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RustServerCodegen.java index 6a6725933da..e345def9add 100755 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RustServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RustServerCodegen.java @@ -440,6 +440,14 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig { return input.replace("*/", "*_/").replace("/*", "/_*"); } + boolean isMimetypeXml(String mimetype) { + return mimetype.toLowerCase().startsWith("application/xml"); + } + + boolean isMimetypePlainText(String mimetype) { + return mimetype.toLowerCase().startsWith("text/plain"); + } + @Override public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map definitions, Swagger swagger) { CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger); @@ -528,14 +536,13 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig { // if "consumes" is defined (per operation or using global definition) if (consumes != null && !consumes.isEmpty()) { List> c = new ArrayList>(); - for (String key : consumes) { + for (String mimeType : consumes) { Map mediaType = new HashMap(); - String mimeType = processMimeType(key); - if (mimeType.startsWith("Application/Xml")) { + if (isMimetypeXml(mimeType)) { additionalProperties.put("usesXml", true); consumesXml = true; - } else if (mimeType.startsWith("Text/Plain")) { + } else if (isMimetypePlainText(mimeType)) { consumesPlainText = true; } @@ -562,14 +569,13 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig { boolean producesPlainText = false; if (produces != null && !produces.isEmpty()) { List> c = new ArrayList>(); - for (String key : produces) { + for (String mimeType : produces) { Map mediaType = new HashMap(); - String mimeType = processMimeType(key); - if (mimeType.startsWith("Application/Xml")) { + if (isMimetypeXml(mimeType)) { additionalProperties.put("usesXml", true); producesXml = true; - } else if (mimeType.startsWith("Text/Plain")) { + } else if (isMimetypePlainText(mimeType)) { producesPlainText = true; } @@ -1023,57 +1029,4 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig { } return false; } - - private String processMimeType(String mimeType){ - // Transform mime type into a form that the hyper mime! macro can handle. - String result = ""; - - String[] split_attributes = mimeType.split(";"); - String media = split_attributes[0]; - String[] mediaTypes = media.split("/"); - - if (mediaTypes.length == 2) { - - if (mediaTypes[0].equals("*")){ - result += "Star"; - } else { - result += escapeText(escapeQuotationMark(initialCaps(mediaTypes[0]))); - } - - result += "/"; - - if (mediaTypes[1].equals("*")) { - result += "Star"; - } else { - result += escapeText(escapeQuotationMark(initialCaps(mediaTypes[1]))); - } - } else { - LOGGER.error("Failed to parse media type: " - + mimeType - + ", media types should have exactly one /"); - } - - if (split_attributes.length == 2) { - String attributes = ""; - String[] attrs = split_attributes[1].split(","); - - for (String attr : attrs) { - String[] keyValuePair =attr.split("="); - if (keyValuePair.length == 2) { - attributes += "(\"" - + escapeText(escapeQuotationMark(keyValuePair[0].trim())) - + "\")=(\"" - + escapeText(escapeQuotationMark(keyValuePair[1].trim())) - + "\")"; - } else { - LOGGER.error("Failed to parse parameter attributes: " - + split_attributes[1] - + ", attributes must be a comma separated list of 'key=value' pairs"); - } - } - result += "; " + attributes; - } - - return result; - } } diff --git a/modules/swagger-codegen/src/main/resources/rust-server/mimetypes.mustache b/modules/swagger-codegen/src/main/resources/rust-server/mimetypes.mustache index 6d71f39f164..744374ca995 100644 --- a/modules/swagger-codegen/src/main/resources/rust-server/mimetypes.mustache +++ b/modules/swagger-codegen/src/main/resources/rust-server/mimetypes.mustache @@ -6,7 +6,7 @@ pub mod responses { // The macro is called per-operation to beat the recursion limit {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#responses}}{{#produces}}{{#-first}}{{#dataType}} /// Create Mime objects for the response content types for {{operationId}} lazy_static! { - pub static ref {{#vendorExtensions}}{{uppercase_operation_id}}_{{x-uppercaseResponseId}}{{/vendorExtensions}}: Mime = mime!({{{mediaType}}}); + pub static ref {{#vendorExtensions}}{{uppercase_operation_id}}_{{x-uppercaseResponseId}}{{/vendorExtensions}}: Mime = "{{{mediaType}}}".parse().unwrap(); } {{/dataType}}{{/-first}}{{/produces}}{{/responses}}{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} } @@ -15,7 +15,7 @@ pub mod requests { use hyper::mime::*; {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#bodyParam}} /// Create Mime objects for the request content types for {{operationId}} lazy_static! { - pub static ref {{#vendorExtensions}}{{uppercase_operation_id}}{{/vendorExtensions}}: Mime = mime!({{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}Application/Json{{/consumes}}); + pub static ref {{#vendorExtensions}}{{uppercase_operation_id}}{{/vendorExtensions}}: Mime = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}Application/Json{{/consumes}}".parse().unwrap(); } {{/bodyParam}}{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} } diff --git a/samples/server/petstore/rust-server/README.md b/samples/server/petstore/rust-server/README.md index 0eb6e87be59..09fa6742f17 100644 --- a/samples/server/petstore/rust-server/README.md +++ b/samples/server/petstore/rust-server/README.md @@ -13,7 +13,7 @@ To see how to make this your own, look here: [README](https://github.com/swagger-api/swagger-codegen/blob/master/README.md) - API version: 1.0.0 -- Build date: 2017-12-14T14:31:31.219+09:00 +- Build date: 2017-12-20T15:51:59.221+09:00 This autogenerated project defines an API crate `petstore_api` which contains: * An `Api` trait defining the API in Rust. diff --git a/samples/server/petstore/rust-server/src/mimetypes.rs b/samples/server/petstore/rust-server/src/mimetypes.rs index 401c05bedd5..c35afc74ff3 100644 --- a/samples/server/petstore/rust-server/src/mimetypes.rs +++ b/samples/server/petstore/rust-server/src/mimetypes.rs @@ -6,51 +6,51 @@ pub mod responses { // The macro is called per-operation to beat the recursion limit /// Create Mime objects for the response content types for TestSpecialTags lazy_static! { - pub static ref TEST_SPECIAL_TAGS_SUCCESSFUL_OPERATION: Mime = mime!(Application/Json); + pub static ref TEST_SPECIAL_TAGS_SUCCESSFUL_OPERATION: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the response content types for TestClientModel lazy_static! { - pub static ref TEST_CLIENT_MODEL_SUCCESSFUL_OPERATION: Mime = mime!(Application/Json); + pub static ref TEST_CLIENT_MODEL_SUCCESSFUL_OPERATION: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the response content types for TestClassname lazy_static! { - pub static ref TEST_CLASSNAME_SUCCESSFUL_OPERATION: Mime = mime!(Application/Json); + pub static ref TEST_CLASSNAME_SUCCESSFUL_OPERATION: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the response content types for FindPetsByStatus lazy_static! { - pub static ref FIND_PETS_BY_STATUS_SUCCESSFUL_OPERATION: Mime = mime!(Application/Xml); + pub static ref FIND_PETS_BY_STATUS_SUCCESSFUL_OPERATION: Mime = "application/xml".parse().unwrap(); } /// Create Mime objects for the response content types for FindPetsByTags lazy_static! { - pub static ref FIND_PETS_BY_TAGS_SUCCESSFUL_OPERATION: Mime = mime!(Application/Xml); + pub static ref FIND_PETS_BY_TAGS_SUCCESSFUL_OPERATION: Mime = "application/xml".parse().unwrap(); } /// Create Mime objects for the response content types for GetPetById lazy_static! { - pub static ref GET_PET_BY_ID_SUCCESSFUL_OPERATION: Mime = mime!(Application/Xml); + pub static ref GET_PET_BY_ID_SUCCESSFUL_OPERATION: Mime = "application/xml".parse().unwrap(); } /// Create Mime objects for the response content types for UploadFile lazy_static! { - pub static ref UPLOAD_FILE_SUCCESSFUL_OPERATION: Mime = mime!(Application/Json); + pub static ref UPLOAD_FILE_SUCCESSFUL_OPERATION: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the response content types for GetInventory lazy_static! { - pub static ref GET_INVENTORY_SUCCESSFUL_OPERATION: Mime = mime!(Application/Json); + pub static ref GET_INVENTORY_SUCCESSFUL_OPERATION: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the response content types for GetOrderById lazy_static! { - pub static ref GET_ORDER_BY_ID_SUCCESSFUL_OPERATION: Mime = mime!(Application/Xml); + pub static ref GET_ORDER_BY_ID_SUCCESSFUL_OPERATION: Mime = "application/xml".parse().unwrap(); } /// Create Mime objects for the response content types for PlaceOrder lazy_static! { - pub static ref PLACE_ORDER_SUCCESSFUL_OPERATION: Mime = mime!(Application/Xml); + pub static ref PLACE_ORDER_SUCCESSFUL_OPERATION: Mime = "application/xml".parse().unwrap(); } /// Create Mime objects for the response content types for GetUserByName lazy_static! { - pub static ref GET_USER_BY_NAME_SUCCESSFUL_OPERATION: Mime = mime!(Application/Xml); + pub static ref GET_USER_BY_NAME_SUCCESSFUL_OPERATION: Mime = "application/xml".parse().unwrap(); } /// Create Mime objects for the response content types for LoginUser lazy_static! { - pub static ref LOGIN_USER_SUCCESSFUL_OPERATION: Mime = mime!(Application/Xml); + pub static ref LOGIN_USER_SUCCESSFUL_OPERATION: Mime = "application/xml".parse().unwrap(); } } @@ -59,63 +59,63 @@ pub mod requests { use hyper::mime::*; /// Create Mime objects for the request content types for TestSpecialTags lazy_static! { - pub static ref TEST_SPECIAL_TAGS: Mime = mime!(Application/Json); + pub static ref TEST_SPECIAL_TAGS: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the request content types for FakeOuterBooleanSerialize lazy_static! { - pub static ref FAKE_OUTER_BOOLEAN_SERIALIZE: Mime = mime!(Application/Json); + pub static ref FAKE_OUTER_BOOLEAN_SERIALIZE: Mime = "Application/Json".parse().unwrap(); } /// Create Mime objects for the request content types for FakeOuterCompositeSerialize lazy_static! { - pub static ref FAKE_OUTER_COMPOSITE_SERIALIZE: Mime = mime!(Application/Json); + pub static ref FAKE_OUTER_COMPOSITE_SERIALIZE: Mime = "Application/Json".parse().unwrap(); } /// Create Mime objects for the request content types for FakeOuterNumberSerialize lazy_static! { - pub static ref FAKE_OUTER_NUMBER_SERIALIZE: Mime = mime!(Application/Json); + pub static ref FAKE_OUTER_NUMBER_SERIALIZE: Mime = "Application/Json".parse().unwrap(); } /// Create Mime objects for the request content types for FakeOuterStringSerialize lazy_static! { - pub static ref FAKE_OUTER_STRING_SERIALIZE: Mime = mime!(Application/Json); + pub static ref FAKE_OUTER_STRING_SERIALIZE: Mime = "Application/Json".parse().unwrap(); } /// Create Mime objects for the request content types for TestClientModel lazy_static! { - pub static ref TEST_CLIENT_MODEL: Mime = mime!(Application/Json); + pub static ref TEST_CLIENT_MODEL: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the request content types for TestInlineAdditionalProperties lazy_static! { - pub static ref TEST_INLINE_ADDITIONAL_PROPERTIES: Mime = mime!(Application/Json); + pub static ref TEST_INLINE_ADDITIONAL_PROPERTIES: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the request content types for TestClassname lazy_static! { - pub static ref TEST_CLASSNAME: Mime = mime!(Application/Json); + pub static ref TEST_CLASSNAME: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the request content types for AddPet lazy_static! { - pub static ref ADD_PET: Mime = mime!(Application/Json); + pub static ref ADD_PET: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the request content types for UpdatePet lazy_static! { - pub static ref UPDATE_PET: Mime = mime!(Application/Json); + pub static ref UPDATE_PET: Mime = "application/json".parse().unwrap(); } /// Create Mime objects for the request content types for PlaceOrder lazy_static! { - pub static ref PLACE_ORDER: Mime = mime!(Application/Json); + pub static ref PLACE_ORDER: Mime = "Application/Json".parse().unwrap(); } /// Create Mime objects for the request content types for CreateUser lazy_static! { - pub static ref CREATE_USER: Mime = mime!(Application/Json); + pub static ref CREATE_USER: Mime = "Application/Json".parse().unwrap(); } /// Create Mime objects for the request content types for CreateUsersWithArrayInput lazy_static! { - pub static ref CREATE_USERS_WITH_ARRAY_INPUT: Mime = mime!(Application/Json); + pub static ref CREATE_USERS_WITH_ARRAY_INPUT: Mime = "Application/Json".parse().unwrap(); } /// Create Mime objects for the request content types for CreateUsersWithListInput lazy_static! { - pub static ref CREATE_USERS_WITH_LIST_INPUT: Mime = mime!(Application/Json); + pub static ref CREATE_USERS_WITH_LIST_INPUT: Mime = "Application/Json".parse().unwrap(); } /// Create Mime objects for the request content types for UpdateUser lazy_static! { - pub static ref UPDATE_USER: Mime = mime!(Application/Json); + pub static ref UPDATE_USER: Mime = "Application/Json".parse().unwrap(); } }