Repository: mirumee/ariadne-codegen
Pull request #307 (2024-07-31 15:44:14)

respect convert_to_snake_case settings
fixes https://github.com/mirumee/ariadne-codegen/issues/275 by passing the `convert_to_snake_case` to various generators created via the `get_package_generator` function. 

adds a test that attempts to test that all the generator class fields that are determined by data passed in from `ClientSettings` are respected.

tested both via unit tests and by regenerating code in which i experienced this issue myself

Rosswell: @rafalp would you be able to take a look at this whenever you get a chance? very minor change
rafalp: Hi @Rosswell, @DamianCzajkowski is the man you want. I am no longer with Mirumee and I have no powers here anymore :D
DamianCzajkowski: Hi @Rosswell, could you add a changelog entry?
Rosswell: @DamianCzajkowski done!
DamianCzajkowski: I've checked it, and it looks fine. Just some linters failed, so after you resolve them, I will merge it and deploy a new version.
Rosswell: @DamianCzajkowski fixed :)

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 86483f1..aef36b3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,7 +4,7 @@
 
 - Changed code typing to satisfy MyPy 1.11.0 version
 - Added support for `async_client=false` to work with `enable_custom_operations=true`
-
+- Fix propagating `convert_to_snake_case` to clients during package generation
 
 ## 0.14.0 (2024-07-17)
 
diff --git a/ariadne_codegen/client_generators/package.py b/ariadne_codegen/client_generators/package.py
index 5c91aa8..529ad7b 100644
--- a/ariadne_codegen/client_generators/package.py
+++ b/ariadne_codegen/client_generators/package.py
@@ -456,7 +456,10 @@ def get_package_generator(
         plugin_manager=plugin_manager,
     )
     custom_fields_generator = CustomFieldsGenerator(
-        schema=schema, custom_scalars=settings.scalars, plugin_manager=plugin_manager
+        schema=schema,
+        custom_scalars=settings.scalars,
+        convert_to_snake_case=settings.convert_to_snake_case,
+        plugin_manager=plugin_manager,
     )
     custom_fields_typing_generator = CustomFieldsTypingGenerator(schema=schema)
     custom_query_generator = None
