Skip to content

Commit 330e1b8

Browse files
authored
Merge pull request #623 from FreeBono/resolve-issue-json
Add support comments in JSON request body #599
2 parents adbae12 + e684f48 commit 330e1b8

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

packages/apidash_core/lib/utils/http_request_utils.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:apidash_core/consts.dart';
22
import 'package:seed/seed.dart';
33
import '../models/models.dart';
44
import 'graphql_utils.dart';
5+
import 'package:json5/json5.dart' as json5;
56

67
Map<String, String>? rowsToMap(
78
List<NameValueModel>? kvRows, {
@@ -100,3 +101,14 @@ String? getRequestBody(APIType type, HttpRequestModel httpRequestModel) {
100101
APIType.graphql => getGraphQLBody(httpRequestModel),
101102
};
102103
}
104+
105+
// TODO: Expose this function to remove JSON comments
106+
String? removeJsonComments(String? json) {
107+
try {
108+
if (json == null) return null;
109+
var parsed = json5.json5Decode(json);
110+
return kJsonEncoder.convert(parsed);
111+
} catch (e) {
112+
return json;
113+
}
114+
}

packages/apidash_core/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies:
1919
http_parser: ^4.1.2
2020
insomnia_collection:
2121
path: ../insomnia_collection
22+
json5: ^0.8.2
2223
postman:
2324
path: ../postman
2425
seed: ^0.0.3
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import 'package:apidash_core/utils/http_request_utils.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group('Testing RemoveJsonComments', () {
6+
test('Removes single-line comments', () {
7+
String input = '''
8+
{
9+
// This is a single-line comment
10+
"key": "value"
11+
}
12+
''';
13+
14+
String expected = '''{
15+
"key": "value"
16+
}''';
17+
expect(removeJsonComments(input), expected);
18+
});
19+
20+
test('Removes multi-line comments', () {
21+
String input = '''
22+
{
23+
/*
24+
This is a multi-line comment
25+
*/
26+
"key": "value"
27+
}
28+
''';
29+
30+
String expected = '''{
31+
"key": "value"
32+
}''';
33+
expect(removeJsonComments(input), expected);
34+
});
35+
36+
test('Handles valid JSON without comments', () {
37+
String input = '{"key":"value"}';
38+
String expected = '''{
39+
"key": "value"
40+
}''';
41+
expect(removeJsonComments(input), expected);
42+
});
43+
44+
test('Returns original string if invalid JSON', () {
45+
String input = '{key: value}';
46+
String expected = '{key: value}';
47+
expect(removeJsonComments(input), expected);
48+
});
49+
50+
test('Removes trailing commas', () {
51+
String input = '''
52+
{
53+
"key1": "value1",
54+
"key2": "value2", // trailing comma
55+
}
56+
''';
57+
58+
String expected = '''{
59+
"key1": "value1",
60+
"key2": "value2"
61+
}''';
62+
expect(removeJsonComments(input), expected);
63+
});
64+
65+
test('Test blank json', () {
66+
String input = '''
67+
{}
68+
''';
69+
70+
String expected = '{}';
71+
expect(removeJsonComments(input), expected);
72+
});
73+
});
74+
}
File renamed without changes.

pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,14 @@ packages:
811811
url: "https://pub.dev"
812812
source: hosted
813813
version: "0.6.7"
814+
json5:
815+
dependency: transitive
816+
description:
817+
name: json5
818+
sha256: b67d6e06c9e225c8277d3c43f796677af7975a2a2b0669ff12ba38ff466a31f4
819+
url: "https://pub.dev"
820+
source: hosted
821+
version: "0.8.2"
814822
json_annotation:
815823
dependency: "direct main"
816824
description:

0 commit comments

Comments
 (0)