Skip to content

Commit f17b699

Browse files
committed
Add tests
1 parent 579f3a6 commit f17b699

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

openapi_python_client/parser/openapi.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def from_data(
388388
else:
389389
name = data.operationId
390390

391-
result: Union[Endpoint, ParseError] = Endpoint(
391+
endpoint = Endpoint(
392392
path=path,
393393
method=method,
394394
summary=utils.remove_string_escapes(data.summary) if data.summary else "",
@@ -398,11 +398,8 @@ def from_data(
398398
tag=tag,
399399
)
400400

401-
if isinstance(result, ParseError):
402-
return result, schemas, parameters
403-
404401
result, schemas, parameters = Endpoint.add_parameters(
405-
endpoint=result,
402+
endpoint=endpoint,
406403
data=data,
407404
schemas=schemas,
408405
parameters=parameters,

tests/test_parser/test_bodies.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from openapi_python_client import Config
2+
from openapi_python_client import schema as oai
3+
from openapi_python_client.parser.bodies import body_from_data
4+
from openapi_python_client.parser.errors import ParseError
5+
from openapi_python_client.parser.properties import Schemas
6+
7+
8+
def test_errors():
9+
operation = oai.Operation(
10+
requestBody=oai.RequestBody(
11+
content={
12+
"invalid content type": oai.MediaType(
13+
media_type_schema=oai.Schema(
14+
type=oai.DataType.STRING,
15+
)
16+
),
17+
"application/json": oai.MediaType(
18+
media_type_schema=None # Missing media type schema is an error
19+
),
20+
"text/html": oai.MediaType( # content type not supported by the generator
21+
media_type_schema=oai.Schema(
22+
type=oai.DataType.STRING,
23+
)
24+
),
25+
"application/sushi+json": oai.MediaType(
26+
media_type_schema=oai.Schema(
27+
type=oai.DataType.INTEGER,
28+
default="make this an invalid property",
29+
)
30+
),
31+
}
32+
),
33+
responses={},
34+
)
35+
36+
errs, _ = body_from_data(data=operation, schemas=Schemas(), config=Config(), endpoint_name="this will not succeed")
37+
38+
assert len(errs) == len(operation.request_body.content)
39+
assert all(isinstance(err, ParseError) for err in errs)

tests/test_parser/test_openapi.py

+44
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from openapi_python_client.parser.errors import ParseError
99
from openapi_python_client.parser.openapi import Endpoint, EndpointCollection
1010
from openapi_python_client.parser.properties import IntProperty, Parameters, Schemas
11+
from openapi_python_client.schema import DataType
1112

1213
MODULE_NAME = "openapi_python_client.parser.openapi"
1314

@@ -874,6 +875,49 @@ def test_from_data_no_security(self, mocker):
874875
config=config,
875876
)
876877

878+
def test_from_data_some_bad_bodies(self):
879+
endpoint, _, _ = Endpoint.from_data(
880+
data=oai.Operation(
881+
responses={},
882+
requestBody=oai.RequestBody(
883+
content={
884+
"application/json": oai.MediaType(media_type_schema=oai.Schema(type=DataType.STRING)),
885+
"not a real media type": oai.MediaType(media_type_schema=oai.Schema(type=DataType.STRING)),
886+
},
887+
),
888+
),
889+
schemas=Schemas(),
890+
config=Config(),
891+
parameters=Parameters(),
892+
tag="tag",
893+
path="/",
894+
method="get",
895+
)
896+
897+
assert isinstance(endpoint, Endpoint)
898+
assert len(endpoint.bodies) == 1
899+
assert len(endpoint.errors) == 1
900+
901+
def test_from_data_all_bodies_bad(self):
902+
endpoint, _, _ = Endpoint.from_data(
903+
data=oai.Operation(
904+
responses={},
905+
requestBody=oai.RequestBody(
906+
content={
907+
"not a real media type": oai.MediaType(media_type_schema=oai.Schema(type=DataType.STRING)),
908+
},
909+
),
910+
),
911+
schemas=Schemas(),
912+
config=Config(),
913+
parameters=Parameters(),
914+
tag="tag",
915+
path="/",
916+
method="get",
917+
)
918+
919+
assert isinstance(endpoint, ParseError)
920+
877921
@pytest.mark.parametrize(
878922
"response_types, expected",
879923
(([], "Any"), (["Something"], "Something"), (["First", "Second", "Second"], "Union[First, Second]")),

0 commit comments

Comments
 (0)