forked from loafle/openapi-generator-original
[Rust-Server] Use string literal based mime initialization (#7212)
* Use string literal based mime initialization to avoid invalid token error * canonicalize mimetypes before checking
This commit is contained in:
parent
e51223eef1
commit
f5adbec1ff
@ -440,6 +440,14 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
return input.replace("*/", "*_/").replace("/*", "/_*");
|
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
|
@Override
|
||||||
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
|
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
|
||||||
CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, 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" is defined (per operation or using global definition)
|
||||||
if (consumes != null && !consumes.isEmpty()) {
|
if (consumes != null && !consumes.isEmpty()) {
|
||||||
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
|
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
|
||||||
for (String key : consumes) {
|
for (String mimeType : consumes) {
|
||||||
Map<String, String> mediaType = new HashMap<String, String>();
|
Map<String, String> mediaType = new HashMap<String, String>();
|
||||||
String mimeType = processMimeType(key);
|
|
||||||
|
|
||||||
if (mimeType.startsWith("Application/Xml")) {
|
if (isMimetypeXml(mimeType)) {
|
||||||
additionalProperties.put("usesXml", true);
|
additionalProperties.put("usesXml", true);
|
||||||
consumesXml = true;
|
consumesXml = true;
|
||||||
} else if (mimeType.startsWith("Text/Plain")) {
|
} else if (isMimetypePlainText(mimeType)) {
|
||||||
consumesPlainText = true;
|
consumesPlainText = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -562,14 +569,13 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
boolean producesPlainText = false;
|
boolean producesPlainText = false;
|
||||||
if (produces != null && !produces.isEmpty()) {
|
if (produces != null && !produces.isEmpty()) {
|
||||||
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
|
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
|
||||||
for (String key : produces) {
|
for (String mimeType : produces) {
|
||||||
Map<String, String> mediaType = new HashMap<String, String>();
|
Map<String, String> mediaType = new HashMap<String, String>();
|
||||||
String mimeType = processMimeType(key);
|
|
||||||
|
|
||||||
if (mimeType.startsWith("Application/Xml")) {
|
if (isMimetypeXml(mimeType)) {
|
||||||
additionalProperties.put("usesXml", true);
|
additionalProperties.put("usesXml", true);
|
||||||
producesXml = true;
|
producesXml = true;
|
||||||
} else if (mimeType.startsWith("Text/Plain")) {
|
} else if (isMimetypePlainText(mimeType)) {
|
||||||
producesPlainText = true;
|
producesPlainText = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1023,57 +1029,4 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
}
|
}
|
||||||
return false;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ pub mod responses {
|
|||||||
// The macro is called per-operation to beat the recursion limit
|
// 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}}
|
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#responses}}{{#produces}}{{#-first}}{{#dataType}} /// Create Mime objects for the response content types for {{operationId}}
|
||||||
lazy_static! {
|
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}}
|
{{/dataType}}{{/-first}}{{/produces}}{{/responses}}{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
|
||||||
}
|
}
|
||||||
@ -15,7 +15,7 @@ pub mod requests {
|
|||||||
use hyper::mime::*;
|
use hyper::mime::*;
|
||||||
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#bodyParam}} /// Create Mime objects for the request content types for {{operationId}}
|
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#bodyParam}} /// Create Mime objects for the request content types for {{operationId}}
|
||||||
lazy_static! {
|
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}}
|
{{/bodyParam}}{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
[README](https://github.com/swagger-api/swagger-codegen/blob/master/README.md)
|
||||||
|
|
||||||
- API version: 1.0.0
|
- 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:
|
This autogenerated project defines an API crate `petstore_api` which contains:
|
||||||
* An `Api` trait defining the API in Rust.
|
* An `Api` trait defining the API in Rust.
|
||||||
|
@ -6,51 +6,51 @@ pub mod responses {
|
|||||||
// The macro is called per-operation to beat the recursion limit
|
// The macro is called per-operation to beat the recursion limit
|
||||||
/// Create Mime objects for the response content types for TestSpecialTags
|
/// Create Mime objects for the response content types for TestSpecialTags
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for TestClientModel
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for TestClassname
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for FindPetsByStatus
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for FindPetsByTags
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for GetPetById
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for UploadFile
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for GetInventory
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for GetOrderById
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for PlaceOrder
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for GetUserByName
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the response content types for LoginUser
|
||||||
lazy_static! {
|
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::*;
|
use hyper::mime::*;
|
||||||
/// Create Mime objects for the request content types for TestSpecialTags
|
/// Create Mime objects for the request content types for TestSpecialTags
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for FakeOuterBooleanSerialize
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for FakeOuterCompositeSerialize
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for FakeOuterNumberSerialize
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for FakeOuterStringSerialize
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for TestClientModel
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for TestInlineAdditionalProperties
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for TestClassname
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for AddPet
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for UpdatePet
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for PlaceOrder
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for CreateUser
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for CreateUsersWithArrayInput
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for CreateUsersWithListInput
|
||||||
lazy_static! {
|
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
|
/// Create Mime objects for the request content types for UpdateUser
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref UPDATE_USER: Mime = mime!(Application/Json);
|
pub static ref UPDATE_USER: Mime = "Application/Json".parse().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user