Skip to content

Commit

Permalink
Fix include/exclude processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Heurtier committed Jan 19, 2024
1 parent 141ab31 commit 247e19e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
17 changes: 11 additions & 6 deletions sphinxcontrib/openapi/openapi30.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,25 +659,30 @@ def openapihttpdomain(spec, **options):

# Check against regular expressions to be included
if 'include' in options:
# use a set to avoid duplicates
new_paths = set()
for i in options['include']:
ir = re.compile(i)
for path in spec['paths']:
if ir.match(path):
paths.append(path)
new_paths.add(path)
paths = list(new_paths)

# If no include nor paths option, then take full path
if 'include' not in options and 'paths' not in options:
paths = spec['paths']
paths = list(spec['paths'].keys())

# Remove paths matching regexp
if 'exclude' in options:
_paths = []
exc_paths = set()
for e in options['exclude']:
er = re.compile(e)
for path in paths:
if not er.match(path):
_paths.append(path)
paths = _paths
if er.match(path):
exc_paths.add(path)
# remove like that to preserve order
for path in exc_paths:
paths.remove(path)

render_request = False
if 'request' in options:
Expand Down
17 changes: 11 additions & 6 deletions sphinxcontrib/openapi/renderers/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,25 +377,30 @@ def render_restructuredtext_markup(self, spec):

# Check against regular expressions to be included
if 'include' in self._options:
# use a set to avoid duplicates
new_entities = set()
for i in self._options['include']:
ir = re.compile(i)
for entity in schemas.keys():
if ir.match(entity):
entities.append(entity)
new_entities.add(entity)
entities = list(new_entities)

# If no include nor entities option, then take full entity
if 'include' not in self._options and 'entities' not in self._options:
entities = schemas.keys()
entities = list(schemas.keys())

# Remove entities matching regexp
if 'exclude' in self._options:
tmp_entities = []
exc_entities = set()
for e in self._options['exclude']:
er = re.compile(e)
for entity in entities:
if not er.match(entity):
tmp_entities.append(entity)
entities = tmp_entities
if er.match(entity):
exc_entities.add(entity)
# remove like that to preserve order
for entity in exc_entities:
entities.remove(entity)

def __entities(x):
return _entities(spec, x)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ def test_filtering(self):
-
""")

renderer = renderers.ModelRenderer(None, {"include": ["A.*"], "exclude": [".*B"]})
renderer = renderers.ModelRenderer(None, {"include": ["A.*", "AB.*"], "exclude": ["B", ".*B"]})
text = '\n'.join(renderer.render_restructuredtext_markup(spec))
assert text == textwrap.dedent("""
.. _/components/schemas/A:
Expand All @@ -1148,7 +1148,7 @@ def test_filtering(self):
-
""")

renderer = renderers.ModelRenderer(None, {"entities": ["AB", "B"], "exclude": ["AB"]})
renderer = renderers.ModelRenderer(None, {"entities": ["AB", "B"], "exclude": ["AB", "A"]})
text = '\n'.join(renderer.render_restructuredtext_markup(spec))
assert text == textwrap.dedent("""
.. _/components/schemas/B:
Expand Down
25 changes: 15 additions & 10 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,16 @@ def test_path_option(self):

def test_include_option(self):
spec = collections.defaultdict(collections.OrderedDict)
spec['paths']['/resource_a'] = {
spec['openapi'] ='3.0.0'
spec['paths']['/Aresource_a'] = {
'get': {
'description': 'resource a',
'responses': {
'200': {'description': 'ok'},
}
}
}
spec['paths']['/resource_b'] = {
spec['paths']['/Bresource_b'] = {
'post': {
'description': 'resource b',
'responses': {
Expand All @@ -331,37 +332,40 @@ def test_include_option(self):

renderer = renderers.HttpdomainOldRenderer(None, {'include': [
'/resource',
'/A.*',
'/Bresource.*'
]})
text = '\n'.join(renderer.render_restructuredtext_markup(spec))
assert text == textwrap.dedent('''
.. http:get:: /resource_a
.. http:get:: /Aresource_a
:synopsis: null
resource a
:status 200:
ok
ok.
.. http:post:: /resource_b
.. http:post:: /Bresource_b
:synopsis: null
resource b
:status 404:
error
error.
''').lstrip()

def test_exclude_option(self):
spec = collections.defaultdict(collections.OrderedDict)
spec['paths']['/resource_a'] = {
spec['openapi'] ='3.0.0'
spec['paths']['/Aresource_a'] = {
'get': {
'description': 'resource a',
'responses': {
'200': {'description': 'ok'},
}
}
}
spec['paths']['/resource_b'] = {
spec['paths']['/Bresource_b'] = {
'post': {
'description': 'resource b',
'responses': {
Expand All @@ -372,16 +376,17 @@ def test_exclude_option(self):

renderer = renderers.HttpdomainOldRenderer(None, {'exclude': [
'/.*_a',
'/A.*',
]})
text = '\n'.join(renderer.render_restructuredtext_markup(spec))
assert text == textwrap.dedent('''
.. http:post:: /resource_b
.. http:post:: /Bresource_b
:synopsis: null
resource b
:status 404:
error
error.
''').lstrip()

def test_method_option(self):
Expand Down

0 comments on commit 247e19e

Please sign in to comment.