@@ -466,6 +469,7 @@ def get_package_generator(
             name="Query",
             base_name=BASE_GRAPHQL_OPERATION_CLASS_NAME,
             enums_module_name=settings.enums_module_name,
+            convert_to_snake_case=settings.convert_to_snake_case,
             custom_scalars=settings.scalars,
             plugin_manager=plugin_manager,
             arguments_generator=ArgumentsGenerator(
@@ -482,6 +486,7 @@ def get_package_generator(
             name="Mutation",
             base_name=BASE_GRAPHQL_OPERATION_CLASS_NAME,
             enums_module_name=settings.enums_module_name,
+            convert_to_snake_case=settings.convert_to_snake_case,
             custom_scalars=settings.scalars,
             plugin_manager=plugin_manager,
             arguments_generator=ArgumentsGenerator(


Test patch:
diff --git a/tests/client_generators/package_generator/test_generator_generation.py b/tests/client_generators/package_generator/test_generator_generation.py
new file mode 100644
index 0000000..9c704fb
--- /dev/null
+++ b/tests/client_generators/package_generator/test_generator_generation.py
@@ -0,0 +1,168 @@
+from pathlib import Path
+
+from graphql import build_ast_schema, parse
+
+from ariadne_codegen.client_generators.package import get_package_generator
+from ariadne_codegen.client_generators.scalars import ScalarData
+from ariadne_codegen.plugins.manager import PluginManager
+from ariadne_codegen.settings import ClientSettings, CommentsStrategy
+
+
+def test_get_package_generator_without_default_settings(tmp_path: Path):
+    schema_path = tmp_path / "schema.graphql"
+    schema_path.touch()
+    queries_path = tmp_path / "queries.graphql"
+    queries_path.touch()
+    base_client_file_path = tmp_path / "valid_file.txt"
+    base_client_file_path.touch()
+    base_client_file_path.write_text("class Class")
+
+    test_schema = build_ast_schema(
+        parse(
+            """
+    schema {
+      query: Query
+      mutation: Mutation
+      subscription: Subscription
+    }
+    type Query {
+      query1(num: Int!): Int!
+    }
+    
+    type Mutation {
+        mutation1(num: Int!): Int!
+    }
+    
+    type Subscription {
+      subscription1(num: Int!): Int!
+    }
+    """
+        )
+    )
+
+    settings_without_defaults = ClientSettings(
+        schema_path=schema_path.as_posix(),
+        remote_schema_url="remote_schema_url",
+        remote_schema_headers={"header": "header"},
+        remote_schema_verify_ssl=False,
+        enable_custom_operations=True,
+        plugins=["imaplugin"],
+        queries_path=schema_path.as_posix(),
+        target_package_name="target_package_name",
+        target_package_path=tmp_path.as_posix(),
+        client_name="client_name",
+        client_file_name="client_file_name",
+        base_client_name="Class",
+        base_client_file_path=base_client_file_path.as_posix(),
+        enums_module_name="enums_module_name",
+        input_types_module_name="input_types_module_name",
+        fragments_module_name="fragments_module_name",
+        include_comments=CommentsStrategy.NONE,
+        convert_to_snake_case=False,
+        include_all_inputs=False,
+        include_all_enums=False,
+        async_client=False,
+        opentelemetry_client=True,
+        files_to_include=[schema_path.as_posix()],
+        scalars={"scalar1": ScalarData(type_="str", graphql_name="scalar1")},
+    )
+    package_generator = get_package_generator(
+        schema=test_schema,
+        fragments=[],
+        settings=settings_without_defaults,
+        plugin_manager=PluginManager(schema=test_schema),
+    )
+
+    # separate out the generated clients
+    client_generator = package_generator.client_generator
+    input_types_generator = package_generator.input_types_generator
+    fragments_generator = package_generator.fragments_generator
+    custom_query_generator = package_generator.custom_query_generator
+    custom_mutation_generator = package_generator.custom_mutation_generator
+
+    # client generator
+    # pylint: disable=protected-access
+    assert {i.module for i in client_generator._imports} == {
+        "typing",
+        "valid_file",
+        "base_model",
+    }
+    assert (
+        client_generator.arguments_generator.convert_to_snake_case
+        == settings_without_defaults.convert_to_snake_case
+    )
+    assert (
+        client_generator.arguments_generator.custom_scalars
+        == settings_without_defaults.scalars
+    )
+    assert client_generator.name == settings_without_defaults.client_name
+    # pylint: disable=protected-access
+    assert (
+        client_generator._class_def.bases[0].id
+        == settings_without_defaults.base_client_name
+    )
+    assert (
+        client_generator.enums_module_name
+        == settings_without_defaults.enums_module_name
+    )
+    assert (
+        client_generator.input_types_module_name
+        == settings_without_defaults.input_types_module_name
+    )
+    assert client_generator.custom_scalars == settings_without_defaults.scalars
+    # input types generator
+    assert (
+        input_types_generator.enums_module
+        == settings_without_defaults.enums_module_name
+    )
+    assert (
+        input_types_generator.convert_to_snake_case
+        == settings_without_defaults.convert_to_snake_case
+    )
+    assert input_types_generator.custom_scalars == settings_without_defaults.scalars
+    # fragments generator
+    assert (
+        fragments_generator.enums_module_name
+        == settings_without_defaults.enums_module_name
+    )
+    assert (
+        fragments_generator.convert_to_snake_case
+        == settings_without_defaults.convert_to_snake_case
+    )
+    assert fragments_generator.custom_scalars == settings_without_defaults.scalars
+    # custom query generator
+    assert (
+        custom_query_generator.enums_module_name
+        == settings_without_defaults.enums_module_name
+    )
+    assert custom_query_generator.custom_scalars == settings_without_defaults.scalars
+    assert (
+        custom_query_generator.convert_to_snake_case
+        == settings_without_defaults.convert_to_snake_case
+    )
+    assert (
+        custom_query_generator.arguments_generator.convert_to_snake_case
+        == settings_without_defaults.convert_to_snake_case
+    )
+    assert (
+        custom_query_generator.arguments_generator.custom_scalars
+        == settings_without_defaults.scalars
+    )
+    # custom mutation generator
+    assert (
+        custom_mutation_generator.enums_module_name
+        == settings_without_defaults.enums_module_name
+    )
+    assert custom_mutation_generator.custom_scalars == settings_without_defaults.scalars
+    assert (
+        custom_mutation_generator.convert_to_snake_case
+        == settings_without_defaults.convert_to_snake_case
+    )
+    assert (
+        custom_mutation_generator.arguments_generator.convert_to_snake_case
+        == settings_without_defaults.convert_to_snake_case
+    )
+    assert (
+        custom_mutation_generator.arguments_generator.custom_scalars
+        == settings_without_defaults.scalars
+    )