Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions demo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";
import "example/example_proto/common/p2p_validate.proto";
package example;

message MyMessage1
{
optional string content = 1;
}

message MyMessage2
{
// fix https://github.com/so1n/protobuf_to_pydantic/issues/82
optional MyMessage1 my_message1 = 1[(p2p_validate.rules).message.required=true];
// fix https://github.com/so1n/protobuf_to_pydantic/issues/85
optional MyMessage1 my_message2 = 2;
MyMessage1 my_message3 = 3[(p2p_validate.rules).message.required=true];
MyMessage1 my_message4 = 4;
}

message MyMessage3
{
// p2p: {"required": true}
optional MyMessage1 my_message1 = 1;
optional MyMessage1 my_message2 = 2;
// p2p: {"required": true}
MyMessage1 my_message3 = 3;
MyMessage1 my_message4 = 4;
}
26 changes: 26 additions & 0 deletions demo_p2p.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[0.0.0](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 4.24.4
# Pydantic Version: 2.9.2
import typing

from google.protobuf.message import Message # type: ignore
from pydantic import BaseModel, Field


class MyMessage1(BaseModel):
content: typing.Optional[str] = Field(default="")


class MyMessage2(BaseModel):
my_message1: typing.Optional[MyMessage1] = Field()
my_message2: typing.Optional[MyMessage1] = Field(default_factory=MyMessage1)
my_message3: MyMessage1 = Field()
my_message4: MyMessage1 = Field(default_factory=MyMessage1)


class MyMessage3(BaseModel):
my_message1: typing.Optional[MyMessage1] = Field()
my_message2: typing.Optional[MyMessage1] = Field(default_factory=MyMessage1)
my_message3: MyMessage1 = Field()
my_message4: MyMessage1 = Field(default_factory=MyMessage1)
10 changes: 10 additions & 0 deletions example/example_proto/p2p_validate/demo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,13 @@ message AfterReferMessage {
string uid=1 [(p2p_validate.rules).string = {example: "10086", title: "UID", description: "user union id", miss_default: true}];
int32 age=2 [(p2p_validate.rules).int32 = {example: 18, title: "use age", ge: 0}];
}


message OptionalMessage{
// fix https://github.com/so1n/protobuf_to_pydantic/issues/82
optional MessageIgnoredTest my_message1 = 1[(p2p_validate.rules).message.required=true];
// fix https://github.com/so1n/protobuf_to_pydantic/issues/85
optional MessageIgnoredTest my_message2 = 2;
MessageIgnoredTest my_message3 = 3[(p2p_validate.rules).message.required=true];
MessageIgnoredTest my_message4 = 4;
}
11 changes: 11 additions & 0 deletions example/example_proto/p2p_validate_by_comment/demo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,14 @@ message AfterReferMessage {
// p2p: {"example": 18, "title": "use age", "ge": 0}
int32 age=2;
}

message OptionalMessage{
// fix https://github.com/so1n/protobuf_to_pydantic/issues/82
// p2p: {"required": true}
optional MessageIgnoredTest my_message1 = 1;
// fix https://github.com/so1n/protobuf_to_pydantic/issues/85
optional MessageIgnoredTest my_message2 = 2;
// p2p: {"required": true}
MessageIgnoredTest my_message3 = 3;
MessageIgnoredTest my_message4 = 4;
}
22 changes: 12 additions & 10 deletions example/proto_3_20_pydanticv1/demo_gen_code.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand All @@ -22,7 +22,7 @@ class SubMessage(BaseModel):
text: str = Field(default="")

field1: str = Field(default="")
field2: SubMessage = Field()
field2: SubMessage = Field(default_factory=SubMessage)


class EmptyMessage(BaseModel):
Expand Down Expand Up @@ -82,7 +82,9 @@ class UserMessage(BaseModel):
demo: ExampleExampleProtoCommonSingleDemoEnum = Field(default=0)
is_adult: bool = Field(default=False)
user_name: str = Field(default="")
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field()
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(
default_factory=ExampleExampleProtoCommonSingleDemoMessage
)


class MapMessage(BaseModel):
Expand All @@ -109,11 +111,11 @@ class IncludeEnum(IntEnum):

user_list_map: typing.Dict[str, RepeatedMessage] = Field(default_factory=dict)
user_map: typing.Dict[str, MapMessage] = Field(default_factory=dict)
user_pay: UserPayMessage = Field()
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
include_enum: IncludeEnum = Field(default=0)
not_enable_user_pay: UserPayMessage = Field()
not_enable_user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
empty: typing.Any = Field()
after_refer: AfterReferMessage = Field()
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)


