mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-22 11:08:34 +00:00
add support for path-level parameters
This commit is contained in:
parent
395f68b234
commit
f232f7419b
4 changed files with 52 additions and 2 deletions
|
@ -3,6 +3,7 @@ from six import iteritems
|
||||||
|
|
||||||
from openapi_core.compat import lru_cache
|
from openapi_core.compat import lru_cache
|
||||||
from openapi_core.schema.operations.generators import OperationsGenerator
|
from openapi_core.schema.operations.generators import OperationsGenerator
|
||||||
|
from openapi_core.schema.parameters.generators import ParametersGenerator
|
||||||
from openapi_core.schema.paths.models import Path
|
from openapi_core.schema.paths.models import Path
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,9 +17,17 @@ class PathsGenerator(object):
|
||||||
paths_deref = self.dereferencer.dereference(paths)
|
paths_deref = self.dereferencer.dereference(paths)
|
||||||
for path_name, path in iteritems(paths_deref):
|
for path_name, path in iteritems(paths_deref):
|
||||||
operations = self.operations_generator.generate(path_name, path)
|
operations = self.operations_generator.generate(path_name, path)
|
||||||
yield path_name, Path(path_name, list(operations))
|
parameters = self.parameters_generator.generate_from_list(
|
||||||
|
path.get('parameters', {})
|
||||||
|
)
|
||||||
|
yield path_name, Path(path_name, list(operations), parameters)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
def operations_generator(self):
|
def operations_generator(self):
|
||||||
return OperationsGenerator(self.dereferencer, self.schemas_registry)
|
return OperationsGenerator(self.dereferencer, self.schemas_registry)
|
||||||
|
|
||||||
|
@property
|
||||||
|
@lru_cache()
|
||||||
|
def parameters_generator(self):
|
||||||
|
return ParametersGenerator(self.dereferencer, self.schemas_registry)
|
||||||
|
|
|
@ -4,9 +4,10 @@
|
||||||
class Path(object):
|
class Path(object):
|
||||||
"""Represents an OpenAPI Path."""
|
"""Represents an OpenAPI Path."""
|
||||||
|
|
||||||
def __init__(self, name, operations):
|
def __init__(self, name, operations, parameters=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.operations = dict(operations)
|
self.operations = dict(operations)
|
||||||
|
self.parameters = dict(parameters) if parameters else {}
|
||||||
|
|
||||||
def __getitem__(self, http_method):
|
def __getitem__(self, http_method):
|
||||||
return self.operations[http_method]
|
return self.operations[http_method]
|
||||||
|
|
17
tests/integration/data/v3.0/path_param.yaml
Normal file
17
tests/integration/data/v3.0/path_param.yaml
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
openapi: "3.0.0"
|
||||||
|
info:
|
||||||
|
title: Minimal OpenAPI specification with path parameters
|
||||||
|
version: "0.1"
|
||||||
|
paths:
|
||||||
|
/resource/{resId}:
|
||||||
|
parameters:
|
||||||
|
- name: resId
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
description: the ID of the resource to retrieve
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
get:
|
||||||
|
responses:
|
||||||
|
default:
|
||||||
|
description: Return the resource.
|
23
tests/integration/test_path_params.py
Normal file
23
tests/integration/test_path_params.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from openapi_core.schema.parameters.enums import ParameterLocation
|
||||||
|
from openapi_core.shortcuts import create_spec
|
||||||
|
|
||||||
|
|
||||||
|
class TestMinimal(object):
|
||||||
|
|
||||||
|
spec_paths = [
|
||||||
|
"data/v3.0/path_param.yaml"
|
||||||
|
]
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("spec_path", spec_paths)
|
||||||
|
def test_param_present(self, factory, spec_path):
|
||||||
|
spec_dict = factory.spec_from_file(spec_path)
|
||||||
|
spec = create_spec(spec_dict)
|
||||||
|
|
||||||
|
path = spec['/resource/{resId}']
|
||||||
|
|
||||||
|
assert len(path.parameters) == 1
|
||||||
|
param = path.parameters['resId']
|
||||||
|
assert param.required
|
||||||
|
assert param.location == ParameterLocation.PATH
|
Loading…
Reference in a new issue