From b73332c4468e27807c0a11d9c3104c08471dd79d Mon Sep 17 00:00:00 2001 From: Daniel Poon <17039704+danielpoonwj@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:46:19 +0800 Subject: [PATCH] [openapi2kong] Fix server port parsing (#105) --- .../04a-servers-upstream.expected.json | 38 +++++++++++++++++++ .../oas3_testfiles/04a-servers-upstream.yaml | 17 +++++++++ openapi2kong/service.go | 6 ++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 openapi2kong/oas3_testfiles/04a-servers-upstream.expected.json create mode 100644 openapi2kong/oas3_testfiles/04a-servers-upstream.yaml diff --git a/openapi2kong/oas3_testfiles/04a-servers-upstream.expected.json b/openapi2kong/oas3_testfiles/04a-servers-upstream.expected.json new file mode 100644 index 0000000..9e1ad73 --- /dev/null +++ b/openapi2kong/oas3_testfiles/04a-servers-upstream.expected.json @@ -0,0 +1,38 @@ +{ + "_format_version": "3.0", + "services": [ + { + "host": "server2.com", + "id": "0907c4ab-d9e4-5d21-813b-c57a97eeaad9", + "name": "simple-api-overview", + "path": "/", + "plugins": [], + "port": 65000, + "protocol": "https", + "routes": [ + { + "id": "eee036de-517e-59cf-a2e0-17b3adfa31b5", + "methods": [ + "GET" + ], + "name": "simple-api-overview_opsid", + "paths": [ + "~/$" + ], + "plugins": [], + "regex_priority": 200, + "strip_path": false, + "tags": [ + "OAS3_import", + "OAS3file_04a-servers-upstream.yaml" + ] + } + ], + "tags": [ + "OAS3_import", + "OAS3file_04a-servers-upstream.yaml" + ] + } + ], + "upstreams": [] +} diff --git a/openapi2kong/oas3_testfiles/04a-servers-upstream.yaml b/openapi2kong/oas3_testfiles/04a-servers-upstream.yaml new file mode 100644 index 0000000..983094d --- /dev/null +++ b/openapi2kong/oas3_testfiles/04a-servers-upstream.yaml @@ -0,0 +1,17 @@ +# properly parses ports, see issue #104 +# https://github.com/Kong/go-apiops/issues/104 +openapi: '3.0.0' +info: + title: Simple API overview + version: v2 +servers: + - url: https://server2.com:65000/ +paths: + /: + get: + operationId: OpsId + summary: List API versions + responses: + '200': + description: |- + 200 response diff --git a/openapi2kong/service.go b/openapi2kong/service.go index 9b33e99..7ac8f72 100644 --- a/openapi2kong/service.go +++ b/openapi2kong/service.go @@ -230,7 +230,11 @@ func CreateKongService( if service["port"] == nil { if targets[0].Port() != "" { // port is provided, so parse it - service["port"], _ = strconv.ParseInt(targets[0].Port(), 10, 16) + parsedPort, err := strconv.ParseUint(targets[0].Port(), 10, 16) + if err != nil { + return nil, nil, err + } + service["port"] = parsedPort } else { // no port provided, so set it based on scheme, where https/443 is the default if scheme != httpScheme {