|
| 1 | +import hashlib |
1 | 2 | from typing import Any, Dict, Set, Union
|
2 | 3 |
|
3 | 4 | from strong_typing.core import JsonType
|
|
17 | 18 | SchemaOptions,
|
18 | 19 | get_schema_identifier,
|
19 | 20 | )
|
20 |
| -from strong_typing.serialization import object_to_json |
| 21 | +from strong_typing.serialization import json_dump_string, object_to_json |
21 | 22 |
|
22 | 23 | from .operations import (
|
23 | 24 | EndpointOperation,
|
|
44 | 45 | TagGroup,
|
45 | 46 | )
|
46 | 47 |
|
47 |
| - |
48 | 48 | SchemaOrRef = Union[Schema, SchemaRef]
|
49 | 49 |
|
50 | 50 |
|
@@ -157,27 +157,50 @@ def build_media_type(
|
157 | 157 | if self.schema_transformer:
|
158 | 158 | schema_transformer: Callable[[SchemaOrRef], SchemaOrRef] = self.schema_transformer # type: ignore
|
159 | 159 | 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 | + |
160 | 167 | return MediaType(
|
161 | 168 | schema=schema,
|
162 | 169 | examples=self._build_examples(examples),
|
163 | 170 | )
|
164 | 171 |
|
165 | 172 | 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 |
168 | 194 |
|
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." |
171 | 197 |
|
172 | 198 | if self.sample_transformer:
|
173 | 199 | sample_transformer: Callable[[JsonType], JsonType] = self.sample_transformer # type: ignore
|
174 | 200 | else:
|
175 | 201 | sample_transformer = lambda sample: sample
|
176 | 202 |
|
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)) |
181 | 204 |
|
182 | 205 |
|
183 | 206 | @dataclass
|
@@ -236,7 +259,7 @@ def _get_status_responses(
|
236 | 259 | if isinstance(example, response_type)
|
237 | 260 | )
|
238 | 261 |
|
239 |
| - return status_responses |
| 262 | + return dict(sorted(status_responses.items())) |
240 | 263 |
|
241 | 264 | def build_response(
|
242 | 265 | self, options: ResponseOptions
|
|
0 commit comments