Skip to content

Commit 9d569c3

Browse files
committed
typeの判定
1 parent 7cea7e4 commit 9d569c3

File tree

6 files changed

+8733
-4
lines changed

6 files changed

+8733
-4
lines changed

annofabapi/pydantic_models/system_metadata_custom.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ def to_json(self) -> str:
4646
@classmethod
4747
def from_json(cls, json_str: str) -> Optional[Self]:
4848
"""Create an instance of SystemMetadataCustom from a JSON string"""
49-
return cls.from_dict(json.loads(json_str))
49+
result = cls.from_dict(json.loads(json_str))
50+
if result.type != "Custom":
51+
raise ValueError("Invalid type")
52+
return result
5053

5154
def to_dict(self) -> Dict[str, Any]:
5255
"""Return the dictionary representation of the model using alias.

annofabapi/pydantic_models/system_metadata_image.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ def to_json(self) -> str:
5050
@classmethod
5151
def from_json(cls, json_str: str) -> Optional[Self]:
5252
"""Create an instance of SystemMetadataImage from a JSON string"""
53-
return cls.from_dict(json.loads(json_str))
53+
result = cls.from_dict(json.loads(json_str))
54+
if result.type != "Image":
55+
raise ValueError("Invalid type")
56+
return result
5457

5558
def to_dict(self) -> Dict[str, Any]:
5659
"""Return the dictionary representation of the model using alias.

annofabapi/pydantic_models/system_metadata_movie.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ def to_json(self) -> str:
4949
@classmethod
5050
def from_json(cls, json_str: str) -> Optional[Self]:
5151
"""Create an instance of SystemMetadataMovie from a JSON string"""
52-
return cls.from_dict(json.loads(json_str))
52+
result = cls.from_dict(json.loads(json_str))
53+
if result.type != "Movie":
54+
raise ValueError("Invalid type")
55+
return result
5356

5457
def to_dict(self) -> Dict[str, Any]:
5558
"""Return the dictionary representation of the model using alias.

generate/generate.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ rm -Rf out/openapi_client
8686
# modelsを生成
8787
cat swagger/swagger-partial-header.yaml swagger/swagger-api-components.yaml > swagger/swagger-models.yaml
8888

89-
# datetime型を含むクラスでは、JSONにserializeできなかったので、str型にする
89+
# datetime型を含むクラスでは、JSONにserializeiできなかったので、str型にする
9090
# https://github.com/OpenAPITools/openapi-generator/issues/19517
9191
docker run --rm -u `id -u`:`id -g` -v ${PWD}:/local -w /local -e JAVA_OPTS=${JAVA_OPTS} ${DOCKER_IMAGE} generate \
9292
--input-spec swagger/swagger-models.yaml \
@@ -96,6 +96,20 @@ docker run --rm -u `id -u`:`id -g` -v ${PWD}:/local -w /local -e JAVA_OPTS=${
9696
--global-property models,modelTests=false,modelDocs=false \
9797

9898
sed 's/from openapi_client.models./from annofabapi.pydantic_models./g' out/openapi_client/models/*.py --in-place
99+
100+
101+
replace_from_dict_method() {
102+
# `from_json`メソッドで`_type`が正しくない場合は`ValueError`を発生させるようにする
103+
# 本来は`from_dict`メソッドを修正すべきだが、
104+
local type_value=$1
105+
local filename=$2
106+
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
107+
}
108+
109+
replace_from_dict_method Movie system_metadata_movie.py
110+
replace_from_dict_method Image system_metadata_image.py
111+
replace_from_dict_method Custom system_metadata_custom.py
112+
99113
cp out/openapi_client/models/*.py ../annofabapi/pydantic_models
100114
rm -Rf out/openapi_client
101115

0 commit comments

Comments
 (0)