Skip to content

Commit

Permalink
Merge 3c82352 into a4340b7
Browse files Browse the repository at this point in the history
  • Loading branch information
javabrett committed Aug 6, 2018
2 parents a4340b7 + 3c82352 commit a1964a6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
14 changes: 14 additions & 0 deletions examples/example_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ def fromfile_decorated_no_descr(username):
def fromfile_decorated_no_sep(username):
return jsonify({'username': username})

@app.route('/v1/decorated_bom/<username>', endpoint='should_be_v1_only_username_bom')
@swag_from('username_specs_bom.yml')
def fromfile_decorated_bom(username):
return jsonify({'username': username})

@app.route('/v1/decorated_utf16/<username>', endpoint='should_be_v1_only_username_utf16')
@swag_from('username_specs_utf16.yml')
def fromfile_decorated_utf16(username):
return jsonify({'username': username})

@app.route('/v1/decorated_utf32/<username>', endpoint='should_be_v1_only_username_utf32')
@swag_from('username_specs_utf32.yml')
def fromfile_decorated_utf32(username):
return jsonify({'username': username})

# OR

Expand Down
5 changes: 4 additions & 1 deletion examples/username_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ First line is the summary
All following lines until the hyphens is added to description
the format of the first lines until 3 hyphens will be not yaml compliant
but everything below the 3 hyphens should be.
YAML files are Unicode - UTF-16 or UTF-8 (default) encoded.
This line contains a UTF-8-encoded character:
There are also UTF-8-encoded characters in the following spec.
---
tags:
- users
Expand All @@ -13,7 +16,7 @@ parameters:
required: true
responses:
200:
description: A single user item
description: A single user item (um único usuário)
schema:
id: rec_username
properties:
Expand Down
26 changes: 26 additions & 0 deletions examples/username_specs_bom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
This is the summary defined in yaml file
First line is the summary
All following lines until the hyphens is added to description
the format of the first lines until 3 hyphens will be not yaml compliant
but everything below the 3 hyphens should be.
YAML files are Unicode - UTF-16 or UTF-8 (default) encoded.
This line contains a UTF-8-encoded character:
There are also UTF-8-encoded characters in the following spec.
---
tags:
- users
parameters:
- in: path
name: username
type: string
required: true
responses:
200:
description: A single user item (um único usuário)
schema:
id: rec_username
properties:
username:
type: string
description: The name of the user
default: 'steve-harris'
Binary file added examples/username_specs_utf16.yml
Binary file not shown.
Binary file added examples/username_specs_utf32.yml
Binary file not shown.
19 changes: 17 additions & 2 deletions flasgger/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8

import codecs
import copy
import imp
import inspect
Expand Down Expand Up @@ -451,15 +452,17 @@ def load_from_file(swag_path, swag_type='yml', root_path=None):
# TODO: support JSON

try:
with open(swag_path) as yaml_file:
enc = detect_by_bom(swag_path)
with codecs.open(swag_path, encoding=enc) as yaml_file:
return yaml_file.read()
except IOError:
# not in the same dir, add dirname
swag_path = os.path.join(
root_path or os.path.dirname(__file__), swag_path
)
try:
with open(swag_path) as yaml_file:
enc = detect_by_bom(swag_path)
with codecs.open(swag_path, encoding=enc) as yaml_file:
return yaml_file.read()
except IOError: # pragma: no cover
# if package dir
Expand All @@ -477,6 +480,18 @@ def load_from_file(swag_path, swag_type='yml', root_path=None):
return yaml_file.read()


def detect_by_bom(path, default='utf-8'):
with open(path, 'rb') as f:
raw = f.read(4) # will read less if the file is smaller
for enc, boms in \
('utf-8-sig', (codecs.BOM_UTF8,)),\
('utf-16', (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE)),\
('utf-32', (codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE)):
if any(raw.startswith(bom) for bom in boms):
return enc
return default


def parse_docstring(obj, process_doc, endpoint=None, verb=None):
"""
Gets swag data for method/view docstring
Expand Down

0 comments on commit a1964a6

Please sign in to comment.