Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private CSharpType GetNextPagePropertyType()
PropertyProvider? property = null;
for (int i = 0; i < NextPagePropertySegments.Count; i++)
{
property = model.Properties.First(p => p.WireInfo?.SerializedName == NextPagePropertySegments[i]);
property = FindPropertyInModelHierarchy(model, NextPagePropertySegments[i]);

if (i < NextPagePropertySegments.Count - 1)
{
Expand All @@ -154,6 +154,29 @@ private CSharpType GetNextPagePropertyType()
return property!.Type;
}

/// <summary>
/// Searches for a property with the specified serialized name in the model and its base models.
/// </summary>
private PropertyProvider FindPropertyInModelHierarchy(TypeProvider model, string serializedName)
{
// First, try to find the property in the current model
var property = model.Properties.FirstOrDefault(p => p.WireInfo?.SerializedName == serializedName);
if (property != null)
{
return property;
}

// If not found, search in the base model hierarchy
if (model is ModelProvider modelProvider && modelProvider.BaseModelProvider != null)
{
return FindPropertyInModelHierarchy(modelProvider.BaseModelProvider, serializedName);
}

// If not found anywhere, throw an exception with a helpful message
throw new InvalidOperationException(
$"Property with serialized name '{serializedName}' not found in model '{model.Name}' or its base models.");
}

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "CollectionResults", $"{Name}.cs");

protected override string BuildNamespace() => Client.Type.Namespace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,62 @@ public void UsesValidFieldIdentifierNames()
Assert.IsTrue(fields.Any(f => f.Name == "_foo"));
}

[Test]
public void InheritedNextLinkInBody()
{
CreatePagingOperationWithInheritedNextLink(InputResponseLocation.Body);

var collectionResultDefinition = ScmCodeModelGenerator.Instance.OutputLibrary.TypeProviders.FirstOrDefault(
t => t is CollectionResultDefinition && t.Name == "CatClientGetCatsCollectionResult");
Assert.IsNotNull(collectionResultDefinition);

var writer = new TypeProviderWriter(collectionResultDefinition!);
var file = writer.Write();
Assert.AreEqual(Helpers.GetExpectedFromFile(), file.Content);
}

[Test]
public void InheritedNextLinkInBodyAsync()
{
CreatePagingOperationWithInheritedNextLink(InputResponseLocation.Body);

var collectionResultDefinition = ScmCodeModelGenerator.Instance.OutputLibrary.TypeProviders.FirstOrDefault(
t => t is CollectionResultDefinition && t.Name == "CatClientGetCatsAsyncCollectionResult");
Assert.IsNotNull(collectionResultDefinition);

var writer = new TypeProviderWriter(collectionResultDefinition!);
var file = writer.Write();
Assert.AreEqual(Helpers.GetExpectedFromFile(), file.Content);
}

[Test]
public void InheritedNextLinkInBodyOfT()
{
CreatePagingOperationWithInheritedNextLink(InputResponseLocation.Body);

var collectionResultDefinition = ScmCodeModelGenerator.Instance.OutputLibrary.TypeProviders.FirstOrDefault(
t => t is CollectionResultDefinition && t.Name == "CatClientGetCatsCollectionResultOfT");
Assert.IsNotNull(collectionResultDefinition);

var writer = new TypeProviderWriter(collectionResultDefinition!);
var file = writer.Write();
Assert.AreEqual(Helpers.GetExpectedFromFile(), file.Content);
}

[Test]
public void InheritedNextLinkInBodyOfTAsync()
{
CreatePagingOperationWithInheritedNextLink(InputResponseLocation.Body);

var collectionResultDefinition = ScmCodeModelGenerator.Instance.OutputLibrary.TypeProviders.FirstOrDefault(
t => t is CollectionResultDefinition && t.Name == "CatClientGetCatsAsyncCollectionResultOfT");
Assert.IsNotNull(collectionResultDefinition);

var writer = new TypeProviderWriter(collectionResultDefinition!);
var file = writer.Write();
Assert.AreEqual(Helpers.GetExpectedFromFile(), file.Content);
}

