Skip to content

Commit

Permalink
Fix polymorphic returned types, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mzeitlin11 committed Dec 28, 2023
1 parent 4b4f493 commit 8157122
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 42 deletions.
8 changes: 4 additions & 4 deletions generated/stripe_core/src/customer/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,14 @@ impl<'a> RetrieveCustomer<'a> {
&self,
client: &stripe::Client,
customer: &stripe_shared::CustomerId,
) -> stripe::Response<RetrieveReturned> {
) -> stripe::Response<RetrieveCustomerReturned> {
client.get_query(&format!("/customers/{customer}"), self)
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum RetrieveReturned {
#[serde(rename = "customer")]
#[serde(untagged)]
pub enum RetrieveCustomerReturned {
Customer(stripe_shared::Customer),
DeletedCustomer(stripe_shared::DeletedCustomer),
}
#[derive(Copy, Clone, Debug, Default, serde::Serialize)]
Expand Down
8 changes: 4 additions & 4 deletions generated/stripe_payment/src/bank_account/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a> UpdateCustomerBankAccount<'a> {
client: &stripe::Client,
customer: &stripe_shared::CustomerId,
id: &str,
) -> stripe::Response<UpdateCustomerReturned> {
) -> stripe::Response<UpdateCustomerBankAccountReturned> {
client.send_form(
&format!("/customers/{customer}/sources/{id}"),
self,
Expand All @@ -165,7 +165,7 @@ impl<'a> UpdateCustomerBankAccount<'a> {
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum UpdateCustomerReturned {
pub enum UpdateCustomerBankAccountReturned {
#[serde(rename = "card")]
Card(stripe_shared::Card),
#[serde(rename = "bank_account")]
Expand All @@ -191,7 +191,7 @@ impl<'a> DeleteCustomerBankAccount<'a> {
client: &stripe::Client,
customer: &stripe_shared::CustomerId,
id: &str,
) -> stripe::Response<DeleteCustomerReturned> {
) -> stripe::Response<DeleteCustomerBankAccountReturned> {
client.send_form(
&format!("/customers/{customer}/sources/{id}"),
self,
Expand All @@ -201,7 +201,7 @@ impl<'a> DeleteCustomerBankAccount<'a> {
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum DeleteCustomerReturned {
pub enum DeleteCustomerBankAccountReturned {
PaymentSource(stripe_shared::PaymentSource),
DeletedPaymentSource(stripe_shared::DeletedPaymentSource),
}
Expand Down
8 changes: 4 additions & 4 deletions generated/stripe_payment/src/card/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a> UpdateCustomerCard<'a> {
client: &stripe::Client,
customer: &stripe_shared::CustomerId,
id: &str,
) -> stripe::Response<UpdateCustomerReturned> {
) -> stripe::Response<UpdateCustomerCardReturned> {
client.send_form(
&format!("/customers/{customer}/sources/{id}"),
self,
Expand All @@ -165,7 +165,7 @@ impl<'a> UpdateCustomerCard<'a> {
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum UpdateCustomerReturned {
pub enum UpdateCustomerCardReturned {
#[serde(rename = "card")]
Card(stripe_shared::Card),
#[serde(rename = "bank_account")]
Expand All @@ -191,7 +191,7 @@ impl<'a> DeleteCustomerCard<'a> {
client: &stripe::Client,
customer: &stripe_shared::CustomerId,
id: &str,
) -> stripe::Response<DeleteCustomerReturned> {
) -> stripe::Response<DeleteCustomerCardReturned> {
client.send_form(
&format!("/customers/{customer}/sources/{id}"),
self,
Expand All @@ -201,7 +201,7 @@ impl<'a> DeleteCustomerCard<'a> {
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum DeleteCustomerReturned {
pub enum DeleteCustomerCardReturned {
PaymentSource(stripe_shared::PaymentSource),
DeletedPaymentSource(stripe_shared::DeletedPaymentSource),
}
Expand Down
4 changes: 2 additions & 2 deletions generated/stripe_payment/src/source/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<'a> DetachSource<'a> {
client: &stripe::Client,
customer: &stripe_shared::CustomerId,
id: &str,
) -> stripe::Response<DetachReturned> {
) -> stripe::Response<DetachSourceReturned> {
client.send_form(
&format!("/customers/{customer}/sources/{id}"),
self,
Expand All @@ -26,7 +26,7 @@ impl<'a> DetachSource<'a> {
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum DetachReturned {
pub enum DetachSourceReturned {
PaymentSource(stripe_shared::PaymentSource),
DeletedPaymentSource(stripe_shared::DeletedPaymentSource),
}
Expand Down
16 changes: 8 additions & 8 deletions generated/stripe_terminal/src/terminal_configuration/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ impl<'a> RetrieveTerminalConfiguration<'a> {
&self,
client: &stripe::Client,
configuration: &stripe_terminal::TerminalConfigurationId,
) -> stripe::Response<RetrieveReturned> {
) -> stripe::Response<RetrieveTerminalConfigurationReturned> {
client.get_query(&format!("/terminal/configurations/{configuration}"), self)
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum RetrieveReturned {
#[serde(rename = "terminal.configuration")]
#[serde(untagged)]
pub enum RetrieveTerminalConfigurationReturned {
TerminalConfiguration(stripe_terminal::TerminalConfiguration),
DeletedTerminalConfiguration(stripe_terminal::DeletedTerminalConfiguration),
}
#[derive(Copy, Clone, Debug, Default, serde::Serialize)]
Expand Down Expand Up @@ -176,7 +176,7 @@ impl<'a> UpdateTerminalConfiguration<'a> {
&self,
client: &stripe::Client,
configuration: &stripe_terminal::TerminalConfigurationId,
) -> stripe::Response<UpdateReturned> {
) -> stripe::Response<UpdateTerminalConfigurationReturned> {
client.send_form(
&format!("/terminal/configurations/{configuration}"),
self,
Expand All @@ -185,9 +185,9 @@ impl<'a> UpdateTerminalConfiguration<'a> {
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum UpdateReturned {
#[serde(rename = "terminal.configuration")]
#[serde(untagged)]
pub enum UpdateTerminalConfigurationReturned {
TerminalConfiguration(stripe_terminal::TerminalConfiguration),
DeletedTerminalConfiguration(stripe_terminal::DeletedTerminalConfiguration),
}
#[derive(Copy, Clone, Debug, Default, serde::Serialize)]
Expand Down
16 changes: 8 additions & 8 deletions generated/stripe_terminal/src/terminal_location/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ impl<'a> RetrieveTerminalLocation<'a> {
&self,
client: &stripe::Client,
location: &stripe_terminal::TerminalLocationId,
) -> stripe::Response<RetrieveReturned> {
) -> stripe::Response<RetrieveTerminalLocationReturned> {
client.get_query(&format!("/terminal/locations/{location}"), self)
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum RetrieveReturned {
#[serde(rename = "terminal.location")]
#[serde(untagged)]
pub enum RetrieveTerminalLocationReturned {
TerminalLocation(stripe_terminal::TerminalLocation),
DeletedTerminalLocation(stripe_terminal::DeletedTerminalLocation),
}
#[derive(Copy, Clone, Debug, serde::Serialize)]
Expand Down Expand Up @@ -145,14 +145,14 @@ impl<'a> UpdateTerminalLocation<'a> {
&self,
client: &stripe::Client,
location: &stripe_terminal::TerminalLocationId,
) -> stripe::Response<UpdateReturned> {
) -> stripe::Response<UpdateTerminalLocationReturned> {
client.send_form(&format!("/terminal/locations/{location}"), self, http_types::Method::Post)
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum UpdateReturned {
#[serde(rename = "terminal.location")]
#[serde(untagged)]
pub enum UpdateTerminalLocationReturned {
TerminalLocation(stripe_terminal::TerminalLocation),
DeletedTerminalLocation(stripe_terminal::DeletedTerminalLocation),
}
#[derive(Copy, Clone, Debug, Default, serde::Serialize)]
Expand Down
16 changes: 8 additions & 8 deletions generated/stripe_terminal/src/terminal_reader/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ impl<'a> UpdateTerminalReader<'a> {
&self,
client: &stripe::Client,
reader: &stripe_terminal::TerminalReaderId,
) -> stripe::Response<UpdateReturned> {
) -> stripe::Response<UpdateTerminalReaderReturned> {
client.send_form(&format!("/terminal/readers/{reader}"), self, http_types::Method::Post)
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum UpdateReturned {
#[serde(rename = "terminal.reader")]
#[serde(untagged)]
pub enum UpdateTerminalReaderReturned {
TerminalReader(stripe_terminal::TerminalReader),
DeletedTerminalReader(stripe_terminal::DeletedTerminalReader),
}
#[derive(Copy, Clone, Debug, Default, serde::Serialize)]
Expand All @@ -52,14 +52,14 @@ impl<'a> RetrieveTerminalReader<'a> {
&self,
client: &stripe::Client,
reader: &stripe_terminal::TerminalReaderId,
) -> stripe::Response<RetrieveReturned> {
) -> stripe::Response<RetrieveTerminalReaderReturned> {
client.get_query(&format!("/terminal/readers/{reader}"), self)
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "object")]
pub enum RetrieveReturned {
#[serde(rename = "terminal.reader")]
#[serde(untagged)]
pub enum RetrieveTerminalReaderReturned {
TerminalReader(stripe_terminal::TerminalReader),
DeletedTerminalReader(stripe_terminal::DeletedTerminalReader),
}
#[derive(Copy, Clone, Debug, serde::Serialize)]
Expand Down
4 changes: 2 additions & 2 deletions openapi/src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ fn build_request(
method_name: &str,
path_id_map: &HashMap<String, ComponentPath>,
) -> anyhow::Result<RequestSpec> {
let return_ident = RustIdent::joined(method_name, "returned");
let params_ident = RustIdent::joined(&method_name, parent_ident);
let return_ident = RustIdent::joined(&params_ident, "returned");
let return_type = Inference::new(&return_ident, ObjectKind::RequestReturned)
.required(true)
.infer_schema_or_ref_type(req.returned);

let params_ident = RustIdent::joined(method_name, parent_ident);
let param_inference =
Inference::new(&params_ident, ObjectKind::RequestParam).can_borrow(true).required(true);

Expand Down
6 changes: 5 additions & 1 deletion openapi/src/rust_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ pub fn as_enum_of_objects<'a>(
let path = variant.rust_type.as_ref().and_then(|t| t.as_component_path())?;
let obj = components.get(path);
let name = obj.data.object_name.as_deref()?;
object_map.insert(name, ObjectRef { path: path.clone(), feature_gate: None });

// If we have duplicate object names, we cannot distinguish by the object tag, so give up here
if object_map.insert(name, ObjectRef { path: path.clone(), feature_gate: None }).is_some() {
return None;
}
}
Some(object_map)
}
Expand Down
20 changes: 19 additions & 1 deletion tests/tests/it/blocking/customer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use stripe_core::customer::{CreateCustomer, DeleteCustomer};
use stripe_core::customer::{
CreateCustomer, DeleteCustomer, RetrieveCustomer, RetrieveCustomerReturned,
};

use crate::mock;

Expand Down Expand Up @@ -26,3 +28,19 @@ fn customer_create_and_delete_with_account() {
customer_create_and_delete(&client);
});
}

#[test]
fn retrieve_customer() {
mock::with_client(|client| {
let id = "cus_123".parse().unwrap();
let ret = RetrieveCustomer::new().send(client, &id).unwrap();
match ret {
RetrieveCustomerReturned::Customer(cust) => {
assert_eq!(cust.id, id);
assert_eq!(cust.invoice_prefix, Some("D331735".into()));
assert_eq!(cust.created, 1234567890);
}
RetrieveCustomerReturned::DeletedCustomer(_) => panic!("expected non-deleted response"),
}
})
}

0 comments on commit 8157122

Please sign in to comment.