Skip to content

Commit e488338

Browse files
authored
fix: issue #33 postman request descriptions are strings (#34)
* chore: consider postman description can be object/string/null Content will be populated, with string value, if not expliclity set. * chore: bump rel version to 0.6.1
1 parent 7504ae8 commit e488338

5 files changed

Lines changed: 1325 additions & 12 deletions

File tree

.github/gitversion.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
next-version: 0.6
1+
next-version: 0.6.1
22
assembly-versioning-scheme: MajorMinorPatch
33
assembly-file-versioning-scheme: MajorMinorPatchTag
44
assembly-informational-format: '{InformationalVersion}'

src/Explore.Cli/Models/Postman/PostmanCollectionContract.cs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#nullable enable
22

3+
using System.Text.Json;
34
using System.Text.Json.Serialization;
45

56
namespace Explore.Cli.Models.Postman;
@@ -27,7 +28,8 @@ public partial class PostmanCollectionInfo
2728
public string? Version { get; set; }
2829

2930
[JsonPropertyName("description")]
30-
public string? Description { get; set; }
31+
[JsonConverter(typeof(DescriptionConverter))]
32+
public Description? Description { get; set; }
3133
}
3234

3335
public class Item
@@ -39,7 +41,8 @@ public class Item
3941
public string? Name { get; set; }
4042

4143
[JsonPropertyName("description")]
42-
public string? Description { get; set; }
44+
[JsonConverter(typeof(DescriptionConverter))]
45+
public Description? Description { get; set; }
4346

4447
[JsonPropertyName("request")]
4548
public Request? Request { get; set; }
@@ -64,6 +67,7 @@ public class Request
6467
public Url? Url { get; set; }
6568

6669
[JsonPropertyName("description")]
70+
[JsonConverter(typeof(DescriptionConverter))]
6771
public Description? Description { get; set; }
6872
}
6973

@@ -76,7 +80,8 @@ public class Header
7680
public string? Value { get; set; }
7781

7882
[JsonPropertyName("description")]
79-
public string? Description { get; set; }
83+
[JsonConverter(typeof(DescriptionConverter))]
84+
public Description? Description { get; set; }
8085
}
8186

8287
public class Body
@@ -109,7 +114,8 @@ public class Formdata
109114
public string? Type { get; set; }
110115

111116
[JsonPropertyName("description")]
112-
public string? Description { get; set; }
117+
[JsonConverter(typeof(DescriptionConverter))]
118+
public Description? Description { get; set; }
113119
}
114120

115121
public class Urlencoded
@@ -121,7 +127,8 @@ public class Urlencoded
121127
public string? Value { get; set; }
122128

123129
[JsonPropertyName("description")]
124-
public string? Description { get; set; }
130+
[JsonConverter(typeof(DescriptionConverter))]
131+
public Description? Description { get; set; }
125132
}
126133

127134
public class Graphql
@@ -165,12 +172,50 @@ public class Query
165172
[JsonPropertyName("value")]
166173
public string? Value { get; set; }
167174
}
168-
169175
public class Description
170176
{
171-
[JsonPropertyName("content")]
172177
public string? Content { get; set; }
173-
174-
[JsonPropertyName("type")]
175178
public string? Type { get; set; }
179+
public string? Version { get; set; }
176180
}
181+
182+
public class DescriptionConverter : JsonConverter<Description>
183+
{
184+
public override Description? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
185+
{
186+
if (reader.TokenType == JsonTokenType.String)
187+
{
188+
return new Description { Content = reader.GetString() };
189+
}
190+
else if (reader.TokenType == JsonTokenType.StartObject)
191+
{
192+
var description = JsonSerializer.Deserialize<Description>(ref reader, options);
193+
return description;
194+
}
195+
else if (reader.TokenType == JsonTokenType.Null)
196+
{
197+
return null;
198+
}
199+
throw new JsonException("Invalid JSON for Description");
200+
}
201+
202+
public override void Write(Utf8JsonWriter writer, Description value, JsonSerializerOptions options)
203+
{
204+
if (value == null)
205+
{
206+
writer.WriteNullValue();
207+
}
208+
else if (value.Content == null)
209+
{
210+
writer.WriteStringValue(value.Content);
211+
}
212+
else
213+
{
214+
writer.WriteStartObject();
215+
writer.WriteString("content", value.Content);
216+
writer.WriteString("type", value.Type);
217+
writer.WriteString("version", value.Version);
218+
writer.WriteEndObject();
219+
}
220+
}
221+
}

src/Explore.Cli/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ internal static async Task ImportPostmanCollection(string exploreCookie, string
180180

181181
//now let's create an API entry in the space
182182
var cleanedAPIName = UtilityHelper.CleanString(item.Name);
183-
var apiContent = new StringContent(JsonSerializer.Serialize(new ApiRequest() { Name = cleanedAPIName, Type = "REST", Description = $"imported from postman on {DateTime.UtcNow.ToShortDateString()}" }), Encoding.UTF8, "application/json");
183+
var apiContent = new StringContent(JsonSerializer.Serialize(new ApiRequest() { Name = cleanedAPIName, Type = "REST", Description = $"{item.Request.Description?.Content + "\n" }imported from postman on {DateTime.UtcNow.ToShortDateString()}" }), Encoding.UTF8, "application/json");
184184

185185
exploreHttpClient.DefaultRequestHeaders.Clear();
186186
exploreHttpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie);

test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ public void IsCollectionVersion2_1_ShouldReturnTrue()
9090
// Assert
9191
Assert.True(result);
9292
}
93-
9493
[Fact]
9594
public void IsCollectionVersion2_1_ShouldReturnFalse()
9695
{
@@ -110,4 +109,16 @@ public void IsCollectionVersion2_1_ShouldReturnFalse()
110109
Assert.False(result3);
111110
}
112111

112+
[Fact]
113+
public void ProcessesDescriptions()
114+
{
115+
// Arrange
116+
var filePath = "../../../fixtures/API_.documentation_postman_collection.json";
117+
var mockCollectionAsJson = File.ReadAllText(filePath);
118+
var postmanCollection = JsonSerializer.Deserialize<PostmanCollection>(mockCollectionAsJson);
119+
// Act
120+
Assert.Equal("Get authenticated user", postmanCollection.Item[0].ItemList[0].Name);
121+
Assert.Equal("GET", postmanCollection.Item[0].ItemList[0].Request?.Method?.ToString());
122+
Assert.Equal("Gets information about the authenticated user.", postmanCollection.Item[0].ItemList[0].Request?.Description.Content.ToString());
123+
}
113124
}

0 commit comments

Comments
 (0)