private static void CreatePagingOperation(InputResponseLocation responseLocation, bool isNested = false)
{
var inputModel = InputFactory.Model("cat", properties:
Expand Down Expand Up @@ -267,5 +323,29 @@ private static void CreatePagingOperation(InputResponseLocation responseLocation

MockHelpers.LoadMockGenerator(inputModels: () => [inputModel], clients: () => [client]);
}

private static void CreatePagingOperationWithInheritedNextLink(InputResponseLocation responseLocation)
{
var inputModel = InputFactory.Model("cat", properties:
[
InputFactory.Property("color", InputPrimitiveType.String, isRequired: true),
]);

// Create the base model with nextLink property
var nextCatProperty = InputFactory.Property("nextCat", InputPrimitiveType.Url);
var baseModel = InputFactory.Model("basePage", properties: [nextCatProperty]);

// Create the derived model that inherits from base and adds cats property
var catsProperty = InputFactory.Property("cats", InputFactory.Array(inputModel));
var derivedModel = InputFactory.Model("page", properties: [catsProperty], baseModel: baseModel);

var pagingMetadata = InputFactory.NextLinkPagingMetadata(["cats"], ["nextCat"], responseLocation);
var response = InputFactory.OperationResponse([200], derivedModel);
var operation = InputFactory.Operation("getCats", responses: [response]);
var inputServiceMethod = InputFactory.PagingServiceMethod("getCats", operation, pagingMetadata: pagingMetadata);
var client = InputFactory.Client("catClient", methods: [inputServiceMethod]);

MockHelpers.LoadMockGenerator(inputModels: () => [inputModel, baseModel, derivedModel], clients: () => [client]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using Sample.Models;

namespace Sample
{
internal partial class CatClientGetCatsCollectionResult : global::System.ClientModel.Primitives.CollectionResult
{
private readonly global::Sample.CatClient _client;
private readonly global::System.ClientModel.Primitives.RequestOptions _options;

public CatClientGetCatsCollectionResult(global::Sample.CatClient client, global::System.ClientModel.Primitives.RequestOptions options)
{
_client = client;
_options = options;
}

public override global::System.Collections.Generic.IEnumerable<global::System.ClientModel.ClientResult> GetRawPages()
{
global::System.ClientModel.Primitives.PipelineMessage message = _client.CreateGetCatsRequest(_options);
global::System.Uri nextPageUri = null;
while (true)
{
global::System.ClientModel.ClientResult result = global::System.ClientModel.ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options));
yield return result;

nextPageUri = ((global::Sample.Models.Page)result).NextCat;
if ((nextPageUri == null))
{
yield break;
}
message = _client.CreateNextGetCatsRequest(nextPageUri, _options);
}
}

public override global::System.ClientModel.ContinuationToken GetContinuationToken(global::System.ClientModel.ClientResult page)
{
global::System.Uri nextPage = ((global::Sample.Models.Page)page).NextCat;
if ((nextPage != null))
{
return global::System.ClientModel.ContinuationToken.FromBytes(global::System.BinaryData.FromString(nextPage.AbsoluteUri));
}
else
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using Sample.Models;

namespace Sample
{
internal partial class CatClientGetCatsAsyncCollectionResult : global::System.ClientModel.Primitives.AsyncCollectionResult
{
private readonly global::Sample.CatClient _client;
private readonly global::System.ClientModel.Primitives.RequestOptions _options;

public CatClientGetCatsAsyncCollectionResult(global::Sample.CatClient client, global::System.ClientModel.Primitives.RequestOptions options)
{
_client = client;
_options = options;
}

public override async global::System.Collections.Generic.IAsyncEnumerable<global::System.ClientModel.ClientResult> GetRawPagesAsync()
{
global::System.ClientModel.Primitives.PipelineMessage message = _client.CreateGetCatsRequest(_options);
global::System.Uri nextPageUri = null;
while (true)
{
global::System.ClientModel.ClientResult result = global::System.ClientModel.ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false));
yield return result;

nextPageUri = ((global::Sample.Models.Page)result).NextCat;
if ((nextPageUri == null))
{
yield break;
}
message = _client.CreateNextGetCatsRequest(nextPageUri, _options);
}
}

public override global::System.ClientModel.ContinuationToken GetContinuationToken(global::System.ClientModel.ClientResult page)
{
global::System.Uri nextPage = ((global::Sample.Models.Page)page).NextCat;
if ((nextPage != null))
{
return global::System.ClientModel.ContinuationToken.FromBytes(global::System.BinaryData.FromString(nextPage.AbsoluteUri));
}
else
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using Sample.Models;

namespace Sample
{
internal partial class CatClientGetCatsCollectionResultOfT : global::System.ClientModel.CollectionResult<global::Sample.Models.Cat>
{
private readonly global::Sample.CatClient _client;
private readonly global::System.ClientModel.Primitives.RequestOptions _options;

public CatClientGetCatsCollectionResultOfT(global::Sample.CatClient client, global::System.ClientModel.Primitives.RequestOptions options)
{
_client = client;
_options = options;
}

public override global::System.Collections.Generic.IEnumerable<global::System.ClientModel.ClientResult> GetRawPages()
{
global::System.ClientModel.Primitives.PipelineMessage message = _client.CreateGetCatsRequest(_options);
global::System.Uri nextPageUri = null;
while (true)
{
global::System.ClientModel.ClientResult result = global::System.ClientModel.ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options));
yield return result;

nextPageUri = ((global::Sample.Models.Page)result).NextCat;
if ((nextPageUri == null))
{
yield break;
}
message = _client.CreateNextGetCatsRequest(nextPageUri, _options);
}
}

public override global::System.ClientModel.ContinuationToken GetContinuationToken(global::System.ClientModel.ClientResult page)
{
global::System.Uri nextPage = ((global::Sample.Models.Page)page).NextCat;
if ((nextPage != null))
{
return global::System.ClientModel.ContinuationToken.FromBytes(global::System.BinaryData.FromString(nextPage.AbsoluteUri));
}
else
{
return null;
}
}

protected override global::System.Collections.Generic.IEnumerable<global::Sample.Models.Cat> GetValuesFromPage(global::System.ClientModel.ClientResult page)
{
return ((global::Sample.Models.Page)page).Cats;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Threading.Tasks;
using Sample.Models;

namespace Sample
{
internal partial class CatClientGetCatsAsyncCollectionResultOfT : global::System.ClientModel.AsyncCollectionResult<global::Sample.Models.Cat>
{
private readonly global::Sample.CatClient _client;
private readonly global::System.ClientModel.Primitives.RequestOptions _options;

public CatClientGetCatsAsyncCollectionResultOfT(global::Sample.CatClient client, global::System.ClientModel.Primitives.RequestOptions options)
{
_client = client;
_options = options;
}

public override async global::System.Collections.Generic.IAsyncEnumerable<global::System.ClientModel.ClientResult> GetRawPagesAsync()
{
global::System.ClientModel.Primitives.PipelineMessage message = _client.CreateGetCatsRequest(_options);
global::System.Uri nextPageUri = null;
while (true)
{
global::System.ClientModel.ClientResult result = global::System.ClientModel.ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false));
yield return result;

nextPageUri = ((global::Sample.Models.Page)result).NextCat;
if ((nextPageUri == null))
{
yield break;
}
message = _client.CreateNextGetCatsRequest(nextPageUri, _options);
}
}

public override global::System.ClientModel.ContinuationToken GetContinuationToken(global::System.ClientModel.ClientResult page)
{
global::System.Uri nextPage = ((global::Sample.Models.Page)page).NextCat;
if ((nextPage != null))
{
return global::System.ClientModel.ContinuationToken.FromBytes(global::System.BinaryData.FromString(nextPage.AbsoluteUri));
}
else
{
return null;
}
}

protected override async global::System.Collections.Generic.IAsyncEnumerable<global::Sample.Models.Cat> GetValuesFromPageAsync(global::System.ClientModel.ClientResult page)
{
foreach (global::Sample.Models.Cat item in ((global::Sample.Models.Page)page).Cats)
{
yield return item;
await global::System.Threading.Tasks.Task.Yield();
}
}
}
}
Loading
Loading