Skip to content

Commit 92b767f

Browse files
committed
Fix issue with double pound signs at root
Also added tests for more complex/nested scenarios.
1 parent d75ad8a commit 92b767f

File tree

10 files changed

+123
-1
lines changed

10 files changed

+123
-1
lines changed

models/OpenAPI/Parser.cfc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,14 @@ component name="OpenAPIParser" accessors="true" {
133133
} else if( isStruct( DocItem ) ) {
134134

135135
//handle top-level values, if they exist
136-
if( structKeyExists( DocItem, "$ref" ) ) return fetchDocumentReference( DocItem[ "$ref" ] );
136+
if( structKeyExists( DocItem, "$ref" ) ) {
137+
// special handling for double pound signs
138+
if( left( DocItem[ "$ref" ], 2 ) == chr( 35 ) & chr( 35 ) ) {
139+
DocItem[ "$ref" ] = right( DocItem[ "$ref" ], ( len( DocItem[ "$ref" ] ) - 1 ) );
140+
return DocItem;
141+
}
142+
return fetchDocumentReference( DocItem[ "$ref" ] );
143+
}
137144

138145
for( var key in DocItem){
139146

@@ -273,8 +280,14 @@ component name="OpenAPIParser" accessors="true" {
273280
**/
274281
private function fetchDocumentReference( required string $ref ){
275282

283+
/* if ( $ref == chr( 35 ) & chr( 35 ) & "/components/requestBodies/PostBody" ) {
284+
writeDump( var=$ref, output="console" );
285+
} */
286+
287+
276288
// double pound ## means we want to preserve the swagger $ref pointer (just remove the extra #)
277289
if( left( $ref, 2 ) == chr( 35 ) & chr( 35 ) ){
290+
writeDump( var=right( $ref, ( len( $ref ) - 1 ) ), output="console" );
278291
return { "$ref": right( $ref, ( len( $ref ) - 1 ) ) };
279292
//resolve internal refrences before looking for externals
280293
} else if( left( $ref, 1 ) == chr( 35 )){
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"summary": "Response",
3+
"description": "A generic response",
4+
"value": {
5+
"error": false,
6+
"data": {},
7+
"messages": []
8+
}
9+
}

test-harness/tests/resources/coldboxRest/coldboxRest.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@
5252
"Pagination": { "$ref": "_schemas/pagination.json" },
5353
"User": { "$ref": "paths/users/_schemas/user.json" },
5454
"Post": { "$ref": "paths/posts/_schemas/post.json" },
55+
"PostBody": { "$ref": "paths/posts/_schemas/postBody.json" },
5556
"Media": { "$ref": "paths/media/_schemas/media.json" },
5657
"BookMedia": { "$ref": "paths/media/_schemas/bookMedia.json" },
5758
"MusicMedia": { "$ref": "paths/media/_schemas/musicMedia.json" }
59+
},
60+
"requestBodies": {
61+
"PostBody": { "$ref": "paths/posts/_bodies/postBody.json" }
5862
}
5963
},
6064
"paths": {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"description": "Post to Create",
3+
"required": true,
4+
"content": {
5+
"application/json": {
6+
"schema": { "$ref": "##/components/schemas/PostBody" },
7+
"example": {
8+
"id": "1",
9+
"title": "Washingtons Newburgh Address",
10+
"body": "While I give you these assurances...",
11+
"authorId": 1
12+
}
13+
}
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"id": {
5+
"type": "string"
6+
},
7+
"title": {
8+
"type": "string"
9+
},
10+
"body": {
11+
"type": "string"
12+
},
13+
"authorId": {
14+
"type": "integer"
15+
}
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"data": {
3+
"id": "1",
4+
"title": "Washingtons Newburgh Address",
5+
"body": "While I give you these assurances, and pledge myself in the most unequivocal manner, to exert whatever ability I am possessed of, in your favor—let me entreat you, Gentlemen, on your part, not to take any measures, which, viewed in the calm light of reason, will lessen the dignity, & sully the glory you have hitherto maintained—let me request you to rely on the plighted faith of your Country, and place a full confidence in the purity of the intentions of Congress",
6+
"author": {
7+
"id": "1",
8+
"firstName": "George",
9+
"lastName": "Washington",
10+
"emailAddress": "potus1@gmail.com"
11+
}
12+
},
13+
"messages": [
14+
"New Post Created Successfully"
15+
],
16+
"error": false
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$ref": "##/components/requestBodies/PostBody"
3+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"200": {
3+
"description": "Success",
4+
"content": {
5+
"application/json": {
6+
"schema": {
7+
"$extend": [
8+
{
9+
"$ref": "../../../_schemas/response.json"
10+
},
11+
{
12+
"properties": {
13+
"data": {
14+
"$ref": "##/components/schemas/Post"
15+
}
16+
}
17+
}
18+
19+
]
20+
},
21+
"example": {
22+
"$ref": "example.200.json"
23+
}
24+
}
25+
}
26+
}
27+
}

test-harness/tests/resources/coldboxRest/paths/posts/posts.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
"responses": {
1111
"$ref": "index/responses.json"
1212
}
13+
},
14+
"post": {
15+
"summary": "Create a post",
16+
"operationId": "posts.create",
17+
"tags": ["posts"],
18+
"requestBody": {
19+
"$ref": "create/requestBody.json"
20+
},
21+
"responses": {
22+
"$ref": "create/responses.json"
23+
}
1324
}
1425
},
1526
"/posts/{id}": {

test-harness/tests/specs/ColdboxSpec.cfc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ component extends="BaseOpenAPISpec"{
4545
it( "Can parse the apiDoc", function(){
4646
var apiDoc = parseApiDoc();
4747

48+
debug( serializeJson( apiDoc ) );
49+
4850
// it can have multiple servers
4951
expect( apiDoc ).toHaveKey( "servers" );
5052
expect( apiDoc.servers ).toBeArray();
@@ -95,6 +97,10 @@ component extends="BaseOpenAPISpec"{
9597
expect( apiDoc.paths[ "/users" ].get.responses[ "200" ].content[ "application/json" ].schema.properties.data.properties.results.items.properties ).toHaveKey( item );
9698
}
9799

100+
// expect the requestBody reference to have one pound sign
101+
expect( apiDoc.paths[ "/posts" ].post.requestBody ).toHaveKey( "$ref" );
102+
expect( left( apiDoc.paths[ "/posts" ].post.requestBody[ '$ref' ], 2 ) ).toBe( "#chr( 35 )#/" );
103+
98104
});
99105

100106
});

0 commit comments

Comments
 (0)