Skip to content

add support for path-level parameters #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion openapi_core/schema/paths/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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


Expand All @@ -16,9 +17,17 @@ def generate(self, paths):
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)
3 changes: 2 additions & 1 deletion openapi_core/schema/paths/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
17 changes: 17 additions & 0 deletions tests/integration/data/v3.0/path_param.yaml
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions tests/integration/test_path_params.py
Original file line number Diff line number Diff line change
@@ -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