Skip to content

Commit

Permalink
Merge pull request #109 from guokr/57_circular_references
Browse files Browse the repository at this point in the history
#57 fix issues
  • Loading branch information
gusibi authored Mar 29, 2018
2 parents 683bd27 + e541b80 commit c7a9470
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
22 changes: 21 additions & 1 deletion swagger_py_codegen/command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import
from os import path
import codecs
try:
import simplejson as json
Expand All @@ -7,6 +8,7 @@
from os import makedirs
from os.path import join, exists, dirname

import six
import yaml
import click

Expand All @@ -19,7 +21,13 @@
from .base import Template


def get_ref_filepath(filename, ref_file):
ref_file = path.normpath(path.join(path.dirname(filename), ref_file))
return ref_file


def spec_load(filename):
spec_data = {}
if filename.endswith('.json'):
loader = json.load
elif filename.endswith('.yml') or filename.endswith('.yaml'):
Expand All @@ -33,7 +41,19 @@ def spec_load(filename):
else:
loader = yaml.load
with codecs.open(filename, 'r', 'utf-8') as f:
return loader(f)
data = loader(f)
spec_data.update(data)
for field, values in six.iteritems(data):
if field not in ['definitions', 'parameters', 'paths']:
continue
if not isinstance(values, dict):
continue
for _field, value in six.iteritems(values):
if _field == '$ref' and value.endswith('.yml'):
_filepath = get_ref_filepath(filename, value)
field_data = spec_load(_filepath)
spec_data[field] = field_data
return spec_data


def write(dist, content):
Expand Down
18 changes: 18 additions & 0 deletions tests/testdef.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Account:
type: object
required:
- emailAddress
properties:
emailAddress:
type: string
installationInformation:
$ref: '#/definitions/InstallationInformation'


InstallationInformation:
description: A collection of information that describes an instance of the application and what device it is running on.
properties:
OSType:
type: string
description:
Android or Apple or other
18 changes: 18 additions & 0 deletions tests/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
swagger: '2.0'
info:
version: "0.1.1"
title: test Application

paths:
/account:
get:
responses:
'200':
description: the status of an account
schema:
properties:
account:
$ref: '#/definitions/Account'

definitions:
$ref: 'testdef.yml'

0 comments on commit c7a9470

Please sign in to comment.