Skip to content

Commit

Permalink
- draft implementation of defaulting to stream for unknown types
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
  • Loading branch information
baywet committed Mar 30, 2022
1 parent d78e7a1 commit c30941c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/Kiota.Builder/Extensions/OpenApiOperationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
namespace Kiota.Builder.Extensions {
public static class OpenApiOperationExtensions {
private static readonly HashSet<string> successCodes = new(StringComparer.OrdinalIgnoreCase) {"200", "201", "202"}; //204 excluded as it won't have a schema
private static readonly HashSet<string> validMimeTypes = new (StringComparer.OrdinalIgnoreCase) {
private static readonly HashSet<string> structuredMimeTypes = new (StringComparer.OrdinalIgnoreCase) {
"application/json",
"text/plain"
"application/xml",
"text/plain",
"text/xml",
"text/yaml",
};
/// <summary>
/// cleans application/vnd.github.mercy-preview+json to application/json
Expand All @@ -19,8 +22,7 @@ public static OpenApiSchema GetResponseSchema(this OpenApiOperation operation)
{
// Return Schema that represents all the possible success responses!
var schemas = operation.Responses.Where(r => successCodes.Contains(r.Key))
.SelectMany(re => re.Value.GetResponseSchemas())
.Where(s => s is not null);
.SelectMany(re => re.Value.GetResponseSchemas());

return schemas.FirstOrDefault();
}
Expand All @@ -29,7 +31,7 @@ public static IEnumerable<OpenApiSchema> GetResponseSchemas(this OpenApiResponse
var schemas = response.Content
.Where(c => !string.IsNullOrEmpty(c.Key))
.Select(c => (Key: c.Key.Split(';', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(), c.Value))
.Where(c => validMimeTypes.Contains(c.Key) || validMimeTypes.Contains(vendorSpecificCleanup.Replace(c.Key, string.Empty)))
.Where(c => structuredMimeTypes.Contains(c.Key) || structuredMimeTypes.Contains(vendorSpecificCleanup.Replace(c.Key, string.Empty)))
.Select(co => co.Value.Schema)
.Where(s => s is not null);

Expand Down
1 change: 0 additions & 1 deletion src/Kiota.Builder/GenerationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class GenerationConfiguration {
public GenerationLanguage Language { get; set; } = GenerationLanguage.CSharp;
public string ApiRootUrl { get; set; }
public string[] PropertiesPrefixToStrip { get; set; } = new string[] { "@odata."};
public HashSet<string> IgnoredRequestContentTypes { get; set; } = new();
public bool UsesBackingStore { get; set; }
public List<string> Serializers { get; set; } = new();
public List<string> Deserializers { get; set; } = new();
Expand Down
13 changes: 6 additions & 7 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ private void CreateRequestBuilderClass(CodeNamespace currentNamespace, OpenApiUr
{
foreach(var operation in currentNode
.PathItems[Constants.DefaultOpenApiLabel]
.Operations
.Where(x => x.Value.RequestBody?.Content?.Any(y => !config.IgnoredRequestContentTypes.Contains(y.Key)) ?? true))
.Operations)
CreateOperationMethods(currentNode, operation.Key, operation.Value, codeClass);
}
CreateUrlManagement(codeClass, currentNode, isApiClientClass);
Expand Down Expand Up @@ -622,13 +621,13 @@ private void CreateOperationMethods(OpenApiUrlTreeNode currentNode, OperationTyp
var returnType = CreateModelDeclarations(currentNode, schema, operation, executorMethod, "Response");
executorMethod.ReturnType = returnType ?? throw new InvalidOperationException("Could not resolve return type for operation");
} else {
var returnType = voidType;
if(operation.Responses.Any(x => x.Value.Content.ContainsKey(RequestBodyBinaryContentType)))
returnType = "binary";
string returnType;
if(!operation.Responses.Any(x => noContentStatusCodes.Contains(x.Key)))
returnType = voidType;
else if (operation.Responses.Any(x => x.Value.Content.ContainsKey(RequestBodyPlainTextContentType)))
returnType = "string";
else if(!operation.Responses.Any(x => noContentStatusCodes.Contains(x.Key)))
logger.LogWarning("could not find operation return type {operationType} {currentNodePath}", operationType, currentNode.Path);
else
returnType = "binary";
executorMethod.ReturnType = new CodeType { Name = returnType, IsExternal = true, };
}

Expand Down

0 comments on commit c30941c

Please sign in to comment.