Skip to content

Commit

Permalink
fix: issue #33 postman request descriptions are strings (#34)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
YOU54F authored Sep 2, 2024
1 parent 7504ae8 commit e488338
Show file tree
Hide file tree
Showing 5 changed files with 1,325 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/gitversion.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
next-version: 0.6
next-version: 0.6.1
assembly-versioning-scheme: MajorMinorPatch
assembly-file-versioning-scheme: MajorMinorPatchTag
assembly-informational-format: '{InformationalVersion}'
Expand Down
63 changes: 54 additions & 9 deletions src/Explore.Cli/Models/Postman/PostmanCollectionContract.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable

using System.Text.Json;
using System.Text.Json.Serialization;

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

[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonConverter(typeof(DescriptionConverter))]
public Description? Description { get; set; }
}

public class Item
Expand All @@ -39,7 +41,8 @@ public class Item
public string? Name { get; set; }

[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonConverter(typeof(DescriptionConverter))]
public Description? Description { get; set; }

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

[JsonPropertyName("description")]
[JsonConverter(typeof(DescriptionConverter))]
public Description? Description { get; set; }
}

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

[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonConverter(typeof(DescriptionConverter))]
public Description? Description { get; set; }
}

public class Body
Expand Down Expand Up @@ -109,7 +114,8 @@ public class Formdata
public string? Type { get; set; }

[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonConverter(typeof(DescriptionConverter))]
public Description? Description { get; set; }
}

public class Urlencoded
Expand All @@ -121,7 +127,8 @@ public class Urlencoded
public string? Value { get; set; }

[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonConverter(typeof(DescriptionConverter))]
public Description? Description { get; set; }
}

public class Graphql
Expand Down Expand Up @@ -165,12 +172,50 @@ public class Query
[JsonPropertyName("value")]
public string? Value { get; set; }
}

public class Description
{
[JsonPropertyName("content")]
public string? Content { get; set; }

[JsonPropertyName("type")]
public string? Type { get; set; }
public string? Version { get; set; }
}

public class DescriptionConverter : JsonConverter<Description>
{
public override Description? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return new Description { Content = reader.GetString() };
}
else if (reader.TokenType == JsonTokenType.StartObject)
{
var description = JsonSerializer.Deserialize<Description>(ref reader, options);
return description;
}
else if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
throw new JsonException("Invalid JSON for Description");
}

public override void Write(Utf8JsonWriter writer, Description value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else if (value.Content == null)
{
writer.WriteStringValue(value.Content);
}
else
{
writer.WriteStartObject();
writer.WriteString("content", value.Content);
writer.WriteString("type", value.Type);
writer.WriteString("version", value.Version);
writer.WriteEndObject();
}
}
}
2 changes: 1 addition & 1 deletion src/Explore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ internal static async Task ImportPostmanCollection(string exploreCookie, string

//now let's create an API entry in the space
var cleanedAPIName = UtilityHelper.CleanString(item.Name);
var apiContent = new StringContent(JsonSerializer.Serialize(new ApiRequest() { Name = cleanedAPIName, Type = "REST", Description = $"imported from postman on {DateTime.UtcNow.ToShortDateString()}" }), Encoding.UTF8, "application/json");
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");

exploreHttpClient.DefaultRequestHeaders.Clear();
exploreHttpClient.DefaultRequestHeaders.Add("Cookie", exploreCookie);
Expand Down
13 changes: 12 additions & 1 deletion test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void IsCollectionVersion2_1_ShouldReturnTrue()
// Assert
Assert.True(result);
}

[Fact]
public void IsCollectionVersion2_1_ShouldReturnFalse()
{
Expand All @@ -110,4 +109,16 @@ public void IsCollectionVersion2_1_ShouldReturnFalse()
Assert.False(result3);
}

[Fact]
public void ProcessesDescriptions()
{
// Arrange
var filePath = "../../../fixtures/API_.documentation_postman_collection.json";
var mockCollectionAsJson = File.ReadAllText(filePath);
var postmanCollection = JsonSerializer.Deserialize<PostmanCollection>(mockCollectionAsJson);
// Act
Assert.Equal("Get authenticated user", postmanCollection.Item[0].ItemList[0].Name);

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 120 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
Assert.Equal("GET", postmanCollection.Item[0].ItemList[0].Request?.Method?.ToString());

Check warning on line 121 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 121 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 121 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 121 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
Assert.Equal("Gets information about the authenticated user.", postmanCollection.Item[0].ItemList[0].Request?.Description.Content.ToString());

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build-test-package

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 122 in test/Explore.Cli.Tests/PostmanCollectionMappingHelperTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}
}
Loading

0 comments on commit e488338

Please sign in to comment.