Skip to content

Add endpoints object #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Moved to returning a map rather than a list. this lets us build the c…
…orrect endpoint data structure.
  • Loading branch information
mooreds committed Dec 15, 2020
commit c40b6bfd2f003c235f785df0fc5181cdf65f0541
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,35 @@ class ClientLibraryPlugin extends BaseGroovyPlugin {
root['apis'] << json

// gather up json by endpoint/http method so that we can build openapi file
def endpoint = json.uri
if (!root['endpoints'][endpoint]) {
root['endpoints'][endpoint] = [:]
}
// most endpoints only have one option, but some have two, with and without a param
def endpoints = buildOpenapiUri(json.uri, json.params)
def normalEndpoint = endpoints["normal"]
def endpointWithOptionalParam = endpoints["withOptionalParam"]
def http_method = json.method
root['endpoints'][endpoint][http_method] = json

if (!root['endpoints'][normalEndpoint]) {
root['endpoints'][normalEndpoint] = [:]
}
def onlyNormal = endpointWithOptionalParam == null
if (onlyNormal) {
// handle normal case
root['endpoints'][normalEndpoint][http_method] = json

if (json.params) {
def optionalUrlSegment = json.params.find { it.required != null && it.required == false && it.type == "urlSegment" }
if (optionalUrlSegment != null ) {
// if it is a url segment but not required, we need to add it here
// this lets openapi create two endpoints, one with the segment and the other without
if (!root['endpoints_w_optional_path_params'][endpoint]) {
root['endpoints_w_optional_path_params'][endpoint] = [:]
}
def modifiable_json = jsonSlurper.parseText(JsonOutput.toJson(json))
// remove it from a copy
modifiable_json.params = modifiable_json.params - optionalUrlSegment
root['endpoints_w_optional_path_params'][endpoint][http_method] = modifiable_json
} else {
// handle case with param
if (!root['endpoints'][endpointWithOptionalParam]) {
root['endpoints'][endpointWithOptionalParam] = [:]
}
root['endpoints'][endpointWithOptionalParam][http_method] = json

// handle with optional params

def optionalUrlSegment = json.params.find { it.required != null && it.required == false && it.type == "urlSegment" }

// remove the optional param from a copy. the normal endpoint doesn't get the optional segment param
def modifiable_json = jsonSlurper.parseText(JsonOutput.toJson(json))
modifiable_json.params = modifiable_json.params - optionalUrlSegment
root['endpoints'][normalEndpoint][http_method] = modifiable_json
}
}

Expand All @@ -124,22 +133,22 @@ class ClientLibraryPlugin extends BaseGroovyPlugin {
}

//look for urlsegments
List buildOpenapiUri(uri, params) {
Map buildOpenapiUri(uri, params) {
if (!params || params.size == 0) {
return [uri]
return ["normal": uri]
}
def urlSegments = params.findAll { it.type == "urlSegment" }
if (!urlSegments || urlSegments.size == 0) {
return [uri]
return ["normal": uri]
}
def optionalUrlSegment = urlSegments.find { it.required != null && it.required == false }
if (optionalUrlSegment == null) {
// only the one url segment, it's required TODO might not be invarant
return [uri+"/{"+urlSegments[0].name+"}"]
return ["normal": uri+"/{"+urlSegments[0].name+"}"]
}
def toReturn = []
toReturn << uri+"/{"+optionalUrlSegment.name+"}"
toReturn << uri
def toReturn = [:]
toReturn["withOptionalParam"] = uri+"/{"+optionalUrlSegment.name+"}"
toReturn["normal"] = uri

return toReturn
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,39 @@ class ClientLibraryPluginTest {
ClientLibraryPlugin plugin = new ClientLibraryPlugin(project, new RuntimeConfiguration(), output)
def uri = "abc"
def params = []
assert plugin.buildOpenapiUri(uri,params) == ["abc"]
assert plugin.buildOpenapiUri(uri,params) == ["normal":"abc"]
}

@Test
void buildOpenapiParamsWithParamsNoSegment() {
ClientLibraryPlugin plugin = new ClientLibraryPlugin(project, new RuntimeConfiguration(), output)
def uri = "abc"
def params = [["type": "urlParameter", "name":"foo"]]
assert plugin.buildOpenapiUri(uri,params) == ["abc"]
assert plugin.buildOpenapiUri(uri,params) == ["normal":"abc"]
}

@Test
void buildOpenapiParamsWithParamsSegmentRequired() {
ClientLibraryPlugin plugin = new ClientLibraryPlugin(project, new RuntimeConfiguration(), output)
def uri = "abc"
def params = [["type": "urlSegment", "name":"foo","required":true]]
assert plugin.buildOpenapiUri(uri,params) == ["abc/{foo}"]
assert plugin.buildOpenapiUri(uri,params) == ["normal":"abc/{foo}"]
}

@Test
void buildOpenapiParamsWithParamsSegmentOptional() {
ClientLibraryPlugin plugin = new ClientLibraryPlugin(project, new RuntimeConfiguration(), output)
def uri = "abc"
def params = [["type": "urlSegment", "name":"foo","required":false]]
assert plugin.buildOpenapiUri(uri,params) == ["abc/{foo}", "abc"]
assert plugin.buildOpenapiUri(uri,params) == ["withOptionalParam": "abc/{foo}", "normal":"abc"]
}

@Test
void buildOpenapiParamsWithMultParamsSegmentOptional() {
ClientLibraryPlugin plugin = new ClientLibraryPlugin(project, new RuntimeConfiguration(), output)
def uri = "abc"
def params = [["type": "urlSegment", "name":"foo","required":false], ["type": "urlParameter", "name":"bar"]]
assert plugin.buildOpenapiUri(uri,params) == ["abc/{foo}", "abc"]
assert plugin.buildOpenapiUri(uri,params) == ["withOptionalParam": "abc/{foo}", "normal":"abc"]
}

@Test
Expand Down