Skip to content

Commit

Permalink
Fix for File Parameter interface not generated in some instances (#2972)
Browse files Browse the repository at this point in the history
- Split the Binary tests into 3 so each endpoint can be generated properly without missing the FileParameter interface
- changed RequiresFileParameterInterface logic so it checks isBinary on arrays and nested objects

Co-authored-by: Craig Ngu <Craig.Ngu@globalx.com.au>
  • Loading branch information
craigngu and Craig Ngu authored Aug 3, 2020
1 parent 857a834 commit 0acad7d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
62 changes: 60 additions & 2 deletions src/NSwag.CodeGeneration.TypeScript.Tests/BinaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task When_body_is_binary_then_blob_is_used_as_parameter_in_TypeScri
}

[Fact]
public async Task WhenSpecContainsFormData_ThenFormDataIsUsedInTypeScript()
public async Task WhenSpecContainsFormDataInSingleMultipartFile_ThenFormDataIsUsedInTypeScript()
{
var json = @"{
""x-generator"": ""NSwag v13.5.0.0 (NJsonSchema v10.1.15.0 (Newtonsoft.Json v11.0.0.0))"",
Expand Down Expand Up @@ -106,6 +106,35 @@ public async Task WhenSpecContainsFormData_ThenFormDataIsUsedInTypeScript()
}
}
},
},
""components"": {}
}";

var document = await OpenApiDocument.FromJsonAsync(json);

//// Act
var codeGenerator = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings());
var code = codeGenerator.GenerateFile();

//// Assert
Assert.Contains("const content_ = new FormData();", code);
Assert.Contains("interface FileParameter", code);
Assert.Contains("content_.append(\"file\", file.data, file.fileName ? file.fileName : \"file\");", code);
}


[Fact]
public async Task WhenSpecContainsFormDataInMultipartFileArray_ThenFormDataIsUsedInTypeScript()
{
var json = @"{
""x-generator"": ""NSwag v13.5.0.0 (NJsonSchema v10.1.15.0 (Newtonsoft.Json v11.0.0.0))"",
""openapi"": ""3.0.0"",
""info"": {
""title"": ""My Title"",
""version"": ""1.0.0""
},
""paths"": {
""/api/FileUpload/UploadFiles"": {
""post"": {
""tags"": [
Expand Down Expand Up @@ -147,6 +176,34 @@ public async Task WhenSpecContainsFormData_ThenFormDataIsUsedInTypeScript()
}
}
},
},
""components"": {}
}";

var document = await OpenApiDocument.FromJsonAsync(json);

//// Act
var codeGenerator = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings());
var code = codeGenerator.GenerateFile();

//// Assert
Assert.Contains("const content_ = new FormData();", code);
Assert.Contains("interface FileParameter", code);
Assert.Contains("content_.append(\"files\", item_.data, item_.fileName ? item_.fileName : \"files\")", code);
}

[Fact]
public async Task WhenSpecContainsFormDataInNestedMultipartForm_ThenFormDataIsUsedInTypeScript()
{
var json = @"{
""x-generator"": ""NSwag v13.5.0.0 (NJsonSchema v10.1.15.0 (Newtonsoft.Json v11.0.0.0))"",
""openapi"": ""3.0.0"",
""info"": {
""title"": ""My Title"",
""version"": ""1.0.0""
},
""paths"": {
""/api/FileUpload/UploadAttachment"": {
""post"": {
""tags"": [
Expand Down Expand Up @@ -212,7 +269,8 @@ public async Task WhenSpecContainsFormData_ThenFormDataIsUsedInTypeScript()
//// Assert
Assert.Contains("const content_ = new FormData();", code);
Assert.Contains("interface FileParameter", code);
Assert.Contains("content_.append(\"file\", file.data, file.fileName ? file.fileName : \"file\");", code);
Assert.Contains("content_.append(\"Contents\", contents.data, contents.fileName ? contents.fileName : \"Contents\");", code);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ public IEnumerable<string> ResponseClassNames
public bool RequiresFileParameterInterface =>
!_settings.TypeScriptGeneratorSettings.ExcludedTypeNames.Contains("FileParameter") &&
(_document.Operations.Any(o => o.Operation.ActualParameters.Any(p => p.ActualTypeSchema.IsBinary)) ||
_document.Operations.Any(o => o.Operation?.RequestBody?.Content?.Any(c => c.Value.Schema?.IsBinary == true || c.Value.Schema?.ActualProperties.Any(p => p.Value.IsBinary) == true) == true));
_document.Operations.Any(o => o.Operation?.RequestBody?.Content?.Any(c => c.Value.Schema?.IsBinary == true ||
c.Value.Schema?.ActualProperties.Any(p => p.Value.IsBinary ||
p.Value.Item?.IsBinary == true ||
p.Value.Items.Any(i => i.IsBinary)
) == true) == true));

/// <summary>Gets a value indicating whether the FileResponse interface should be rendered.</summary>
public bool RequiresFileResponseInterface =>
Expand Down

0 comments on commit 0acad7d

Please sign in to comment.