Skip to content

Commit

Permalink
update swagger doc generation to recursively walk mounted routers
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyhd committed Jun 19, 2018
1 parent 757a7f5 commit 6266dc4
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions R/plumber.R
Expand Up @@ -532,29 +532,8 @@ plumber <- R6Class(
private$addFilterInternal(filter)
},
swaggerFile = function(){ #FIXME: test
endpoints <- self$endpoints

if (length(self$mounts) > 0){
for (path in names(self$mounts)){

mount <- self$mounts[[path]]
mount_endpoints <- mount$endpoints
path <- sub("[/]$", "", path)

if (length(mount_endpoints) > 0) {
mount_endpoints <- lapply(mount_endpoints, function(i){
i <- lapply(i, function(j){
subpath <- sub("^[/]", "", j$path)
j$path <- paste(path, subpath, sep="/")
j
})
i
})

endpoints <- c(endpoints, mount_endpoints)
}
}
}
endpoints <- private$swaggerFileWalkMountsInternal(self)
endpoints <- prepareSwaggerEndpoints(endpoints)

# Extend the previously parsed settings with the endpoints
Expand Down Expand Up @@ -696,6 +675,30 @@ plumber <- R6Class(
}

private$ends[[preempt]] <- c(private$ends[[preempt]], ep)
},
swaggerFileWalkMountsInternal = function(router, parentPath = ""){

parentPath <- sub("[/]$", "", parentPath)
endpoints <- lapply(router$endpoints, function(endpoint){

endpointEntries <- lapply(endpoint, function(endpointEntry){
endpointPath <- sub("^[/]", "", endpointEntry$path)
endpointPath <- paste(parentPath, endpointPath, sep="/")
endpointEntry$path <- endpointPath
endpointEntry
})

endpoint
})

mounts <- router$mounts
mountedEndpoints <- unlist(lapply(names(mounts), function(mountPath){
mountedSubrouter <- router$mounts[[mountPath]]
mountPath <- sub("^[/]", "", mountPath)
mountedEndpoints <- private$swaggerFileWalkMountsInternal(mountedSubrouter, paste(parentPath, mountPath, sep="/"))
}))

c(endpoints, list(mountedEndpoints))
}
)
)
Expand Down

0 comments on commit 6266dc4

Please sign in to comment.