so1n / protobuf_to_pydantic

Generate a pydantic.BaseModel with parameter verification function from the Python Message object(by the Protobuf file).
Apache License 2.0
80 stars 6 forks source link

It generates invalid Python code for some message structure #64

Closed hizbiz closed 3 weeks ago

hizbiz commented 1 month ago

Describe the bug For some message structure, it generates invalid Python code.

To Reproduce Steps to reproduce the behavior:

  1. generate pydantic model from the following proto file:
    
    syntax = "proto3";

message MessageA { string aMessage = 1; MessageB complexMessage = 2; }

message MessageB { string bMessage = 1; MessageC cMessage = 2;

message MessageC { string text = 1; } }


**Expected behavior**
Valid Pydantic Python code be generated.

Instead, the generated code loooks like this:
```python
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.7](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 5.27.3 
# Pydantic Version: 2.8.2 
from xyz import BaseModel
from google.protobuf.message import Message  # type: ignore
from pydantic import Field

class MessageC(BaseModel):
    text: str = Field(default="")

class MessageB(BaseModel):
class MessageC(BaseModel):
    text: str = Field(default="")

    bMessage: str = Field(default="")
    cMessage: MessageC = Field()

class MessageA(BaseModel):
    aMessage: str = Field(default="")
    complexMessage: MessageB = Field()

Desktop (please complete the following information):

so1n commented 3 weeks ago

Thanks for your feedback, I have fixed this issue (https://github.com/so1n/protobuf_to_pydantic/pull/66)