Skip to content

Commit

Permalink
Merge pull request #4086 from microsoft/feature/none-key
Browse files Browse the repository at this point in the history
- adds support for none key for serializer arguments
  • Loading branch information
baywet authored Jan 31, 2024
2 parents 9f47a43 + 75f091e commit 801c779
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added 'none' key for serializer and deserializer arguments to enable portable clients generation. [#3796](https://github.com/microsoft/kiota/issues/3796)
- Added Japanese translations to vscode extension.
- Added support for deprecation annotations in Python. [#2798](https://github.com/microsoft/kiota/issues/2798)

Expand Down
10 changes: 8 additions & 2 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,8 @@ private void CreateUrlManagement(CodeClass currentClass, OpenApiUrlTreeNode curr
currentClass.AddProperty(pathParametersProperty);
if (isApiClientClass)
{
constructor.SerializerModules = config.Serializers;
constructor.DeserializerModules = config.Deserializers;
constructor.SerializerModules = ReplaceNoneSerializersByEmptySet(config.Serializers);
constructor.DeserializerModules = ReplaceNoneSerializersByEmptySet(config.Deserializers);
constructor.BaseUrl = config.ApiRootUrl ?? string.Empty;
pathParametersProperty.DefaultValue = $"new {pathParametersProperty.Type.Name}()";
}
Expand Down Expand Up @@ -1007,6 +1007,12 @@ private void CreateUrlManagement(CodeClass currentClass, OpenApiUrlTreeNode curr
currentClass.AddMethod(overloadCtor);
}
}
private static HashSet<string> ReplaceNoneSerializersByEmptySet(HashSet<string> serializers)
{
if (serializers.Count == 1 && serializers.Contains("none")) return [];
return serializers;
}

private static readonly Func<CodeClass, int> shortestNamespaceOrder = x => x.GetNamespaceDepth();
/// <summary>
/// Remaps definitions to custom types so they can be used later in generation or in refiners
Expand Down
10 changes: 5 additions & 5 deletions src/kiota/KiotaHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,23 +355,23 @@ private static Command GetGenerateCommand()

var serializerOption = new Option<List<string>>(
"--serializer",
() => defaultConfiguration.Serializers.ToList(),
"The fully qualified class names for serializers. Accepts multiple values.");
() => [.. defaultConfiguration.Serializers],
"The fully qualified class names for serializers. Accepts multiple values. Use `none` to generate a client without any serializer.");
serializerOption.AddAlias("-s");
serializerOption.ArgumentHelpName = "classes";

var deserializerOption = new Option<List<string>>(
"--deserializer",
() => defaultConfiguration.Deserializers.ToList(),
"The fully qualified class names for deserializers. Accepts multiple values.");
() => [.. defaultConfiguration.Deserializers],
"The fully qualified class names for deserializers. Accepts multiple values. Use `none` to generate a client without any deserializer.");
deserializerOption.AddAlias("--ds");
deserializerOption.ArgumentHelpName = "classes";

var cleanOutputOption = GetCleanOutputOption(defaultConfiguration.CleanOutput);

var structuredMimeTypesOption = new Option<List<string>>(
"--structured-mime-types",
() => defaultConfiguration.StructuredMimeTypes.ToList(),
() => [.. defaultConfiguration.StructuredMimeTypes],
"The MIME types with optional priorities as defined in RFC9110 Accept header to use for structured data model generation. Accepts multiple values.");
structuredMimeTypesOption.AddAlias("-m");

Expand Down
36 changes: 36 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,42 @@ await File.WriteAllTextAsync(tempFilePath, @$"openapi: 3.0.1
Assert.Equal(expected, constructor.BaseUrl);
}
[Fact]
public async Task HonoursNoneKeyForSerialization()
{
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
await File.WriteAllTextAsync(tempFilePath, @$"openapi: 3.0.1
info:
title: OData Service for namespace microsoft.graph
description: This OData service is located at https://graph.microsoft.com/v1.0
version: 1.0.1
servers:
- url: https://graph.microsoft.com/v1.0
paths:
/enumeration:
get:
responses:
'200':
content:
application/json:
schema:
type: string");
var mockLogger = new Mock<ILogger<KiotaBuilder>>();
var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration { ClientClassName = "Graph", OpenAPIFilePath = "https://graph.microsoft.com/description.yaml", Serializers = ["none"], Deserializers = ["none"] }, _httpClient);
await using var fs = new FileStream(tempFilePath, FileMode.Open);
var document = await builder.CreateOpenApiDocumentAsync(fs);
var node = builder.CreateUriSpace(document);
builder.SetApiRootUrl();
var codeModel = builder.CreateSourceModel(node);
var rootNS = codeModel.FindNamespaceByName("ApiSdk");
Assert.NotNull(rootNS);
var clientBuilder = rootNS.FindChildByName<CodeClass>("Graph", false);
Assert.NotNull(clientBuilder);
var constructor = clientBuilder.Methods.FirstOrDefault(static x => x.IsOfKind(CodeMethodKind.ClientConstructor));
Assert.NotNull(constructor);
Assert.Empty(constructor.SerializerModules);
Assert.Empty(constructor.DeserializerModules);
}
[Fact]
public async Task DeduplicatesHostNames()
{
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
Expand Down

0 comments on commit 801c779

Please sign in to comment.