Skip to content

Commit 552b74e

Browse files
committed
Remove illegal characters from identifiers in "examples"
1 parent 904ef3e commit 552b74e

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

pyopenapi/generator.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hashlib
12
from typing import Any, Dict, Set, Union
23

34
from strong_typing.core import JsonType
@@ -17,7 +18,7 @@
1718
SchemaOptions,
1819
get_schema_identifier,
1920
)
20-
from strong_typing.serialization import object_to_json
21+
from strong_typing.serialization import json_dump_string, object_to_json
2122

2223
from .operations import (
2324
EndpointOperation,
@@ -44,7 +45,6 @@
4445
TagGroup,
4546
)
4647

47-
4848
SchemaOrRef = Union[Schema, SchemaRef]
4949

5050

@@ -157,27 +157,50 @@ def build_media_type(
157157
if self.schema_transformer:
158158
schema_transformer: Callable[[SchemaOrRef], SchemaOrRef] = self.schema_transformer # type: ignore
159159
schema = schema_transformer(schema)
160+
161+
if not examples:
162+
return MediaType(schema=schema)
163+
164+
if len(examples) == 1:
165+
return MediaType(schema=schema, example=self._build_example(examples[0]))
166+
160167
return MediaType(
161168
schema=schema,
162169
examples=self._build_examples(examples),
163170
)
164171

165172
def _build_examples(
166-
self, examples: Optional[List[Any]] = None
167-
) -> Optional[Dict[str, Union[Example, ExampleRef]]]:
173+
self, examples: List[Any]
174+
) -> Dict[str, Union[Example, ExampleRef]]:
175+
"Creates a set of several examples for a media type."
176+
177+
if self.sample_transformer:
178+
sample_transformer: Callable[[JsonType], JsonType] = self.sample_transformer # type: ignore
179+
else:
180+
sample_transformer = lambda sample: sample
181+
182+
results: Dict[str, Union[Example, ExampleRef]] = {}
183+
for example in examples:
184+
value = sample_transformer(object_to_json(example))
185+
186+
hash_string = (
187+
hashlib.md5(json_dump_string(value).encode("utf-8")).digest().hex()
188+
)
189+
name = f"ex-{hash_string}"
190+
191+
results[name] = Example(value=value)
192+
193+
return results
168194

169-
if examples is None:
170-
return None
195+
def _build_example(self, example: Any) -> Any:
196+
"Creates a single example for a media type."
171197

172198
if self.sample_transformer:
173199
sample_transformer: Callable[[JsonType], JsonType] = self.sample_transformer # type: ignore
174200
else:
175201
sample_transformer = lambda sample: sample
176202

177-
return {
178-
str(example): Example(value=sample_transformer(object_to_json(example)))
179-
for example in examples
180-
}
203+
return sample_transformer(object_to_json(example))
181204

182205

183206
@dataclass
@@ -236,7 +259,7 @@ def _get_status_responses(
236259
if isinstance(example, response_type)
237260
)
238261

239-
return status_responses
262+
return dict(sorted(status_responses.items()))
240263

241264
def build_response(
242265
self, options: ResponseOptions

0 commit comments

Comments
 (0)