class OptionalMessage(BaseModel):
Expand All @@ -123,7 +125,7 @@ class OptionalMessage(BaseModel):
y: int = Field(default=0)
name: typing.Optional[str] = Field(default="")
age: typing.Optional[int] = Field(default=0)
item: typing.Optional[InvoiceItem] = Field()
item: typing.Optional[InvoiceItem] = Field(default_factory=InvoiceItem)
str_list: typing.List[str] = Field(default_factory=list)
int_map: typing.Dict[str, int] = Field(default_factory=dict)
default_template_test: float = Field(default=0.0)
Expand All @@ -142,23 +144,23 @@ class Config:

class RootMessage(BaseModel):
field1: str = Field(default="")
field2: AnOtherMessage = Field()
field2: AnOtherMessage = Field(default_factory=AnOtherMessage)


class TestSameName0(BaseModel):
class Body(BaseModel):
input_model: str = Field(default="")
input_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()
body: Body = Field(default_factory=Body)


class TestSameName1(BaseModel):
class Body(BaseModel):
output_model: str = Field(default="")
output_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()
body: Body = Field(default_factory=Body)


class Demo1(BaseModel):
Expand Down
15 changes: 11 additions & 4 deletions example/proto_3_20_pydanticv1/demo_gen_code_by_p2p.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down Expand Up @@ -435,10 +435,10 @@ class NotEnableUserPayMessage(BaseModel):

string_in_map_test: typing.Dict[str, StringTest] = Field(default_factory=dict)
map_in_map_test: typing.Dict[str, MapTest] = Field(default_factory=dict)
user_pay: UserPayMessage = Field()
not_enable_user_pay: NotEnableUserPayMessage = Field()
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
not_enable_user_pay: NotEnableUserPayMessage = Field(default_factory=NotEnableUserPayMessage)
empty: typing.Any = Field()
after_refer: AfterReferMessage = Field()
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)


class OneOfNotTest(BaseModel):
Expand Down Expand Up @@ -476,6 +476,13 @@ class OneOfTest(BaseModel):
one_of_validator = root_validator(pre=True, allow_reuse=True)(check_one_of)


class OptionalMessage(BaseModel):
my_message1: typing.Optional[MessageIgnoredTest] = Field()
my_message2: typing.Optional[MessageIgnoredTest] = Field(default_factory=MessageIgnoredTest)
my_message3: MessageIgnoredTest = Field()
my_message4: MessageIgnoredTest = Field(default_factory=MessageIgnoredTest)


class RepeatedTest(BaseModel):
range_test: typing.List[str] = Field(default_factory=list, min_items=1, max_items=5)
unique_test: typing.List[str] = Field(default_factory=list, unique_items=True)
Expand Down
8 changes: 4 additions & 4 deletions example/proto_3_20_pydanticv1/demo_gen_code_by_pgv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand Down Expand Up @@ -290,10 +290,10 @@ class NotEnableUserPayMessage(BaseModel):

string_in_map_test: typing.Dict[str, StringTest] = Field(default_factory=dict)
map_in_map_test: typing.Dict[str, MapTest] = Field(default_factory=dict)
user_pay: UserPayMessage = Field()
not_enable_user_pay: NotEnableUserPayMessage = Field()
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
not_enable_user_pay: NotEnableUserPayMessage = Field(default_factory=NotEnableUserPayMessage)
empty: typing.Any = Field()
after_refer: AfterReferMessage = Field()
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)


class OneOfNotTest(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand All @@ -25,7 +25,7 @@ class SubMessage(BaseModel):
text: str = Field(default="")

field1: str = Field(default="")
field2: SubMessage = Field()
field2: SubMessage = Field(default_factory=SubMessage)


class EmptyMessage(BaseModel):
Expand Down Expand Up @@ -85,7 +85,9 @@ class UserMessage(BaseModel):
demo: ExampleExampleProtoCommonSingleDemoEnum = Field(default=0)
is_adult: bool = Field(default=False)
user_name: str = Field(default="", description="user name", min_length=1, max_length=10, example="so1n")
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(customer_string="c1", customer_int=1)
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(
default_factory=ExampleExampleProtoCommonSingleDemoMessage, customer_string="c1", customer_int=1
)


class MapMessage(BaseModel):
Expand All @@ -112,10 +114,10 @@ class IncludeEnum(IntEnum):

user_list_map: typing.Dict[str, RepeatedMessage] = Field(default_factory=dict)
user_map: typing.Dict[str, MapMessage] = Field(default_factory=dict)
user_pay: UserPayMessage = Field()
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
include_enum: IncludeEnum = Field(default=0)
empty: typing.Any = Field()
after_refer: AfterReferMessage = Field()
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)


