add support for path-level parameters

This commit is contained in:
Marco Capitani 2019-03-08 10:19:09 +01:00
parent 395f68b234
commit f232f7419b
4 changed files with 52 additions and 2 deletions

View file

@ -3,6 +3,7 @@ from six import iteritems
from openapi_core.compat import lru_cache
from openapi_core.schema.operations.generators import OperationsGenerator
from openapi_core.schema.parameters.generators import ParametersGenerator
from openapi_core.schema.paths.models import Path
@ -16,9 +17,17 @@ class PathsGenerator(object):
paths_deref = self.dereferencer.dereference(paths)
for path_name, path in iteritems(paths_deref):
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
@lru_cache()
def operations_generator(self):
return OperationsGenerator(self.dereferencer, self.schemas_registry)
@property
@lru_cache()
def parameters_generator(self):
return ParametersGenerator(self.dereferencer, self.schemas_registry)

View file

@ -4,9 +4,10 @@
class Path(object):
"""Represents an OpenAPI Path."""
def __init__(self, name, operations):
def __init__(self, name, operations, parameters=None):
self.name = name
self.operations = dict(operations)
self.parameters = dict(parameters) if parameters else {}
def __getitem__(self, http_method):
return self.operations[http_method]

View 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.

View 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