From faf9c4c9f070bb49006b494a90da8ae1f431dfb5 Mon Sep 17 00:00:00 2001 From: dekkku <161748598+dekkku@users.noreply.github.com> Date: Sun, 21 Jul 2024 16:42:04 +0530 Subject: [PATCH] fix: save singularized candidate name (#2487) --- ...ures__generator__jsonplaceholder.json.snap | 22 +++++++++---------- .../config/transformer/improve_type_names.rs | 14 +++++++----- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/cli/generator/snapshots/tailcall__cli__generator__generator__test__generator_spec__src__cli__generator__tests__fixtures__generator__jsonplaceholder.json.snap b/src/cli/generator/snapshots/tailcall__cli__generator__generator__test__generator_spec__src__cli__generator__tests__fixtures__generator__jsonplaceholder.json.snap index 13b7854e8f..3d513cd8db 100644 --- a/src/cli/generator/snapshots/tailcall__cli__generator__generator__test__generator_spec__src__cli__generator__tests__fixtures__generator__jsonplaceholder.json.snap +++ b/src/cli/generator/snapshots/tailcall__cli__generator__generator__test__generator_spec__src__cli__generator__tests__fixtures__generator__jsonplaceholder.json.snap @@ -14,6 +14,14 @@ type Address { zipcode: String } +type Comment { + body: String + email: String + id: Int + name: String + postId: Int +} + type Company { bs: String catchPhrase: String @@ -40,21 +48,13 @@ type Post { userId: Int } -type PostComment { - body: String - email: String - id: Int - name: String - postId: Int -} - type Query { - comment(p1: Int!): PostComment @http(path: "/comments/{{.args.p1}}") - comments: [PostComment] @http(path: "/comments") + comment(p1: Int!): Comment @http(path: "/comments/{{.args.p1}}") + comments: [Comment] @http(path: "/comments") photo(p1: Int!): Photo @http(path: "/photos/{{.args.p1}}") photos: [Photo] @http(path: "/photos") post(p1: Int!): Post @http(path: "/posts/{{.args.p1}}") - postComments(postId: Int): [PostComment] @http(path: "/comments", query: [{key: "postId", value: "{{.args.postId}}"}]) + postComments(postId: Int): [Comment] @http(path: "/comments", query: [{key: "postId", value: "{{.args.postId}}"}]) posts: [Post] @http(path: "/posts") todo(p1: Int!): Todo @http(path: "/todos/{{.args.p1}}") todos: [Todo] @http(path: "/todos") diff --git a/src/core/config/transformer/improve_type_names.rs b/src/core/config/transformer/improve_type_names.rs index 0b5971fbed..1ba7054dc5 100644 --- a/src/core/config/transformer/improve_type_names.rs +++ b/src/core/config/transformer/improve_type_names.rs @@ -38,16 +38,16 @@ impl<'a> CandidateConvergence<'a> { // Filter out candidates that have already been converged or are already present // in types let candidates_to_consider = candidate_list.iter().filter(|(candidate_name, _)| { - let singularized_candidate_name = candidate_name.to_singular().to_pascal_case(); - !converged_candidate_set.contains(&singularized_candidate_name) - && !self.config.types.contains_key(&singularized_candidate_name) + let candidate_type_name = candidate_name.to_pascal_case(); + !converged_candidate_set.contains(&candidate_type_name) + && !self.config.types.contains_key(&candidate_type_name) }); // Find the candidate with the highest frequency and priority if let Some((candidate_name, _)) = candidates_to_consider .max_by_key(|(key, value)| (value.frequency, value.priority, *key)) { - let singularized_candidate_name = candidate_name.to_singular().to_pascal_case(); + let singularized_candidate_name = candidate_name.to_pascal_case(); finalized_candidates .insert(type_name.to_owned(), singularized_candidate_name.clone()); converged_candidate_set.insert(singularized_candidate_name); @@ -86,7 +86,9 @@ impl<'a> CandidateGeneration<'a> { .entry(field_info.type_of.to_owned()) .or_default(); - if let Some(key_val) = inner_map.get_mut(field_name) { + let singularized_candidate = field_name.to_singular(); + + if let Some(key_val) = inner_map.get_mut(&singularized_candidate) { key_val.frequency += 1 } else { // in order to infer the types correctly, always prioritize the non-operation @@ -98,7 +100,7 @@ impl<'a> CandidateGeneration<'a> { }; inner_map.insert( - field_name.to_owned(), + singularized_candidate, CandidateStats { frequency: 1, priority }, ); }