Skip to content

Commit

Permalink
Revert "Add generation of enum array in query"
Browse files Browse the repository at this point in the history
  • Loading branch information
andrueastman authored Feb 29, 2024
1 parent fbc998e commit 3817e88
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 94 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed bug where stream responses would generate incorrect partial paging code. [#4207](https://github.com/microsoft/kiota/issues/4207)
- Sanitize Accept and Content-Type headers during generation. [#4211](https://github.com/microsoft/kiota/issues/4211)
- Fixed a bug in enum default value generator for TypeScript. [#4216](https://github.com/microsoft/kiota/issues/4216)
- Generate enum array for query parameters. [#4225](https://github.com/microsoft/kiota/issues/4225)

## [1.11.1] - 2024-02-05

Expand Down
16 changes: 7 additions & 9 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2343,25 +2343,23 @@ private void AddPropertyForQueryParameter(OpenApiUrlTreeNode node, OperationType
{
CodeType? resultType = default;
var addBackwardCompatibleParameter = false;

if (parameter.Schema.IsEnum() || (parameter.Schema.IsArray() && parameter.Schema.Items.IsEnum()))
if (parameter.Schema.IsEnum())
{
var enumSchema = parameter.Schema.IsArray() ? parameter.Schema.Items : parameter.Schema;
var codeNamespace = enumSchema.IsReferencedSchema() switch
var schema = parameter.Schema;
var codeNamespace = schema.IsReferencedSchema() switch
{
true => GetShortestNamespace(parameterClass.GetImmediateParentOfType<CodeNamespace>(), enumSchema), // referenced schema
true => GetShortestNamespace(parameterClass.GetImmediateParentOfType<CodeNamespace>(), schema), // referenced schema
false => parameterClass.GetImmediateParentOfType<CodeNamespace>(), // Inline schema, i.e. specific to the Operation
};
var shortestNamespace = GetShortestNamespace(codeNamespace, enumSchema);
var enumName = enumSchema.GetSchemaName().CleanupSymbolName();
var shortestNamespace = GetShortestNamespace(codeNamespace, schema);
var enumName = schema.GetSchemaName().CleanupSymbolName();
if (string.IsNullOrEmpty(enumName))
enumName = $"{operationType.ToString().ToFirstCharacterUpperCase()}{parameter.Name.CleanupSymbolName().ToFirstCharacterUpperCase()}QueryParameterType";
if (AddEnumDeclarationIfDoesntExist(node, enumSchema, enumName, shortestNamespace) is { } enumDeclaration)
if (AddEnumDeclarationIfDoesntExist(node, schema, enumName, shortestNamespace) is { } enumDeclaration)
{
resultType = new CodeType
{
TypeDefinition = enumDeclaration,
IsNullable = !parameter.Schema.IsArray()
};
addBackwardCompatibleParameter = true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ private static void AssignPropertyFromParameter(CodeClass parentClass, CodeMetho
{
for (var i = 0; i < parameters.Count; i++)
{
var isNonNullableCollection = !parameters[i].Type.IsNullable && parameters[i].Type.CollectionKind != CodeTypeBase.CodeTypeCollectionKind.None;
writer.WriteLine($"$this->{properties[i].Name.ToFirstCharacterLowerCase()} = ${parameters[i].Name.ToFirstCharacterLowerCase()}{(isNonNullableCollection ? "?? []" : string.Empty)};");
writer.WriteLine($"$this->{properties[i].Name.ToFirstCharacterLowerCase()} = ${parameters[i].Name.ToFirstCharacterLowerCase()};");
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Kiota.Builder/Writers/Python/CodePropertyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public override void WriteCodeElement(CodeProperty codeElement, LanguageWriter w
case CodePropertyKind.Options:
case CodePropertyKind.QueryParameter:
conventions.WriteInLineDescription(codeElement, writer);
var isNonNullableCollection = !codeElement.Type.IsNullable && codeElement.Type.CollectionKind != CodeTypeBase.CodeTypeCollectionKind.None;
writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)}{codeElement.NamePrefix}{codeElement.Name}: {(codeElement.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(codeElement.Type.IsNullable ? "]" : string.Empty)} {(isNonNullableCollection ? "= []" : "= None")}");
writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)}{codeElement.NamePrefix}{codeElement.Name}: {(codeElement.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(codeElement.Type.IsNullable ? "]" : string.Empty)} = None");
writer.WriteLine();
break;
case CodePropertyKind.ErrorMessageOverride when parentClass.IsErrorDefinition:
Expand Down
80 changes: 0 additions & 80 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Kiota.Builder.Extensions;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.MicrosoftExtensions;
Expand Down Expand Up @@ -7494,83 +7493,4 @@ The comment text in [Atlassian Document
var linkIssueRequestJsonBeanClass = codeModel.FindChildByName<CodeClass>("LinkIssueRequestJsonBean");
Assert.NotNull(linkIssueRequestJsonBeanClass);
}

[Fact]
public async Task EnumArrayQueryParameter()
{
const string schemaDocument = """
openapi: 3.0.2
info:
title: Enum
version: 1.0.0
paths:
/EnumQuery:
get:
parameters:
- name: enumValues
in: query
schema:
type: array
items:
$ref: '#/components/schemas/EnumValue'
- name: enumValues2
in: query
schema:
$ref: '#/components/schemas/EnumValue'
responses:
'200':
description: response
content:
application/json:
schema:
$ref: '#/components/schemas/EnumObject'
components:
schemas:
EnumValue:
type: string
enum:
- Value1
- Value2
- Value3
EnumObject:
type: object
properties:
enumArray:
type: array
items:
$ref: '#/components/schemas/EnumValue'
""";

var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
await using var fs = await GetDocumentStream(schemaDocument);

var builder = new KiotaBuilder(
NullLogger<KiotaBuilder>.Instance,
new GenerationConfiguration
{
ClientClassName = "EnumTest",
OpenAPIFilePath = tempFilePath,
IncludeAdditionalData = false
},
_httpClient);

var document = await builder.CreateOpenApiDocumentAsync(fs);
Assert.NotNull(document);
var node = builder.CreateUriSpace(document);
var codeModel = builder.CreateSourceModel(node);
Assert.NotNull(codeModel);
var enumRequestBuilder = codeModel.FindChildByName<CodeClass>("EnumQueryRequestBuilder");
Assert.NotNull(enumRequestBuilder);
var queryParameters = enumRequestBuilder.FindChildByName<CodeClass>("EnumQueryRequestBuilderGetQueryParameters");
Assert.NotNull(queryParameters);

Assert.Contains(queryParameters.Properties, p =>
p.Type is
{
IsCollection: true,
IsArray: true,
CollectionKind: CodeTypeBase.CodeTypeCollectionKind.Array,
Name: "EnumValue"
});
}
}

0 comments on commit 3817e88

Please sign in to comment.