Skip to content

Commit 5973a19

Browse files
committed
added model spec
1 parent 40428a3 commit 5973a19

File tree

6 files changed

+194
-7
lines changed

6 files changed

+194
-7
lines changed

Cakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ task 'dev', "Open source files, run spec in browser, and watch for changes", ->
5151
exec "open http-spec.html"
5252
exec "open operation-spec.html"
5353
exec "open request-spec.html"
54+
exec "open model-spec.html"
5455
coffee = spawn 'coffee', ['-cw', '-o', 'lib', 'src']
5556
coffee.stdout.on 'data', (data) -> console.log data.toString().trim()

lib/swagger-model-spec.js

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/swagger.js

Lines changed: 26 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

model-spec.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE HTML>
2+
<head>
3+
<title>swagger.js specs</title>
4+
5+
<link rel="stylesheet" type="text/css" href="lib/jasmine-1.3.1/jasmine.css">
6+
7+
<script type="text/javascript" src="lib/shred.bundle.js" /></script>
8+
<script type="text/javascript" src="lib/jquery-1.8.0.min.js"></script>
9+
<script type="text/javascript" src="lib/jasmine-1.3.1/jasmine.js"></script>
10+
<script type="text/javascript" src="lib/jasmine-1.3.1/jasmine-html.js"></script>
11+
12+
<!-- be sure to include source files first -->
13+
<script type="text/javascript" src="lib/swagger.js"></script>
14+
15+
<!-- then spec files -->
16+
<script type="text/javascript" src="lib/swagger-model-spec.js"></script>
17+
</head>
18+
<body>
19+
20+
<script type="text/javascript">
21+
$(function() {
22+
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
23+
jasmine.getEnv().execute();
24+
});
25+
</script>
26+
27+
</body>
28+
</html>

src/swagger-model-spec.coffee

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
window.api_key = 'special-key'
2+
3+
describe 'Swagger models version 1.2 spec', ->
4+
5+
beforeEach ->
6+
success = ->
7+
""
8+
window.authorizations.add "key", new ApiKeyAuthorization("api_key", "special-key", "header")
9+
window.swagger = new SwaggerApi({url: 'http://localhost:8002/api/api-docs', success: success})
10+
waitsFor ->
11+
swagger.ready?
12+
13+
describe "get model operations", ->
14+
15+
beforeEach ->
16+
window.body = null
17+
window.response = null
18+
window.callback = null
19+
window.error = null
20+
window.success_callback = (data) ->
21+
window.response = data
22+
window.error_callback = (data) ->
23+
window.error = data
24+
25+
it "verifies the Pet model", ->
26+
pet = window.swagger.apis.pet.models["Pet"]
27+
expect(pet.name).toBe("Pet")
28+
expect(pet.properties.length).toBe(6)
29+
30+
props = pet.properties
31+
32+
expect(props[0].name).toBe("id")
33+
expect(props[0].dataType).toBe("integer")
34+
35+
expect(props[1].name).toBe("category")
36+
expect(props[1].dataType).toBe("Category")
37+
38+
expect(props[2].name).toBe("name")
39+
expect(props[2].dataType).toBe("string")
40+
41+
expect(props[3].name).toBe("photoUrls")
42+
expect(props[3].dataType).toBe("array")
43+
expect(props[3].refDataType).toBe("string")
44+
45+
expect(props[4].name).toBe("tags")
46+
expect(props[4].dataType).toBe("array")
47+
expect(props[4].refDataType).toBe("Tag")
48+
49+
expect(props[5].name).toBe("status")
50+
expect(props[5].dataType).toBe("string")
51+
52+
it "verifies the Pet sample JSON", ->
53+
pet = window.swagger.apis.pet.models["Pet"]
54+
sample = pet.createJSONSample()
55+
console.log sample
56+
console.log JSON.stringify sample

src/swagger.coffee

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
log = ->
2+
if window.console then console.log.apply console,arguments
3+
14
class SwaggerApi
25

36
# Defaults
@@ -471,11 +474,24 @@ class SwaggerModelProperty
471474
result = @refModel.createJSONSample(modelsToIgnore)
472475
else
473476
if @isCollection
474-
result = @refDataType
477+
result = @toSampleValue @refDataType
475478
else
476-
result = @dataType
479+
result = @toSampleValue @dataType
477480
if @isCollection then [result] else result
478481

482+
toSampleValue: (value) ->
483+
if value is "integer"
484+
result = 0
485+
else if value is "boolean"
486+
result = false
487+
else if value is "double"
488+
result = 0.0
489+
else if value is "string"
490+
result = ""
491+
else
492+
result = value
493+
result
494+
479495
toString: ->
480496
req = if @required then 'propReq' else 'propOpt'
481497

@@ -745,7 +761,7 @@ class SwaggerRequest
745761
# if any form params, content-type must be set
746762
if (param for param in @operation.parameters when param.paramType is "form").length > 0
747763
type = param.type || param.dataType
748-
if (param for param in @operation.parameters when type.toLowerCase() is "file").length > 0
764+
if (param for param in @operation.parameters when typeof type isnt 'undefined' and type.toLowerCase() is "file").length > 0
749765
requestContentType = "multipart/form-data"
750766
else
751767
requestContentType = "application/x-www-form-urlencoded"

0 commit comments

Comments
 (0)