From 6ac61c8258aca35591437f5b388eab3d1ef810cf Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Wed, 26 Jun 2024 03:54:24 -0400 Subject: [PATCH] fix!: operation name --- lib/mix/tasks/generate.ex | 2 +- lib/openapi/phases/compile.ex | 18 +++++++++++++++++- test/stripe/util_test.exs | 9 +++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/mix/tasks/generate.ex b/lib/mix/tasks/generate.ex index 0041eafd..35db6ad0 100644 --- a/lib/mix/tasks/generate.ex +++ b/lib/mix/tasks/generate.ex @@ -1,5 +1,5 @@ defmodule Mix.Tasks.Stripe.Generate do - @moduledoc "The hello mix task: `mix help hello`" + @moduledoc "The hello mix task: `mix stripe.generate`" use Mix.Task def run(_) do diff --git a/lib/openapi/phases/compile.ex b/lib/openapi/phases/compile.ex index 54b19953..b7797563 100644 --- a/lib/openapi/phases/compile.ex +++ b/lib/openapi/phases/compile.ex @@ -48,7 +48,11 @@ defmodule Stripe.OpenApi.Phases.Compile do end end) - function_name = String.to_atom(operation["method_name"]) + function_name = + operation_definition.id + |> to_func_name() + |> Macro.underscore() + |> String.to_atom() success_response_spec = return_spec(operation_definition.success_response) @@ -509,4 +513,16 @@ defmodule Stripe.OpenApi.Phases.Compile do defp lookup_operation(path, operations) do Map.get(operations, path) end + + # NOTE: This is a mapping of operation ids to function names. This is necessary + # to avoid breaking changes. In the future, we should remove this mapping and + # use the operation id directly as the function name. + @operation_identity_mapping %{} + + defp to_func_name(operation_id) do + case Map.get(@operation_identity_mapping, operation_id) do + nil -> raise "Unknown operation id: #{inspect(operation_id)}" + name -> name + end + end end diff --git a/test/stripe/util_test.exs b/test/stripe/util_test.exs index 80504f9d..b2ac3c05 100644 --- a/test/stripe/util_test.exs +++ b/test/stripe/util_test.exs @@ -37,8 +37,13 @@ defmodule Stripe.UtilTest do assert object_name_to_module("billing_portal.session") == Stripe.BillingPortal.Session assert object_name_to_module("checkout.session") == Stripe.Checkout.Session - assert object_name_to_module("identity.verification_report") == Stripe.Identity.VerificationReport - assert object_name_to_module("identity.verification_session") == Stripe.Identity.VerificationSession + + assert object_name_to_module("identity.verification_report") == + Stripe.Identity.VerificationReport + + assert object_name_to_module("identity.verification_session") == + Stripe.Identity.VerificationSession + assert object_name_to_module("issuing.authorization") == Stripe.Issuing.Authorization assert object_name_to_module("issuing.card") == Stripe.Issuing.Card assert object_name_to_module("issuing.cardholder") == Stripe.Issuing.Cardholder