class OptionalMessage(BaseModel):
Expand All @@ -125,7 +127,7 @@ class OptionalMessage(BaseModel):
y: int = Field(default=0, alias="yy", title="use age", ge=0.0, example=18)
name: typing.Optional[str] = Field(default="")
age: typing.Optional[int] = Field(default=0)
item: typing.Optional[InvoiceItem] = Field()
item: typing.Optional[InvoiceItem] = Field(default_factory=InvoiceItem)
str_list: typing.List[str] = Field(default_factory=list)
int_map: typing.Dict[str, int] = Field(default_factory=dict)
default_template_test: float = Field(default=1600000000.0)
Expand All @@ -144,20 +146,20 @@ class Config:

class RootMessage(BaseModel):
field1: str = Field(default="")
field2: AnOtherMessage = Field()
field2: AnOtherMessage = Field(default_factory=AnOtherMessage)


class TestSameName0(BaseModel):
class Body(BaseModel):
input_model: str = Field(default="")
input_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()
body: Body = Field(default_factory=Body)


class TestSameName1(BaseModel):
class Body(BaseModel):
output_model: str = Field(default="")
output_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()
body: Body = Field(default_factory=Body)
20 changes: 11 additions & 9 deletions example/proto_3_20_pydanticv1/demo_gen_code_by_text_comment_pyi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand All @@ -25,7 +25,7 @@ class SubMessage(BaseModel):
text: str = Field(default="")

field1: str = Field(default="")
field2: SubMessage = Field()
field2: SubMessage = Field(default_factory=SubMessage)


class EmptyMessage(BaseModel):
Expand Down Expand Up @@ -85,7 +85,9 @@ class UserMessage(BaseModel):
demo: ExampleExampleProtoCommonSingleDemoEnum = Field(default=0)
is_adult: bool = Field(default=False)
user_name: str = Field(default="", description="user name", min_length=1, max_length=10, example="so1n")
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(customer_string="c1", customer_int=1)
demo_message: ExampleExampleProtoCommonSingleDemoMessage = Field(
default_factory=ExampleExampleProtoCommonSingleDemoMessage, customer_string="c1", customer_int=1
)


class MapMessage(BaseModel):
Expand All @@ -112,10 +114,10 @@ class IncludeEnum(IntEnum):

user_list_map: typing.Dict[str, RepeatedMessage] = Field(default_factory=dict)
user_map: typing.Dict[str, MapMessage] = Field(default_factory=dict)
user_pay: UserPayMessage = Field()
user_pay: UserPayMessage = Field(default_factory=UserPayMessage)
include_enum: IncludeEnum = Field(default=0)
empty: typing.Any = Field()
after_refer: AfterReferMessage = Field()
after_refer: AfterReferMessage = Field(default_factory=AfterReferMessage)


class OptionalMessage(BaseModel):
Expand All @@ -125,7 +127,7 @@ class OptionalMessage(BaseModel):
y: int = Field(default=0, alias="yy", title="use age", ge=0.0, example=18)
name: typing.Optional[str] = Field(default="")
age: typing.Optional[int] = Field(default=0)
item: typing.Optional[InvoiceItem] = Field()
item: typing.Optional[InvoiceItem] = Field(default_factory=InvoiceItem)
str_list: typing.List[str] = Field(default_factory=list)
int_map: typing.Dict[str, int] = Field(default_factory=dict)
default_template_test: float = Field(default=1600000000.0)
Expand All @@ -144,20 +146,20 @@ class Config:

class RootMessage(BaseModel):
field1: str = Field(default="")
field2: AnOtherMessage = Field()
field2: AnOtherMessage = Field(default_factory=AnOtherMessage)


class TestSameName0(BaseModel):
class Body(BaseModel):
input_model: str = Field(default="")
input_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()
body: Body = Field(default_factory=Body)


class TestSameName1(BaseModel):
class Body(BaseModel):
output_model: str = Field(default="")
output_info: typing.Dict[str, str] = Field(default_factory=dict)

body: Body = Field()
body: Body = Field(default_factory=Body)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
from enum import IntEnum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.3.0.3](https://github.com/so1n/protobuf_to_pydantic)
# gen by protobuf_to_pydantic[v0.3.0.4](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 3.20.3
# Pydantic Version: 1.10.7
import typing
Expand All @@ -25,10 +25,10 @@ class ReportData(MyBaseSchema):

_one_of_dict = {"ReportData.data": {"fields": {"location_value", "time_value"}, "required": True}}
one_of_validator = root_validator(pre=True, allow_reuse=True)(check_one_of)
location_value: typing.Optional[GeoLocation] = Field(default=None)
location_value: typing.Optional[GeoLocation] = Field(default_factory=GeoLocation)
time_value: datetime = Field(default_factory=datetime.now)


class Report(MyBaseSchema):
source_id: typing.Optional[str] = Field(default="")
data: ReportData = Field()
data: ReportData = Field(default_factory=ReportData)
Loading
Loading