Skip to content

Commit

Permalink
Merge pull request #3 from moneymeets/feature/fix-reference-resolver
Browse files Browse the repository at this point in the history
Feature/fix reference resolver
  • Loading branch information
catcombo authored Sep 6, 2024
2 parents 2cfaf7d + e1ff1a7 commit 3f295cd
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 1 deletion.
4 changes: 3 additions & 1 deletion spec2sdk/parsers/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def resolve_reference(reference: str) -> dict:
parsed_ref = urlparse(reference)

if parsed_ref.scheme == parsed_ref.netloc == "":
ref_relative_filepath = Path(parsed_ref.path) if parsed_ref.path else relative_filepath
ref_relative_filepath = (
relative_filepath.parent / Path(parsed_ref.path) if parsed_ref.path else relative_filepath
)
ref_schema_fragment = reduce(
lambda acc, key: acc[key],
filter(None, parsed_ref.fragment.split("/")),
Expand Down
Empty file added tests/parsers/__init__.py
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"components": {
"parameters": {
"CommentId": {
"in": "path",
"name": "commentId",
"required": true,
"schema": {
"type": "integer"
}
}
},
"schemas": {
"Comment": {
"properties": {
"author": {
"properties": {
"email": {
"type": "string"
},
"name": {
"type": "string"
}
},
"type": "object",
"x-schema-name": "User"
},
"text": {
"type": "string"
}
},
"type": "object"
},
"User": {
"properties": {
"email": {
"type": "string"
},
"name": {
"type": "string"
}
},
"type": "object"
}
}
},
"info": {
"title": "Example API",
"version": "1.0"
},
"openapi": "3.0.0",
"paths": {
"/comment{commentId}": {
"get": {
"operationId": "getComment",
"parameters": [
{
"in": "path",
"name": "commentId",
"required": true,
"schema": {
"type": "integer"
},
"x-schema-name": "CommentId"
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"author": {
"properties": {
"email": {
"type": "string"
},
"name": {
"type": "string"
}
},
"type": "object",
"x-schema-name": "User"
},
"text": {
"type": "string"
}
},
"type": "object",
"x-schema-name": "Comment"
}
}
},
"description": "Successful response"
}
}
}
}
}
}
45 changes: 45 additions & 0 deletions tests/parsers/resolver/test_data/local_references/input/api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
openapi: 3.0.0

info:
title: Example API
version: '1.0'

paths:
/comment{commentId}:
get:
operationId: getComment
parameters:
- $ref: '#/components/parameters/CommentId'
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/Comment'

components:
parameters:
CommentId:
name: commentId
in: path
required: true
schema:
type: integer

schemas:
Comment:
type: object
properties:
text:
type: string
author:
$ref: '#/components/schemas/User'

User:
type: object
properties:
name:
type: string
email:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"info": {
"title": "Example API",
"version": "1.0"
},
"openapi": "3.0.0",
"paths": {
"/comment{commentId}": {
"get": {
"operationId": "getComment",
"parameters": [
{
"in": "path",
"name": "commentId",
"required": true,
"schema": {
"type": "integer"
},
"x-schema-name": "CommentId"
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"author": {
"properties": {
"city": {
"maxLength": 50,
"type": "string",
"x-schema-name": "City"
},
"email": {
"type": "string"
},
"name": {
"type": "string"
}
},
"type": "object",
"x-schema-name": "User"
},
"text": {
"type": "string"
}
},
"type": "object",
"x-schema-name": "Comment"
}
}
},
"description": "Successful response"
}
}
}
}
}
}
19 changes: 19 additions & 0 deletions tests/parsers/resolver/test_data/remote_references/input/api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
openapi: 3.0.0

info:
title: Example API
version: '1.0'

paths:
/comment{commentId}:
get:
operationId: getComment
parameters:
- $ref: 'definitions/parameters.yml#/components/parameters/CommentId'
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: 'definitions/schemas.yml#/components/schemas/Comment'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
components:
parameters:
CommentId:
name: commentId
in: path
required: true
schema:
type: integer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
components:
schemas:
Comment:
type: object
properties:
text:
type: string
author:
$ref: 'users/user.yml#/components/schemas/User'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
components:
schemas:
City:
type: string
maxLength: 50
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
components:
schemas:
User:
type: object
properties:
name:
type: string
email:
type: string
city:
$ref: './location.yml#/components/schemas/City'
22 changes: 22 additions & 0 deletions tests/parsers/resolver/test_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json
from pathlib import Path

import pytest

from spec2sdk.parsers.resolver import ResolvingParser

TEST_DATA_DIR = Path(__file__).parent / "test_data"


@pytest.mark.parametrize("test_data_name", ("local_references", "remote_references"))
def test_resolve_references(test_data_name: str):
data_dir = TEST_DATA_DIR / test_data_name
input_spec = data_dir / "input" / "api.yml"
expected_schema = json.loads((data_dir / "expected_output" / "schema.json").read_text())

assert (
ResolvingParser(base_path=input_spec.parent).parse(
relative_filepath=input_spec.relative_to(input_spec.parent),
)
== expected_schema
)

0 comments on commit 3f295cd

Please sign in to comment.