Skip to content

Commit

Permalink
- Add check for when FormField should be visible.
Browse files Browse the repository at this point in the history
- try catch all exceptions and return error message.
- Validation for options after being deserialized.
- Rename in queriesMock.ts.
  • Loading branch information
Konrad-Simso committed Oct 11, 2024
1 parent 1fb11bf commit bbc4c79
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend/src/Designer/Controllers/OptionsController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Json;

Check warning on line 3 in backend/src/Designer/Controllers/OptionsController.cs

View workflow job for this annotation

GitHub Actions / Format check

Using directive is unnecessary.
using System.Threading;
Expand Down Expand Up @@ -149,7 +150,7 @@ public async Task<IActionResult> UploadFile(string org, string repo, [FromForm]
List<Option> newOptionsList = await _optionsService.UploadNewOption(org, repo, developer, fileName, file, cancellationToken);
return Ok(newOptionsList);
}
catch (JsonException e)
catch (Exception e)
{
return BadRequest(e.Message);
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
Expand Down Expand Up @@ -72,6 +73,14 @@ public async Task<List<Option>> UploadNewOption(string org, string repo, string
List<Option> deserializedOptions = JsonSerializer.Deserialize<List<Option>>(payload.OpenReadStream(),
new JsonSerializerOptions { WriteIndented = true, AllowTrailingCommas = true });

foreach (Option option in deserializedOptions)
{
if (string.IsNullOrEmpty(option.Value) || string.IsNullOrEmpty(option.Label))
{
throw new Exception("Uploaded file is missing one of the following attributes for a option: value or label.");
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.

var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, repo, developer);
await altinnAppGitRepository.CreateOrOverwriteOptionsList(optionsListId, deserializedOptions, cancellationToken);

Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/shared/src/mocks/queriesMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export const queriesMock: ServicesContextProps = {
updateAppMetadata: jest.fn().mockImplementation(() => Promise.resolve()),
updateAppConfig: jest.fn().mockImplementation(() => Promise.resolve()),
uploadDataModel: jest.fn().mockImplementation(() => Promise.resolve<JsonSchema>({})),
uploadOption: jest.fn().mockImplementation(() => Promise.resolve()),
uploadOptionList: jest.fn().mockImplementation(() => Promise.resolve()),
upsertTextResources: jest
.fn()
.mockImplementation(() => Promise.resolve<ITextResourcesObjectFormat>({})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function EditCodeList<T extends SelectionComponentType>({
<ErrorMessage>
{error instanceof Error ? error.message : t('ux_editor.modal_properties_error_message')}
</ErrorMessage>
) : (
) : optionListIds?.length !== 0 ? (
<FormField
key={component.id}
id={component.id}
Expand All @@ -102,6 +102,8 @@ export function EditCodeList<T extends SelectionComponentType>({
</StudioNativeSelect>
)}
/>
) : (
<> </>
)}
<StudioFileUploader
className={classes.studioFileUploader}
Expand Down

0 comments on commit bbc4c79

Please sign in to comment.