Skip to content

Commit

Permalink
fix: fixes an issue with missing renames of Self_
Browse files Browse the repository at this point in the history
  • Loading branch information
jwiesler committed Jan 29, 2024
1 parent af4b1e8 commit 9c58038
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 6 deletions.
20 changes: 18 additions & 2 deletions openapi/src/codegen.rs
Expand Up @@ -657,7 +657,7 @@ pub fn gen_unions(out: &mut String, unions: &BTreeMap<String, InferredUnion>, me
.unwrap_or_else(|| schema.schema_data.title.clone().unwrap());
let variant_name = meta.schema_to_rust_type(&object_name);
let type_name = meta.schema_to_rust_type(variant_schema);
if variant_name.to_snake_case() != object_name {
if variant_to_serde_snake_case(&variant_name) != object_name {
write_serde_rename(out, &object_name);
}
out.push_str(" ");
Expand Down Expand Up @@ -694,6 +694,22 @@ pub fn gen_unions(out: &mut String, unions: &BTreeMap<String, InferredUnion>, me
}
}

/// This code is taken from serde RenameRule::apply_to_variant
/// It differs in some cases from heck, so we need to make sure we
/// do exactly the same when figuring out whether we need a serde(rename)
/// e.g. heck_snake(Self_) = self
/// serde_snake(Self_) = self_
pub fn variant_to_serde_snake_case(variant: &str) -> String {
let mut snake = String::new();
for (i, ch) in variant.char_indices() {
if i > 0 && ch.is_uppercase() {
snake.push('_');
}
snake.push(ch.to_ascii_lowercase());
}
snake
}

#[tracing::instrument(skip_all)]
pub fn gen_variant_name(wire_name: &str, meta: &Metadata) -> String {
match wire_name {
Expand Down Expand Up @@ -732,7 +748,7 @@ pub fn gen_enums(out: &mut String, enums: &BTreeMap<String, InferredEnum>, meta:
if variant_name.trim().is_empty() {
panic!("unhandled enum variant: {:?}", wire_name)
}
if &variant_name.to_snake_case() != wire_name {
if &variant_to_serde_snake_case(&variant_name) != wire_name {
write_serde_rename(out, wire_name);
}
out.push_str(" ");
Expand Down
3 changes: 3 additions & 0 deletions src/resources/generated/checkout_session.rs
Expand Up @@ -4699,6 +4699,7 @@ impl std::default::Default for CheckoutUsBankAccountPaymentMethodOptionsVerifica
#[serde(rename_all = "snake_case")]
pub enum CreateCheckoutSessionAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -5013,6 +5014,7 @@ impl std::default::Default for CreateCheckoutSessionCustomerUpdateShipping {
#[serde(rename_all = "snake_case")]
pub enum CreateCheckoutSessionInvoiceCreationInvoiceDataIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -7833,6 +7835,7 @@ impl std::default::Default for CreateCheckoutSessionShippingOptionsShippingRateD
#[serde(rename_all = "snake_case")]
pub enum CreateCheckoutSessionSubscriptionDataInvoiceSettingsIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down
1 change: 1 addition & 0 deletions src/resources/generated/connect_account_reference.rs
Expand Up @@ -24,6 +24,7 @@ pub struct ConnectAccountReference {
#[serde(rename_all = "snake_case")]
pub enum ConnectAccountReferenceType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down
2 changes: 2 additions & 0 deletions src/resources/generated/invoice.rs
Expand Up @@ -1555,6 +1555,7 @@ impl std::default::Default for CollectionMethod {
#[serde(rename_all = "snake_case")]
pub enum CreateInvoiceAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -1621,6 +1622,7 @@ impl std::default::Default for CreateInvoiceFromInvoiceAction {
#[serde(rename_all = "snake_case")]
pub enum CreateInvoiceIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down
2 changes: 0 additions & 2 deletions src/resources/generated/issuing_card.rs
Expand Up @@ -608,7 +608,6 @@ pub enum IssuingCardSpendingLimitCategories {
HardwareStores,
HealthAndBeautySpas,
HearingAidsSalesAndSupplies,
#[serde(rename = "heating_plumbing_a_c")]
HeatingPlumbingAC,
HobbyToyAndGameShops,
HomeSupplyWarehouseStores,
Expand Down Expand Up @@ -737,7 +736,6 @@ pub enum IssuingCardSpendingLimitCategories {
TruckUtilityTrailerRentals,
TypesettingPlateMakingAndRelatedServices,
TypewriterStores,
#[serde(rename = "u_s_federal_government_agencies_or_departments")]
USFederalGovernmentAgenciesOrDepartments,
UniformsCommercialClothing,
UsedMerchandiseAndSecondhandStores,
Expand Down
2 changes: 0 additions & 2 deletions src/resources/generated/issuing_cardholder.rs
Expand Up @@ -510,7 +510,6 @@ pub enum IssuingCardholderSpendingLimitCategories {
HardwareStores,
HealthAndBeautySpas,
HearingAidsSalesAndSupplies,
#[serde(rename = "heating_plumbing_a_c")]
HeatingPlumbingAC,
HobbyToyAndGameShops,
HomeSupplyWarehouseStores,
Expand Down Expand Up @@ -639,7 +638,6 @@ pub enum IssuingCardholderSpendingLimitCategories {
TruckUtilityTrailerRentals,
TypesettingPlateMakingAndRelatedServices,
TypewriterStores,
#[serde(rename = "u_s_federal_government_agencies_or_departments")]
USFederalGovernmentAgenciesOrDepartments,
UniformsCommercialClothing,
UsedMerchandiseAndSecondhandStores,
Expand Down
6 changes: 6 additions & 0 deletions src/resources/generated/payment_link.rs
Expand Up @@ -1788,6 +1788,7 @@ impl std::default::Default for CreatePaymentLinkAfterCompletionType {
#[serde(rename_all = "snake_case")]
pub enum CreatePaymentLinkAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -1996,6 +1997,7 @@ impl std::default::Default for CreatePaymentLinkCustomFieldsType {
#[serde(rename_all = "snake_case")]
pub enum CreatePaymentLinkInvoiceCreationInvoiceDataIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -2965,6 +2967,7 @@ impl std::default::Default for CreatePaymentLinkShippingAddressCollectionAllowed
#[serde(rename_all = "snake_case")]
pub enum CreatePaymentLinkSubscriptionDataInvoiceSettingsIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -4314,6 +4317,7 @@ impl std::default::Default for UpdatePaymentLinkAfterCompletionType {
#[serde(rename_all = "snake_case")]
pub enum UpdatePaymentLinkAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -4416,6 +4420,7 @@ impl std::default::Default for UpdatePaymentLinkCustomFieldsType {
#[serde(rename_all = "snake_case")]
pub enum UpdatePaymentLinkInvoiceCreationInvoiceDataIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -5315,6 +5320,7 @@ impl std::default::Default for UpdatePaymentLinkShippingAddressCollectionAllowed
#[serde(rename_all = "snake_case")]
pub enum UpdatePaymentLinkSubscriptionDataInvoiceSettingsIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down
4 changes: 4 additions & 0 deletions src/resources/generated/subscription.rs
Expand Up @@ -1946,6 +1946,7 @@ impl std::default::Default for CancellationDetailsReason {
#[serde(rename_all = "snake_case")]
pub enum CreateSubscriptionAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -1980,6 +1981,7 @@ impl std::default::Default for CreateSubscriptionAutomaticTaxLiabilityType {
#[serde(rename_all = "snake_case")]
pub enum CreateSubscriptionInvoiceSettingsIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -3161,6 +3163,7 @@ impl std::default::Default for SubscriptionsResourcePaymentSettingsSaveDefaultPa
#[serde(rename_all = "snake_case")]
pub enum UpdateSubscriptionAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -3241,6 +3244,7 @@ impl std::default::Default for UpdateSubscriptionCancellationDetailsFeedback {
#[serde(rename_all = "snake_case")]
pub enum UpdateSubscriptionInvoiceSettingsIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down
4 changes: 4 additions & 0 deletions src/resources/generated/subscription_schedule.rs
Expand Up @@ -1274,6 +1274,7 @@ pub struct UpdateSubscriptionSchedulePhasesItemsPriceDataRecurring {
#[serde(rename_all = "snake_case")]
pub enum CreateSubscriptionSchedulePhasesAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -1558,6 +1559,7 @@ impl std::default::Default for SubscriptionScheduleDefaultSettingsCollectionMeth
#[serde(rename_all = "snake_case")]
pub enum SubscriptionScheduleDefaultSettingsParamsAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -1668,6 +1670,7 @@ impl std::default::Default for SubscriptionScheduleEndBehavior {
#[serde(rename_all = "snake_case")]
pub enum SubscriptionScheduleInvoiceSettingsIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down Expand Up @@ -1776,6 +1779,7 @@ impl std::default::Default for SubscriptionScheduleStatus {
#[serde(rename_all = "snake_case")]
pub enum UpdateSubscriptionSchedulePhasesAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

Expand Down

0 comments on commit 9c58038

Please sign in to comment.