diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java index 127853a88fe7..74ddd477387e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java @@ -315,6 +315,24 @@ public boolean isRestful() { return isRestfulIndex() || isRestfulShow() || isRestfulCreate() || isRestfulUpdate() || isRestfulDestroy(); } + /** + * Check if operation produces text/plain responses. + * NOTE: This does not mean it _only_ produces text/plain, just that it is one of the produces types. + * + * @return true if at least one produces is text/plain + */ + public boolean producesTextPlain() { + if (produces != null) { + for (Map produce : produces) { + if ("text/plain".equalsIgnoreCase(produce.get("mediaType").split(";")[0].trim()) + && "String".equals(returnType)) { + return true; + } + } + } + return false; + } + /** * Get the substring except baseName from path * 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 726a05d1a615..2cdd367aaad7 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 @@ -817,17 +817,10 @@ public int compare(CodegenParameter one, CodegenParameter another) { if (NATIVE.equals(getLibrary()) || APACHE.equals(getLibrary())) { OperationMap operations = objs.getOperations(); List operationList = operations.getOperation(); - Pattern methodPattern = Pattern.compile("^(.*):([^:]*)$"); for (CodegenOperation op : operationList) { // add extension to indicate content type is `text/plain` and the response type is `String` - if (op.produces != null) { - for (Map produce : op.produces) { - if ("text/plain".equalsIgnoreCase(produce.get("mediaType").split(";")[0].trim()) - && "String".equals(op.returnType)) { - op.vendorExtensions.put("x-java-text-plain-string", true); - continue; - } - } + if ("String".equals(op.returnType) && op.producesTextPlain()) { + op.vendorExtensions.put("x-java-text-plain-string", true); } } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java index 18c6e48a636a..351282d5cc5c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java @@ -677,6 +677,10 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List serde_json::from_str(&local_var_content).map_err(Error::from), + {{#vendorExtensions.x-supports-plain-text}} + ContentType::Text => return Ok(local_var_content), + {{/vendorExtensions.x-supports-plain-text}} + {{^vendorExtensions.x-supports-plain-text}} + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `{{returnType}}`"))), + {{/vendorExtensions.x-supports-plain-text}} + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `{{returnType}}`")))), + } {{/returnType}} {{/supportMultipleResponses}} {{#supportMultipleResponses}} diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest-trait/api_mod.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest-trait/api_mod.mustache index c693585870fb..41a4ba0f81f6 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest-trait/api_mod.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest-trait/api_mod.mustache @@ -128,6 +128,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + {{#apiInfo}} {{#apis}} pub mod {{{classFilename}}}; diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache index ffe0f199a980..d43f0e854c39 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache @@ -1,9 +1,9 @@ {{>partial_header}} use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; {{#operations}} {{#operation}} @@ -355,6 +355,18 @@ pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}(configuration: let resp = configuration.client.execute(req){{#supportAsync}}.await{{/supportAsync}}?; let status = resp.status(); + {{^supportMultipleResponses}} + {{^isResponseFile}} + {{#returnType}} + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); + {{/returnType}} + {{/isResponseFile}} + {{/supportMultipleResponses}} if !status.is_client_error() && !status.is_server_error() { {{^supportMultipleResponses}} @@ -367,7 +379,16 @@ pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}(configuration: {{/returnType}} {{#returnType}} let content = resp.text(){{#supportAsync}}.await{{/supportAsync}}?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + {{#vendorExtensions.x-supports-plain-text}} + ContentType::Text => return Ok(content), + {{/vendorExtensions.x-supports-plain-text}} + {{^vendorExtensions.x-supports-plain-text}} + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `{{returnType}}`"))), + {{/vendorExtensions.x-supports-plain-text}} + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `{{returnType}}`")))), + } {{/returnType}} {{/isResponseFile}} {{/supportMultipleResponses}} diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache index bc5f68f44074..4d5df28e9bb8 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache @@ -134,6 +134,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + {{#apiInfo}} {{#apis}} pub mod {{{classFilename}}}; diff --git a/modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml index 0e50c34403bd..97ead87c5e48 100644 --- a/modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml @@ -495,6 +495,10 @@ paths: application/json: schema: type: string + text/plain: + schema: + type: string + '400': description: Invalid username/password supplied /user/logout: diff --git a/samples/client/others/rust/reqwest-regression-16119/src/apis/default_api.rs b/samples/client/others/rust/reqwest-regression-16119/src/apis/default_api.rs index 382dc5a2ce0d..79730fd260f7 100644 --- a/samples/client/others/rust/reqwest-regression-16119/src/apis/default_api.rs +++ b/samples/client/others/rust/reqwest-regression-16119/src/apis/default_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`repro`] @@ -36,10 +36,20 @@ pub fn repro(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Parent`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Parent`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/others/rust/reqwest-regression-16119/src/apis/mod.rs b/samples/client/others/rust/reqwest-regression-16119/src/apis/mod.rs index 4b09ba50b401..fdcc89b36066 100644 --- a/samples/client/others/rust/reqwest-regression-16119/src/apis/mod.rs +++ b/samples/client/others/rust/reqwest-regression-16119/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod default_api; pub mod configuration; diff --git a/samples/client/others/rust/reqwest/api-with-ref-param/src/apis/default_api.rs b/samples/client/others/rust/reqwest/api-with-ref-param/src/apis/default_api.rs index 562421a76248..5f0e129882a3 100644 --- a/samples/client/others/rust/reqwest/api-with-ref-param/src/apis/default_api.rs +++ b/samples/client/others/rust/reqwest/api-with-ref-param/src/apis/default_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`demo_color_get`] diff --git a/samples/client/others/rust/reqwest/api-with-ref-param/src/apis/mod.rs b/samples/client/others/rust/reqwest/api-with-ref-param/src/apis/mod.rs index 4b09ba50b401..fdcc89b36066 100644 --- a/samples/client/others/rust/reqwest/api-with-ref-param/src/apis/mod.rs +++ b/samples/client/others/rust/reqwest/api-with-ref-param/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod default_api; pub mod configuration; diff --git a/samples/client/others/rust/reqwest/composed-oneof/src/apis/default_api.rs b/samples/client/others/rust/reqwest/composed-oneof/src/apis/default_api.rs index 593abfd48216..cc749951d6eb 100644 --- a/samples/client/others/rust/reqwest/composed-oneof/src/apis/default_api.rs +++ b/samples/client/others/rust/reqwest/composed-oneof/src/apis/default_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`create_state`] @@ -69,10 +69,20 @@ pub fn get_state(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetState200Response`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::GetState200Response`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/others/rust/reqwest/composed-oneof/src/apis/mod.rs b/samples/client/others/rust/reqwest/composed-oneof/src/apis/mod.rs index 4b09ba50b401..fdcc89b36066 100644 --- a/samples/client/others/rust/reqwest/composed-oneof/src/apis/mod.rs +++ b/samples/client/others/rust/reqwest/composed-oneof/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod default_api; pub mod configuration; diff --git a/samples/client/others/rust/reqwest/emptyObject/src/apis/default_api.rs b/samples/client/others/rust/reqwest/emptyObject/src/apis/default_api.rs index 6b5803fd2423..e89c27b8eb3c 100644 --- a/samples/client/others/rust/reqwest/emptyObject/src/apis/default_api.rs +++ b/samples/client/others/rust/reqwest/emptyObject/src/apis/default_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`endpoint_get`] @@ -36,10 +36,20 @@ pub fn endpoint_get(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::EmptyObject`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::EmptyObject`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/others/rust/reqwest/emptyObject/src/apis/mod.rs b/samples/client/others/rust/reqwest/emptyObject/src/apis/mod.rs index 4b09ba50b401..fdcc89b36066 100644 --- a/samples/client/others/rust/reqwest/emptyObject/src/apis/mod.rs +++ b/samples/client/others/rust/reqwest/emptyObject/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod default_api; pub mod configuration; diff --git a/samples/client/others/rust/reqwest/oneOf-array-map/src/apis/default_api.rs b/samples/client/others/rust/reqwest/oneOf-array-map/src/apis/default_api.rs index d7fbf92af0ad..b5fb8c4fd942 100644 --- a/samples/client/others/rust/reqwest/oneOf-array-map/src/apis/default_api.rs +++ b/samples/client/others/rust/reqwest/oneOf-array-map/src/apis/default_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`root_get`] @@ -43,10 +43,20 @@ pub fn root_get(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Fruit`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Fruit`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/others/rust/reqwest/oneOf-array-map/src/apis/mod.rs b/samples/client/others/rust/reqwest/oneOf-array-map/src/apis/mod.rs index 4b09ba50b401..fdcc89b36066 100644 --- a/samples/client/others/rust/reqwest/oneOf-array-map/src/apis/mod.rs +++ b/samples/client/others/rust/reqwest/oneOf-array-map/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod default_api; pub mod configuration; diff --git a/samples/client/others/rust/reqwest/oneOf-reuseRef/src/apis/default_api.rs b/samples/client/others/rust/reqwest/oneOf-reuseRef/src/apis/default_api.rs index 3dceb3f7d040..ab51a425448a 100644 --- a/samples/client/others/rust/reqwest/oneOf-reuseRef/src/apis/default_api.rs +++ b/samples/client/others/rust/reqwest/oneOf-reuseRef/src/apis/default_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`get_fruit`] @@ -36,10 +36,20 @@ pub fn get_fruit(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Fruit`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Fruit`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/others/rust/reqwest/oneOf-reuseRef/src/apis/mod.rs b/samples/client/others/rust/reqwest/oneOf-reuseRef/src/apis/mod.rs index 4b09ba50b401..fdcc89b36066 100644 --- a/samples/client/others/rust/reqwest/oneOf-reuseRef/src/apis/mod.rs +++ b/samples/client/others/rust/reqwest/oneOf-reuseRef/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod default_api; pub mod configuration; diff --git a/samples/client/others/rust/reqwest/oneOf/src/apis/bar_api.rs b/samples/client/others/rust/reqwest/oneOf/src/apis/bar_api.rs index 6af94c62e442..797ac77c279d 100644 --- a/samples/client/others/rust/reqwest/oneOf/src/apis/bar_api.rs +++ b/samples/client/others/rust/reqwest/oneOf/src/apis/bar_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`create_bar`] @@ -39,10 +39,20 @@ pub fn create_bar(configuration: &configuration::Configuration, bar_create: mode let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Bar`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Bar`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/others/rust/reqwest/oneOf/src/apis/foo_api.rs b/samples/client/others/rust/reqwest/oneOf/src/apis/foo_api.rs index 8fefb3f45904..d71a4e6a152d 100644 --- a/samples/client/others/rust/reqwest/oneOf/src/apis/foo_api.rs +++ b/samples/client/others/rust/reqwest/oneOf/src/apis/foo_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`create_foo`] @@ -46,10 +46,20 @@ pub fn create_foo(configuration: &configuration::Configuration, foo: Option serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooRefOrValue`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooRefOrValue`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -70,10 +80,20 @@ pub fn get_all_foos(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::FooRefOrValue>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::FooRefOrValue>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/others/rust/reqwest/oneOf/src/apis/mod.rs b/samples/client/others/rust/reqwest/oneOf/src/apis/mod.rs index 96f98fdcb6e1..36835a5e8fa6 100644 --- a/samples/client/others/rust/reqwest/oneOf/src/apis/mod.rs +++ b/samples/client/others/rust/reqwest/oneOf/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod bar_api; pub mod foo_api; diff --git a/samples/client/petstore/rust/hyper/petstore/docs/UserApi.md b/samples/client/petstore/rust/hyper/petstore/docs/UserApi.md index 22192c1fd888..6d592c638ef0 100644 --- a/samples/client/petstore/rust/hyper/petstore/docs/UserApi.md +++ b/samples/client/petstore/rust/hyper/petstore/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/hyper0x/petstore/docs/UserApi.md b/samples/client/petstore/rust/hyper0x/petstore/docs/UserApi.md index d536057ae15a..b4481390cfae 100644 --- a/samples/client/petstore/rust/hyper0x/petstore/docs/UserApi.md +++ b/samples/client/petstore/rust/hyper0x/petstore/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest-trait/petstore/docs/UserApi.md b/samples/client/petstore/rust/reqwest-trait/petstore/docs/UserApi.md index d536057ae15a..b4481390cfae 100644 --- a/samples/client/petstore/rust/reqwest-trait/petstore/docs/UserApi.md +++ b/samples/client/petstore/rust/reqwest-trait/petstore/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/fake_api.rs index 7368946bb46f..b4eb9420cdb1 100644 --- a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/fake_api.rs @@ -14,9 +14,10 @@ use async_trait::async_trait; use mockall::automock; use reqwest; use std::sync::Arc; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; use super::{Error, configuration}; +use crate::apis::ContentType; #[cfg_attr(feature = "mockall", automock)] #[async_trait] diff --git a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/mod.rs b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/mod.rs index ba670264380c..9336a17460d3 100644 --- a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod pet_api; pub mod store_api; diff --git a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/pet_api.rs b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/pet_api.rs index 2ac8dff7ec8f..d1dafb82af4d 100644 --- a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/pet_api.rs @@ -14,9 +14,10 @@ use async_trait::async_trait; use mockall::automock; use reqwest; use std::sync::Arc; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; use super::{Error, configuration}; +use crate::apis::ContentType; #[cfg_attr(feature = "mockall", automock)] #[async_trait] @@ -90,10 +91,20 @@ impl PetApi for PetApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -165,10 +176,20 @@ impl PetApi for PetApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::Pet>`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `Vec<models::Pet>`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -200,10 +221,20 @@ impl PetApi for PetApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::Pet>`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `Vec<models::Pet>`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -236,10 +267,20 @@ impl PetApi for PetApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -268,10 +309,20 @@ impl PetApi for PetApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -344,10 +395,20 @@ impl PetApi for PetApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ApiResponse`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::ApiResponse`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; diff --git a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/store_api.rs b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/store_api.rs index 16ca71786d04..7adfa7218540 100644 --- a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/store_api.rs +++ b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/store_api.rs @@ -14,9 +14,10 @@ use async_trait::async_trait; use mockall::automock; use reqwest; use std::sync::Arc; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; use super::{Error, configuration}; +use crate::apis::ContentType; #[cfg_attr(feature = "mockall", automock)] #[async_trait] @@ -108,10 +109,20 @@ impl StoreApi for StoreApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `std::collections::HashMap<String, i32>`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `std::collections::HashMap<String, i32>`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -136,10 +147,20 @@ impl StoreApi for StoreApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Order`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::Order`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -165,10 +186,20 @@ impl StoreApi for StoreApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Order`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::Order`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; diff --git a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/testing_api.rs b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/testing_api.rs index 9562cb8a90d3..d59233d2d981 100644 --- a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/testing_api.rs +++ b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/testing_api.rs @@ -14,9 +14,10 @@ use async_trait::async_trait; use mockall::automock; use reqwest; use std::sync::Arc; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; use super::{Error, configuration}; +use crate::apis::ContentType; #[cfg_attr(feature = "mockall", automock)] #[async_trait] @@ -63,10 +64,20 @@ impl TestingApi for TestingApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `std::path::PathBuf`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `std::path::PathBuf`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -90,10 +101,20 @@ impl TestingApi for TestingApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TypeTesting`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::TypeTesting`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; diff --git a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/user_api.rs b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/user_api.rs index 7e3f79c7d2b0..ba2c479df495 100644 --- a/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/user_api.rs +++ b/samples/client/petstore/rust/reqwest-trait/petstore/src/apis/user_api.rs @@ -14,9 +14,10 @@ use async_trait::async_trait; use mockall::automock; use reqwest; use std::sync::Arc; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; use super::{Error, configuration}; +use crate::apis::ContentType; #[cfg_attr(feature = "mockall", automock)] #[async_trait] @@ -231,10 +232,20 @@ impl UserApi for UserApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::User`"))), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::User`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; @@ -261,10 +272,20 @@ impl UserApi for UserApiClient { let local_var_resp = local_var_client.execute(local_var_req).await?; let local_var_status = local_var_resp.status(); + let local_var_content_type = local_var_resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let local_var_content_type = super::ContentType::from(local_var_content_type); let local_var_content = local_var_resp.text().await?; if !local_var_status.is_client_error() && !local_var_status.is_server_error() { - serde_json::from_str(&local_var_content).map_err(Error::from) + match local_var_content_type { + ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from), + ContentType::Text => return Ok(local_var_content), + ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `String`")))), + } } else { let local_var_entity: Option = serde_json::from_str(&local_var_content).ok(); let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity }; diff --git a/samples/client/petstore/rust/reqwest/name-mapping/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest/name-mapping/src/apis/fake_api.rs index 7e45b087616c..156ab2a1a597 100644 --- a/samples/client/petstore/rust/reqwest/name-mapping/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest/name-mapping/src/apis/fake_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`get_parameter_name_mapping`] diff --git a/samples/client/petstore/rust/reqwest/name-mapping/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/name-mapping/src/apis/mod.rs index 006006c1ad1d..df474de37822 100644 --- a/samples/client/petstore/rust/reqwest/name-mapping/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/name-mapping/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod configuration; diff --git a/samples/client/petstore/rust/reqwest/petstore-async-middleware/docs/UserApi.md b/samples/client/petstore/rust/reqwest/petstore-async-middleware/docs/UserApi.md index d536057ae15a..b4481390cfae 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-middleware/docs/UserApi.md +++ b/samples/client/petstore/rust/reqwest/petstore-async-middleware/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/fake_api.rs index be9503699260..6be823ab5c83 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/fake_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`test_nullable_required_param`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/mod.rs index 33b8ca9e647e..b27445925dc4 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/mod.rs @@ -99,6 +99,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod pet_api; pub mod store_api; diff --git a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/pet_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/pet_api.rs index c8055993606e..342302918d25 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/pet_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`add_pet`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/store_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/store_api.rs index 17760b176768..d14c940610bf 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/store_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/store_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`delete_order`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/testing_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/testing_api.rs index 3f365095bf63..24717a365382 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/testing_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/testing_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed successes of method [`tests_file_response_get`] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/user_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/user_api.rs index e4dfae546d4c..c377f41f4e08 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/user_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-middleware/src/apis/user_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`create_user`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/docs/UserApi.md b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/docs/UserApi.md index d536057ae15a..b4481390cfae 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/docs/UserApi.md +++ b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/fake_api.rs index be9503699260..6be823ab5c83 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/fake_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`test_nullable_required_param`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/mod.rs index 3f68c684126e..c55f52db30b2 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/mod.rs @@ -93,6 +93,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod pet_api; pub mod store_api; diff --git a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/pet_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/pet_api.rs index 185acd30b033..7ff1983fc3af 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/pet_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`add_pet`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/store_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/store_api.rs index 859e3088916d..70d5b105a402 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/store_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/store_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`delete_order`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/testing_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/testing_api.rs index 3f365095bf63..24717a365382 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/testing_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/testing_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed successes of method [`tests_file_response_get`] diff --git a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/user_api.rs b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/user_api.rs index ddfebdf33851..f44d949fefaf 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/user_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/apis/user_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`create_user`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async/docs/UserApi.md b/samples/client/petstore/rust/reqwest/petstore-async/docs/UserApi.md index d536057ae15a..b4481390cfae 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/docs/UserApi.md +++ b/samples/client/petstore/rust/reqwest/petstore-async/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/fake_api.rs index be9503699260..6be823ab5c83 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/fake_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`test_nullable_required_param`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs index d791dfb5a892..a075c8414431 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod pet_api; pub mod store_api; diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/pet_api.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/pet_api.rs index c8055993606e..342302918d25 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/pet_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`add_pet`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/store_api.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/store_api.rs index 17760b176768..d14c940610bf 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/store_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/store_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`delete_order`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/testing_api.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/testing_api.rs index 3f365095bf63..24717a365382 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/testing_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/testing_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed successes of method [`tests_file_response_get`] diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/user_api.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/user_api.rs index e4dfae546d4c..c377f41f4e08 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/apis/user_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/apis/user_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`create_user`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-avoid-box/docs/UserApi.md b/samples/client/petstore/rust/reqwest/petstore-avoid-box/docs/UserApi.md index d536057ae15a..b4481390cfae 100644 --- a/samples/client/petstore/rust/reqwest/petstore-avoid-box/docs/UserApi.md +++ b/samples/client/petstore/rust/reqwest/petstore-avoid-box/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/fake_api.rs index be9503699260..6be823ab5c83 100644 --- a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/fake_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`test_nullable_required_param`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/mod.rs index d791dfb5a892..a075c8414431 100644 --- a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod pet_api; pub mod store_api; diff --git a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/pet_api.rs b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/pet_api.rs index c8055993606e..342302918d25 100644 --- a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/pet_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`add_pet`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/store_api.rs b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/store_api.rs index 17760b176768..d14c940610bf 100644 --- a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/store_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/store_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`delete_order`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/testing_api.rs b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/testing_api.rs index 3f365095bf63..24717a365382 100644 --- a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/testing_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/testing_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed successes of method [`tests_file_response_get`] diff --git a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/user_api.rs b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/user_api.rs index e4dfae546d4c..c377f41f4e08 100644 --- a/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/user_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-avoid-box/src/apis/user_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for passing parameters to the method [`create_user`] #[derive(Clone, Debug)] diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/docs/UserApi.md b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/docs/UserApi.md index d536057ae15a..b4481390cfae 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/docs/UserApi.md +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/fake_api.rs index 84059e46e596..a364dafd0e72 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/fake_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`test_nullable_required_param`] diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/mod.rs index 9b4554aa07fe..c03ebdf001fc 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/mod.rs @@ -94,6 +94,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod pet_api; pub mod store_api; diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/pet_api.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/pet_api.rs index cb4a4d7c038c..bc728dd02451 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/pet_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`add_pet`] @@ -115,10 +115,20 @@ pub fn add_pet(configuration: &configuration::Configuration, pet: models::Pet) - let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -215,10 +225,20 @@ pub fn find_pets_by_status(configuration: &configuration::Configuration, status: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::Pet>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::Pet>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -262,10 +282,20 @@ pub fn find_pets_by_tags(configuration: &configuration::Configuration, tags: Vec let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::Pet>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::Pet>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -310,10 +340,20 @@ pub fn get_pet_by_id(configuration: &configuration::Configuration, pet_id: i64) let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -354,10 +394,20 @@ pub fn update_pet(configuration: &configuration::Configuration, pet: models::Pet let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -459,10 +509,20 @@ pub fn upload_file(configuration: &configuration::Configuration, pet_id: i64, ad let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ApiResponse`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ApiResponse`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/store_api.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/store_api.rs index c4d6b07478e1..78dcac90360d 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/store_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/store_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`delete_order`] @@ -110,10 +110,20 @@ pub fn get_inventory(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `std::collections::HashMap<String, i32>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `std::collections::HashMap<String, i32>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -137,10 +147,20 @@ pub fn get_order_by_id(configuration: &configuration::Configuration, order_id: i let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Order`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Order`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -165,10 +185,20 @@ pub fn place_order(configuration: &configuration::Configuration, order: models:: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Order`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Order`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/testing_api.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/testing_api.rs index 285fc2e06996..ed232269085a 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/testing_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/testing_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`tests_file_response_get`] @@ -66,10 +66,20 @@ pub fn tests_type_testing_get(configuration: &configuration::Configuration, ) -> let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TypeTesting`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::TypeTesting`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/user_api.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/user_api.rs index c1fc777613d1..da93971ba232 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/user_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/apis/user_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`create_user`] @@ -290,10 +290,20 @@ pub fn get_user_by_name(configuration: &configuration::Configuration, username: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::User`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::User`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -320,10 +330,20 @@ pub fn login_user(configuration: &configuration::Configuration, username: &str, let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Ok(content), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `String`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/docs/UserApi.md b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/docs/UserApi.md index 3673dbe718d5..0db7baad2900 100644 --- a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/docs/UserApi.md +++ b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/fake_api.rs index 84059e46e596..a364dafd0e72 100644 --- a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/fake_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`test_nullable_required_param`] diff --git a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/mod.rs index d791dfb5a892..a075c8414431 100644 --- a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod pet_api; pub mod store_api; diff --git a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/pet_api.rs b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/pet_api.rs index 9c15cc673f7e..4f3780b34bee 100644 --- a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/pet_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`add_pet`] @@ -102,10 +102,20 @@ pub fn add_pet(configuration: &configuration::Configuration, foo_pet: models::Fo let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooPet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooPet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -176,10 +186,20 @@ pub fn find_pets_by_status(configuration: &configuration::Configuration, status: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::FooPet>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::FooPet>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -210,10 +230,20 @@ pub fn find_pets_by_tags(configuration: &configuration::Configuration, tags: Vec let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::FooPet>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::FooPet>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -245,10 +275,20 @@ pub fn get_pet_by_id(configuration: &configuration::Configuration, pet_id: i64) let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooPet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooPet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -276,10 +316,20 @@ pub fn update_pet(configuration: &configuration::Configuration, foo_pet: models: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooPet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooPet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -355,10 +405,20 @@ pub fn upload_file(configuration: &configuration::Configuration, pet_id: i64, ad let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooApiResponse`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooApiResponse`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/store_api.rs b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/store_api.rs index bfcdf8e075eb..d6cdf8ae9508 100644 --- a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/store_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/store_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`delete_order`] @@ -97,10 +97,20 @@ pub fn get_inventory(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `std::collections::HashMap<String, i32>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `std::collections::HashMap<String, i32>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -124,10 +134,20 @@ pub fn get_order_by_id(configuration: &configuration::Configuration, order_id: i let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooOrder`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooOrder`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -152,10 +172,20 @@ pub fn place_order(configuration: &configuration::Configuration, foo_order: mode let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooOrder`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooOrder`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/testing_api.rs b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/testing_api.rs index fb0745d6a62c..9aeb411aed45 100644 --- a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/testing_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/testing_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`tests_file_response_get`] @@ -66,10 +66,20 @@ pub fn tests_type_testing_get(configuration: &configuration::Configuration, ) -> let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooTypeTesting`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooTypeTesting`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/user_api.rs b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/user_api.rs index 0d2f0f327ddd..b50ce1caaab5 100644 --- a/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/user_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore-model-name-prefix/src/apis/user_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`create_user`] @@ -238,10 +238,20 @@ pub fn get_user_by_name(configuration: &configuration::Configuration, username: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::FooUser`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::FooUser`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -268,10 +278,20 @@ pub fn login_user(configuration: &configuration::Configuration, username: &str, let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Ok(content), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `String`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore/docs/UserApi.md b/samples/client/petstore/rust/reqwest/petstore/docs/UserApi.md index d536057ae15a..b4481390cfae 100644 --- a/samples/client/petstore/rust/reqwest/petstore/docs/UserApi.md +++ b/samples/client/petstore/rust/reqwest/petstore/docs/UserApi.md @@ -191,7 +191,7 @@ No authorization required ### HTTP request headers - **Content-Type**: Not defined -- **Accept**: application/xml, application/json +- **Accept**: application/xml, application/json, text/plain [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) diff --git a/samples/client/petstore/rust/reqwest/petstore/src/apis/fake_api.rs b/samples/client/petstore/rust/reqwest/petstore/src/apis/fake_api.rs index 84059e46e596..a364dafd0e72 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/apis/fake_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/apis/fake_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`test_nullable_required_param`] diff --git a/samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs b/samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs index d791dfb5a892..a075c8414431 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/apis/mod.rs @@ -90,6 +90,27 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String unimplemented!("Only objects are supported with style=deepObject") } +/// Internal use only +/// A content type supported by this client. +#[allow(dead_code)] +enum ContentType { + Json, + Text, + Unsupported(String) +} + +impl From<&str> for ContentType { + fn from(content_type: &str) -> Self { + if content_type.starts_with("application") && content_type.contains("json") { + return Self::Json; + } else if content_type.starts_with("text/plain") { + return Self::Text; + } else { + return Self::Unsupported(content_type.to_string()); + } + } +} + pub mod fake_api; pub mod pet_api; pub mod store_api; diff --git a/samples/client/petstore/rust/reqwest/petstore/src/apis/pet_api.rs b/samples/client/petstore/rust/reqwest/petstore/src/apis/pet_api.rs index 35c663eb7148..f30857ad8724 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/apis/pet_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`add_pet`] @@ -102,10 +102,20 @@ pub fn add_pet(configuration: &configuration::Configuration, pet: models::Pet) - let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -176,10 +186,20 @@ pub fn find_pets_by_status(configuration: &configuration::Configuration, status: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::Pet>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::Pet>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -210,10 +230,20 @@ pub fn find_pets_by_tags(configuration: &configuration::Configuration, tags: Vec let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::Pet>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::Pet>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -245,10 +275,20 @@ pub fn get_pet_by_id(configuration: &configuration::Configuration, pet_id: i64) let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -276,10 +316,20 @@ pub fn update_pet(configuration: &configuration::Configuration, pet: models::Pet let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Pet`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Pet`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -355,10 +405,20 @@ pub fn upload_file(configuration: &configuration::Configuration, pet_id: i64, ad let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ApiResponse`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ApiResponse`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore/src/apis/store_api.rs b/samples/client/petstore/rust/reqwest/petstore/src/apis/store_api.rs index 9918f21ad4a4..ee5e6a5d2d46 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/apis/store_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/apis/store_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`delete_order`] @@ -97,10 +97,20 @@ pub fn get_inventory(configuration: &configuration::Configuration, ) -> Result serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `std::collections::HashMap<String, i32>`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `std::collections::HashMap<String, i32>`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -124,10 +134,20 @@ pub fn get_order_by_id(configuration: &configuration::Configuration, order_id: i let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Order`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Order`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -152,10 +172,20 @@ pub fn place_order(configuration: &configuration::Configuration, order: models:: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Order`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Order`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore/src/apis/testing_api.rs b/samples/client/petstore/rust/reqwest/petstore/src/apis/testing_api.rs index 285fc2e06996..ed232269085a 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/apis/testing_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/apis/testing_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`tests_file_response_get`] @@ -66,10 +66,20 @@ pub fn tests_type_testing_get(configuration: &configuration::Configuration, ) -> let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TypeTesting`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::TypeTesting`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); diff --git a/samples/client/petstore/rust/reqwest/petstore/src/apis/user_api.rs b/samples/client/petstore/rust/reqwest/petstore/src/apis/user_api.rs index 299e19063df2..db7d7313ece1 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/apis/user_api.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/apis/user_api.rs @@ -10,9 +10,9 @@ use reqwest; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, de::Error as _}; use crate::{apis::ResponseContent, models}; -use super::{Error, configuration}; +use super::{Error, configuration, ContentType}; /// struct for typed errors of method [`create_user`] @@ -238,10 +238,20 @@ pub fn get_user_by_name(configuration: &configuration::Configuration, username: let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::User`"))), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::User`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok(); @@ -268,10 +278,20 @@ pub fn login_user(configuration: &configuration::Configuration, username: &str, let resp = configuration.client.execute(req)?; let status = resp.status(); + let content_type = resp + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or("application/octet-stream"); + let content_type = super::ContentType::from(content_type); if !status.is_client_error() && !status.is_server_error() { let content = resp.text()?; - serde_json::from_str(&content).map_err(Error::from) + match content_type { + ContentType::Json => serde_json::from_str(&content).map_err(Error::from), + ContentType::Text => return Ok(content), + ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `String`")))), + } } else { let content = resp.text()?; let entity: Option = serde_json::from_str(&content).ok();