-
Notifications
You must be signed in to change notification settings - Fork 2
pydantic_modelsの追加 #698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
pydantic_modelsの追加 #698
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yuji38kwmt
commented
Jan 21, 2025
Comment on lines
+101
to
+115
replace_from_dict_method() { | ||
# `from_json`メソッドで`_type`が正しくない場合は`ValueError`を発生させるようにする | ||
# 本来は`from_dict`メソッドを修正すべきだが、 | ||
local type_value=$1 | ||
local filename=$2 | ||
sed "s/return cls\.from_dict(json\.loads(json_str))/result = cls.from_dict(json.loads(json_str))\\n if result.type != \"$type_value\": raise ValueError(\"Invalid type\")\\n return result/" out/openapi_client/models/${filename} --in-place | ||
} | ||
# `from_dict`メソッドで`oneOf`に該当するスキーマが複数見つかる場合はErrorが発生する | ||
# その場合は、`type`の値を判定するようにした | ||
replace_from_dict_method Movie system_metadata_movie.py | ||
replace_from_dict_method Image system_metadata_image.py | ||
replace_from_dict_method Custom system_metadata_custom.py | ||
replace_from_dict_method Classification full_annotation_data_classification.py | ||
replace_from_dict_method Segmentation full_annotation_data_segmentation.py | ||
replace_from_dict_method SegmentationV2 full_annotation_data_segmentation_v2.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parseできない場合のみ、_type
の値を判定するようにした。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
OpenAPI Generator v7を用いてpydantic_modelsを作成しました。
背景
もともと主要なスキーマのみdataclassを自動生成していました。
自動生成ではカスタムテンプレート(
models.mustache
)を用意して、catでファイルを連結したり複雑なことをしていました。メンテナンスをもう少し楽にするため、OpenAPI Generatorが生成したmodelsを採用することにしました。
OpenAPI Generator v4のときと比較すると、v7が生成したmodelsは利用しやすいです。
たとえばPydanticが使われています。
したがって、OpenAPI Generatorが生成したmodelsをそのまま利用することにしました。
考えたこと
既存の
models.py
は残すため、pydantic_models/
というディレクトリに配置しました。今後の運用
実際に使ってみて利用できそうなら、
annofabapi/models.py
,annofabapi/dataclass/*.py
を非推奨にする。