Skip to content
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

fix: error not thrown when import spec with >3MB file #44

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions src/apic-extension/azext_apic_extension/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import yaml
import requests
from knack.log import get_logger
from knack.util import CLIError
import chardet
from azure.cli.core.aaz._arg import AAZStrArg
from .command_patches import ImportAPIDefinitionExtension
Expand All @@ -32,11 +33,13 @@ def pre_operations(self):

# Check the size of 'value' if format is inline and raise error if value is greater than 3 mb
if args.format == 'inline':
value_size_bytes = sys.getsizeof(args.value)
value_size_bytes = sys.getsizeof(str(args.value))
value_size_mb = value_size_bytes / (1024 * 1024) # Convert bytes to megabytes
if value_size_mb > 3:
logger.error('The size of "value" is greater than 3 MB. '
'Please use --format "url" to import the specification from a URL for size greater than 3 mb.')
raise CLIError(
'The size of "value" is greater than 3 MB. '
'Please use --format "link" to import the specification from a URL for size greater than 3 mb.'
)


class ExportSpecificationExtension(ExportAPIDefinitionExtension):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import os

from knack.util import CLIError
from azure.cli.testsdk import ScenarioTest, ResourceGroupPreparer
from .utils import ApicServicePreparer, ApicApiPreparer, ApicVersionPreparer, ApicDefinitionPreparer

Expand Down Expand Up @@ -185,4 +186,24 @@ def test_definition_import_from_file(self):

assert exported_content == imported_content, "The exported content is not the same as the imported content."
finally:
os.remove(exported_file_path)
os.remove(exported_file_path)

def test_definition_import_large_value(self):
self.kwargs.update({
'specification': '{"name":"openapi","version":"3.0.0"}',
'file_name': "test_definition_import_large_value.txt",
'rg': "mock_resource_group",
's': 'mock-service-name',
'api': 'mock-api-id',
'v': 'mock-version-id',
'd': 'mock-definition-id'
})

try:
with open(self.kwargs['file_name'], 'w') as file:
file.write('a' * 4 * 1024 * 1024) # generate a 4MB file

with self.assertRaisesRegexp(CLIError, 'The size of "value" is greater than 3 MB. Please use --format "link" to import the specification from a URL for size greater than 3 mb.') as cm:
self.cmd('az apic api definition import-specification -g {rg} -s {s} --api-id {api} --version-id {v} --definition-id {d} --format "inline" --specification \'{specification}\' --value "@{file_name}"')
finally:
os.remove(self.kwargs['file_name'])
Loading