Skip to content

Commit

Permalink
Merge pull request #490 from jwiesler/fix-self-rename
Browse files Browse the repository at this point in the history
Fix issue with missing rename of Self_
  • Loading branch information
arlyon authored Feb 15, 2024
2 parents 5a43415 + 9c58038 commit a91ac97
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -4721,6 +4721,7 @@ impl std::default::Default for CheckoutUsBankAccountPaymentMethodOptionsVerifica
#[serde(rename_all = "snake_case")]
pub enum CreateCheckoutSessionAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

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

Expand Down Expand Up @@ -7857,6 +7859,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -2967,6 +2969,7 @@ impl std::default::Default for CreatePaymentLinkShippingAddressCollectionAllowed
#[serde(rename_all = "snake_case")]
pub enum CreatePaymentLinkSubscriptionDataInvoiceSettingsIssuerType {
Account,
#[serde(rename = "self")]
Self_,
}

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

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

Expand Down Expand Up @@ -5321,6 +5326,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
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,7 @@ impl std::default::Default for CancellationDetailsReason {
#[serde(rename_all = "snake_case")]
pub enum CreateSubscriptionAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

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

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

Expand Down Expand Up @@ -3253,6 +3256,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
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ pub struct UpdateSubscriptionSchedulePhasesItemsPriceDataRecurring {
#[serde(rename_all = "snake_case")]
pub enum CreateSubscriptionSchedulePhasesAutomaticTaxLiabilityType {
Account,
#[serde(rename = "self")]
Self_,
}

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

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

Expand Down Expand Up @@ -1789,6 +1792,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 a91ac97

Please sign in to comment.