Skip to content
12 changes: 12 additions & 0 deletions packages/apidash_core/lib/utils/http_request_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:apidash_core/consts.dart';
import 'package:seed/seed.dart';
import '../models/models.dart';
import 'graphql_utils.dart';
import 'package:json5/json5.dart' as json5;

Map<String, String>? rowsToMap(
List<NameValueModel>? kvRows, {
Expand Down Expand Up @@ -100,3 +101,14 @@ String? getRequestBody(APIType type, HttpRequestModel httpRequestModel) {
APIType.graphql => getGraphQLBody(httpRequestModel),
};
}

// TODO: Expose this function to remove JSON comments
String? removeJsonComments(String? json) {
try {
if (json == null) return null;
var parsed = json5.json5Decode(json);
return kJsonEncoder.convert(parsed);
} catch (e) {
return json;
}
}
1 change: 1 addition & 0 deletions packages/apidash_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies:
http_parser: ^4.1.2
insomnia_collection:
path: ../insomnia_collection
json5: ^0.8.2
postman:
path: ../postman
seed: ^0.0.3
Expand Down
74 changes: 74 additions & 0 deletions packages/apidash_core/test/utils/http_request_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'package:apidash_core/utils/http_request_utils.dart';
import 'package:test/test.dart';

void main() {
group('Testing RemoveJsonComments', () {
test('Removes single-line comments', () {
String input = '''
{
// This is a single-line comment
"key": "value"
}
''';

String expected = '''{
"key": "value"
}''';
expect(removeJsonComments(input), expected);
});

test('Removes multi-line comments', () {
String input = '''
{
/*
This is a multi-line comment
*/
"key": "value"
}
''';

String expected = '''{
"key": "value"
}''';
expect(removeJsonComments(input), expected);
});

test('Handles valid JSON without comments', () {
String input = '{"key":"value"}';
String expected = '''{
"key": "value"
}''';
expect(removeJsonComments(input), expected);
});

test('Returns original string if invalid JSON', () {
String input = '{key: value}';
String expected = '{key: value}';
expect(removeJsonComments(input), expected);
});

test('Removes trailing commas', () {
String input = '''
{
"key1": "value1",
"key2": "value2", // trailing comma
}
''';

String expected = '''{
"key1": "value1",
"key2": "value2"
}''';
expect(removeJsonComments(input), expected);
});

test('Test blank json', () {
String input = '''
{}
''';

String expected = '{}';
expect(removeJsonComments(input), expected);
});
});
}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.7"
json5:
dependency: transitive
description:
name: json5
sha256: b67d6e06c9e225c8277d3c43f796677af7975a2a2b0669ff12ba38ff466a31f4
url: "https://pub.dev"
source: hosted
version: "0.8.2"
json_annotation:
dependency: "direct main"
description:
Expand Down