Skip to content

Commit db35912

Browse files
v0.1.0 🌵
1 parent 8c3cbf1 commit db35912

7 files changed

Lines changed: 60 additions & 30 deletions

File tree

.github/workflows/build.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ on:
44
release:
55
types: [published]
66
push:
7-
tags: "v[1-9]+.[0-9]+.[0-9]+"
7+
tags:
8+
- "v[0-9]+.[0-9]+.[0-9]+"
89
branches:
910
- main
1011
- ci
@@ -59,6 +60,10 @@ jobs:
5960
path: junit/pytest-results-${{ matrix.python-version }}.xml
6061
if: always()
6162

63+
- name: Codecov
64+
run: |
65+
bash <(curl -s https://codecov.io/bash)
66+
6267
- name: Install distribution dependencies
6368
run: pip install --upgrade twine setuptools wheel
6469
if: matrix.python-version == 3.8 || matrix.python-version == 3.9

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[![pypi](https://img.shields.io/pypi/v/essentials-openapi.svg)](https://pypi.python.org/pypi/essentials-openapi)
33
[![versions](https://img.shields.io/pypi/pyversions/essentials-openapi.svg)](https://github.com/neoteroi/essentials-openapi)
44
[![license](https://img.shields.io/github/license/neoteroi/essentials-openapi.svg)](https://github.com/neoteroi/essentials-openapi/blob/master/LICENSE)
5+
[![codecov](https://codecov.io/gh/Neoteroi/essentials-openapi/branch/main/graph/badge.svg?token=WEZ8YECJDF)](https://codecov.io/gh/Neoteroi/essentials-openapi)
56

67
# essentials-openapi
78

openapidocs/common.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
import copy
22
import json
3-
from essentials.json import FriendlyEncoder
4-
from dataclasses import asdict, fields, is_dataclass
3+
from dataclasses import fields, is_dataclass
54
from enum import Enum
65
from typing import Any, List, Tuple
76

87
import yaml
8+
from essentials.json import FriendlyEncoder
99

1010

1111
class Format(Enum):
1212
YAML = "YAML"
1313
JSON = "JSON"
1414

1515

16-
class EntitiesJSONEncoder(FriendlyEncoder):
17-
def default(self, obj: Any) -> Any:
18-
try:
19-
return super().default(obj)
20-
except TypeError:
21-
if is_dataclass(obj):
22-
return {k: v for k, v in asdict(obj).items() if v is not None}
23-
raise
24-
25-
2616
class OpenAPIRoot:
2717
"""Base class for a root OpenAPI Documentation"""
2818

@@ -67,8 +57,6 @@ def _asdict_inner(obj, dict_factory):
6757
value = _asdict_inner(getattr(obj, f.name), dict_factory)
6858
result.append((f.name, value))
6959
return dict_factory(result)
70-
elif isinstance(obj, tuple) and hasattr(obj, "_fields"):
71-
return type(obj)(*[_asdict_inner(v, dict_factory) for v in obj])
7260
elif isinstance(obj, (list, tuple)):
7361
return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
7462
elif isinstance(obj, dict):
@@ -104,7 +92,7 @@ def to_obj(self, item: Any) -> Any:
10492

10593
def to_json(self, item: Any) -> str:
10694
return json.dumps(
107-
self.to_obj(item), indent=4, ensure_ascii=False, cls=EntitiesJSONEncoder
95+
self.to_obj(item), indent=4, ensure_ascii=False, cls=FriendlyEncoder
10896
)
10997

11098
def to_yaml(self, item: Any) -> str:

openapidocs/v2.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from abc import ABC
77
from enum import Enum
88
from dataclasses import dataclass
9-
from openapidocs.common import OpenAPIRoot, normalize_dict
9+
from openapidocs.common import OpenAPIRoot
1010
from typing import Any, List, Optional, Dict, Type, Union
1111

1212

@@ -339,18 +339,6 @@ class Tag:
339339
external_docs: Optional[ExternalDocs] = None
340340

341341

342-
@dataclass
343-
class Security:
344-
requirements: List[SecurityRequirement]
345-
optional: bool = False
346-
347-
def to_obj(self):
348-
items = [normalize_dict(item) for item in self.requirements]
349-
if self.optional:
350-
items.insert(0, {})
351-
return items
352-
353-
354342
@dataclass
355343
class OpenAPI(OpenAPIRoot):
356344
swagger: str = "2.0"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def readme():
88

99
setup(
1010
name="essentials-openapi",
11-
version="0.0.9",
11+
version="0.1.0",
1212
description="Classes to generate OpenAPI Documentation v3 and v2, in JSON and YAML",
1313
long_description=readme(),
1414
long_description_content_type="text/markdown",

tests/test_common.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
from enum import Enum
3+
from openapidocs.common import Serializer, normalize_key, normalize_dict_factory
4+
5+
6+
class ExampleType(Enum):
7+
A = "a"
8+
B = "b"
9+
10+
11+
def test_serializer_raises_for_unsupported_class():
12+
class A:
13+
pass
14+
15+
with pytest.raises(TypeError):
16+
Serializer().to_json(A())
17+
18+
19+
@pytest.mark.parametrize(
20+
"value,expected_result",
21+
[
22+
("one", "one"),
23+
("snake_case", "snakeCase"),
24+
("snake_case_really", "snakeCaseReally"),
25+
(10, 10),
26+
(ExampleType.A, "a"),
27+
],
28+
)
29+
def test_normalize_key(value, expected_result):
30+
assert normalize_key(value) == expected_result
31+
32+
33+
def test_normalize_dict_factory():
34+
class A:
35+
def to_obj(self):
36+
return {"x": 1}
37+
38+
values = [("a", A())]
39+
40+
assert normalize_dict_factory(values) == {"a": {"x": 1}}

tests/test_v2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,3 +1257,11 @@ def test_equality(example_type: Type[TestItem]) -> None:
12571257
one = example.get_instance()
12581258
two = example.get_instance()
12591259
assert one == two
1260+
1261+
1262+
@pytest.mark.parametrize(
1263+
"value,expected_result",
1264+
[("one", "#/definitions/one"), (Contact, "#/definitions/Contact")],
1265+
)
1266+
def test_get_ref(value, expected_result):
1267+
assert get_ref(value) == expected_result

0 commit comments

Comments
 (0)