Skip to content

Commit

Permalink
adding ways to add component info for version mismatch (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vibaswan authored Aug 26, 2024
1 parent b47ea83 commit 5c3e030
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
27 changes: 23 additions & 4 deletions openapiart/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,9 @@ def _write_api_class(self, methods, factories):
self._write(2, "if self._version_check is None:")
self._write(3, "self._version_check = False")
self._write(2, "self._version_check_err = None")
self._write(2, "self._client_name = None")
self._write(2, "self._client_ver = None")
self._write(2, "self._server_name = None")
else:
self._write(2, "pass")

Expand Down Expand Up @@ -823,6 +826,16 @@ def _generate_version_api_methods(self):
if not self._generate_version_api:
return

# adding functionality for adding component info
self._write(
1,
"def set_component_info(self, client_name, client_version, server_name):",
)
self._write(2, "self._client_name = client_name")
self._write(2, "self._client_app_ver = client_version")
self._write(2, "self._server_name = server_name")
self._write()

self._write(
indent=1,
line="""def _check_client_server_version_compatibility(
Expand Down Expand Up @@ -879,10 +892,16 @@ def _do_version_check(self):
local.api_spec_version, remote.api_spec_version, "API spec"
)
except Exception as e:
msg = "client SDK version '{}' is not compatible with server SDK version '{}'".format(
local.sdk_version, remote.sdk_version
)
return Exception("{}: {}".format(msg, str(e))), None
if self._client_name is not None:
msg = "{} {} is not compatible with {} {}".format(
self._client_name, self._client_app_ver, self._server_name, remote.app_version
)
return Exception(msg), None
else:
msg = "client SDK version '{}' is not compatible with server SDK version '{}'".format(
local.sdk_version, remote.sdk_version
)
return Exception("{}: {}".format(msg, str(e))), None
return None, None
Expand Down
21 changes: 19 additions & 2 deletions openapiart/openapiartgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ def _build_api_interface(self):
localVersion Version
remoteVersion Version
checkError error
clientName string
clientAppVer string
serverName string
}}
type {internal_struct_name} struct {{
apiSt
Expand Down Expand Up @@ -1013,6 +1016,8 @@ def _get_version_api_interface_method_signatures(self):
"// SetVersionCompatibilityCheck allows enabling or disabling automatic version",
"// compatibility check between client and server API spec version upon API call",
"SetVersionCompatibilityCheck(bool)",
"// ability to set component names and specific version for version check error msgs",
"SetComponentInformation(string, string, string)",
"// CheckVersionCompatibility compares API spec version for local client and remote server,",
"// and returns an error if they are not compatible according to Semantic Versioning 2.0.0",
"CheckVersionCompatibility() error",
Expand Down Expand Up @@ -1045,6 +1050,12 @@ def _get_version_api_interface_method_impl(self, struct_name):
api.versionMeta.checkVersion = v
}}
func (api *{0}) SetComponentInformation(clientName string, clientVer string, serverName string) {{
api.versionMeta.clientName = clientName
api.versionMeta.clientAppVer = clientVer
api.versionMeta.serverName = serverName
}}
func (api *{0}) checkLocalRemoteVersionCompatibility() (error, error) {{
localVer := api.GetLocalVersion()
remoteVer, err := api.GetRemoteVersion()
Expand All @@ -1053,10 +1064,16 @@ def _get_version_api_interface_method_impl(self, struct_name):
}}
err = checkClientServerVersionCompatibility(localVer.ApiSpecVersion(), remoteVer.ApiSpecVersion(), "API spec")
if err != nil {{
return fmt.Errorf(
if api.versionMeta.clientName != "" {{
return fmt.Errorf(
"%s %s is not compatible with %s %s", api.versionMeta.clientName, api.versionMeta.clientAppVer,
api.versionMeta.serverName, remoteVer.AppVersion(),), nil
}} else {{
return fmt.Errorf(
"client SDK version '%s' is not compatible with server SDK version '%s': %v",
localVer.SdkVersion(), remoteVer.SdkVersion(), err,
), nil
), nil
}}
}}
return nil, nil
Expand Down
1 change: 1 addition & 0 deletions openapiart/tests/grpcserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def GetConfig(self, request, context):
def GetVersion(self, request, context):
self._log("Executing GetVersion")
v = op.api().get_local_version()
v.app_version = "1.2.3"
response_200 = {"version": v.serialize(v.DICT)}
res_obj = json_format.Parse(
json.dumps(response_200), pb2.GetVersionResponse()
Expand Down
24 changes: 24 additions & 0 deletions openapiart/tests/test_grpc_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,29 @@ def test_grpc_accept_yaml(grpc_api):
grpc_api.set_config(s_obj)


def test_version_mismatch_error(utils, grpc_api):
with open(utils.get_test_config_path("config.json")) as f:
payload = json.load(f)
try:
grpc_api.get_local_version().api_spec_version = "2.0.1"
grpc_api._version_check = True
grpc_api._version_check_err = None
grpc_api.set_component_info(
"keng-controller", "1.8.0", "protocol-engine"
)
grpc_api.set_config(payload)
raise Exception("expected version error")
except Exception as e:
assert (
str(e)
== "keng-controller 1.8.0 is not compatible with protocol-engine 1.2.3"
)
finally:
grpc_api.get_local_version().api_spec_version = "0.1.0"
grpc_api._version_check = False
#
# print(grpc_api.__dict__)


if __name__ == "__main__":
pytest.main(["-v", "-s", __file__])
2 changes: 1 addition & 1 deletion pkg/mock_grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *GrpcServer) GetConfig(ctx context.Context, req *empty.Empty) (*sanity.G
}

func (s *GrpcServer) GetVersion(ctx context.Context, req *empty.Empty) (*sanity.GetVersionResponse, error) {
ver, _ := openapiart.NewApi().GetLocalVersion().Marshal().ToProto()
ver, _ := openapiart.NewApi().GetLocalVersion().SetAppVersion("1.2.3").Marshal().ToProto()
resp := &sanity.GetVersionResponse{
Version: ver,
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,16 @@ func TestGrpcErrorStructUpdate(t *testing.T) {
assert.Equal(t, errSt.Kind(), openapiart.ErrorKind.VALIDATION)
assert.Equal(t, errSt.Errors()[0], "invalid1")
}

func TestVersionMismatchMsgWithComponentInfo(t *testing.T) {
api := apis[0]
config1 := NewFullyPopulatedPrefixConfig(api)
config1.SetResponse(openapiart.PrefixConfigResponse.STATUS_200)
api.SetVersionCompatibilityCheck(true)
api.GetLocalVersion().SetApiSpecVersion("2.0.1")
api.SetComponentInformation("keng-controller", "1.8.0", "protocol-engine")
_, err := api.SetConfig(config1)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "keng-controller 1.8.0 is not compatible with protocol-engine 1.2.3")
api.SetVersionCompatibilityCheck(false)
}

0 comments on commit 5c3e030

Please sign in to comment.