Skip to content

Conversation

chensjlv
Copy link

@chensjlv chensjlv commented Aug 5, 2025

PR Summary: Handle Package Name More Gracefully

Overview

This commit fixes a critical bug in field resolution where long package names caused incorrect message name extraction.

Changes Made

File: protobuf_to_pydantic/gen_model.py

  • Modified _get_field_info_dict_by_full_name() to accept package_name parameter
  • Added logic to strip package prefix from full names during field resolution
  • Updated related method signatures to pass package name consistently
  • Added package name extraction from descriptor file

Example Scenario

Consider a protobuf file with:

syntax = "proto3";
package analytics.dataplatform.user;

message UserProfile {
  string user_id = 1;
  int32 age = 2;
}

Why Previous Implementation Was Buggy

The old code would split analytics.dataplatform.user.UserProfile.user_id into ["analytics", "dataplatform", "user", "UserProfile", "user_id"] and incorrectly extract dataplatform as the message name (line 211: message_name, *key_list = split_full_name[1:]). This caused field validation rules to be lost since the lookup would search for dataplatform.user_id instead of UserProfile.user_id.

Benefits

  • Accurate Field Resolution: Properly handles long package names in field lookups
  • Preserved Validation Rules: Ensures field validation metadata is correctly applied
  • Consistent Behavior: Uniform package name handling across all message types

@chensjlv chensjlv changed the title handle package name more gracefully [BUGFIX] handle package name more gracefully Aug 5, 2025
@miloqinCedar
Copy link

miloqinCedar commented Aug 5, 2025

this would solve my case too
very nice to have

@so1n
Copy link
Owner

so1n commented Aug 17, 2025

Thanks for your PR. It would be great if you could add test cases. @chensjlv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants