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 @@ -139,9 +139,31 @@ private MethodBodyStatement[] BuildXmlWriteMethodBody()
? _xmlWriterSnippet.WriteStartElement(ns.Prefix, nameHintExpression, ns.Namespace)
: _xmlWriterSnippet.WriteStartElement(nameHintExpression);

// When the model has a root namespace, pre-declare property namespaces that
// differ from the root so they appear on the root element rather than inline.
var nsDeclarations = new List<MethodBodyStatement>();
if (ns != null)
{
var propertyNamespaces = AllCategorizedXmlProperties.Namespaces;
if (propertyNamespaces != null)
{
foreach (var kvp in propertyNamespaces)
{
if (kvp.Key != ns.Namespace)
{
nsDeclarations.Add(
_xmlWriterSnippet.WriteNamespaceDeclaration(kvp.Value.Prefix, kvp.Key));
}
}
}
}

var ifBody = new List<MethodBodyStatement> { writeStartElement };
ifBody.AddRange(nsDeclarations);

return
[
new IfStatement(nameHintNotNull) { writeStartElement },
new IfStatement(nameHintNotNull) { ifBody.ToArray() },
MethodBodyStatement.EmptyLine,
This.Invoke(XmlModelWriteCoreMethodName, _xmlWriterParameter, _serializationOptionsParameter).Terminate(),
MethodBodyStatement.EmptyLine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public static MethodBodyStatement WriteValue(this ScopedApi<XmlWriter> writer, V
public static MethodBodyStatement WriteAttributeString(this ScopedApi<XmlWriter> writer, string prefix, string localName, string ns, ValueExpression value)
=> writer.Invoke(nameof(XmlWriter.WriteAttributeString), [Literal(prefix), Literal(localName), Literal(ns), value]).Terminate();

/// <summary>
/// Generates a namespace declaration attribute on the current element: xmlns:prefix="namespace".
/// </summary>
public static MethodBodyStatement WriteNamespaceDeclaration(this ScopedApi<XmlWriter> writer, string prefix, string ns)
=> writer.Invoke(nameof(XmlWriter.WriteAttributeString), [Literal("xmlns"), Literal(prefix), Null, Literal(ns)]).Terminate();

public static MethodBodyStatement WriteStringValue(this ScopedApi<XmlWriter> writer, ValueExpression value, string format)
=> ModelSerializationExtensionsSnippets.WriteStringValue(writer, value, format);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, T
IsDiscriminator = IsDiscriminatorProperty(inputProperty);
var hasOutputUsage = inputProperty.EnclosingType?.Usage.HasFlag(InputModelTypeUsage.Output) ?? false;
Modifiers = IsDiscriminator || (!hasOutputUsage && _isRequiredNonNullableConstant) ? MethodSignatureModifiers.Internal : MethodSignatureModifiers.Public;
Name = inputProperty.Name == enclosingType.Name
? $"{inputProperty.Name.ToIdentifierName()}Property"
: inputProperty.Name.ToIdentifierName();
var identifierName = inputProperty.Name.ToIdentifierName();
Name = identifierName == enclosingType.Name
? $"{identifierName}Property"
: identifierName;
Body = new AutoPropertyBody(propHasSetter, setterModifier, GetPropertyInitializationValue(propertyType, inputProperty));

WireInfo = new PropertyWireInformation(inputProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ public void TestSpecialWords()
Assert.AreEqual(inputPropertyName.ToIdentifierName() + "Property", property.Name);
}

[Test]
public void TestPropertyNameConflictsWithTypeNameAfterPascalCase()
{
var testTypeProvider = new TestTypeProvider(name: "Filter");
InputModelProperty inputModelProperty = InputFactory.Property("filter", InputPrimitiveType.String);
InputFactory.Model("Filter", properties: [inputModelProperty]);

var property = new PropertyProvider(inputModelProperty, testTypeProvider);
Assert.AreEqual("FilterProperty", property.Name);
}

[Test]
public void CanUpdatePropertyProvider()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,222 @@ public Task GetXmlErrorValue() => Test((host) =>
Assert.AreEqual(400, exception!.Status);
return Task.CompletedTask;
});

[SpectorTest]
public Task GetModelWithRenamedProperty() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithRenamedPropertyValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.Title);
Assert.AreEqual("bar", response.Value.Author);
});

[SpectorTest]
public Task PutModelWithRenamedProperty() => Test(async (host) =>
{
var model = new ModelWithRenamedProperty("foo", "bar");
var response = await new XmlClient(host, null).GetModelWithRenamedPropertyValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithNestedModel() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithNestedModelValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.NotNull(response.Value.Nested);
Assert.AreEqual("foo", response.Value.Nested.Name);
Assert.AreEqual(123, response.Value.Nested.Age);
});

[SpectorTest]
public Task PutModelWithNestedModel() => Test(async (host) =>
{
var model = new ModelWithNestedModel(new SimpleModel("foo", 123));
var response = await new XmlClient(host, null).GetModelWithNestedModelValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithRenamedNestedModel() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithRenamedNestedModelValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.NotNull(response.Value.Author);
Assert.AreEqual("foo", response.Value.Author.Name);
});

[SpectorTest]
public Task PutModelWithRenamedNestedModel() => Test(async (host) =>
{
var model = new ModelWithRenamedNestedModel(new Author("foo"));
var response = await new XmlClient(host, null).GetModelWithRenamedNestedModelValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithWrappedPrimitiveCustomItemNames() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithWrappedPrimitiveCustomItemNamesValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(2, response.Value.Tags.Count);
Assert.AreEqual("fiction", response.Value.Tags[0]);
Assert.AreEqual("classic", response.Value.Tags[1]);
});

[SpectorTest]
public Task PutModelWithWrappedPrimitiveCustomItemNames() => Test(async (host) =>
{
var model = new ModelWithWrappedPrimitiveCustomItemNames(new[] { "fiction", "classic" });
var response = await new XmlClient(host, null).GetModelWithWrappedPrimitiveCustomItemNamesValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithUnwrappedModelArray() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithUnwrappedModelArrayValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(2, response.Value.Items.Count);
Assert.AreEqual("foo", response.Value.Items[0].Name);
Assert.AreEqual(123, response.Value.Items[0].Age);
Assert.AreEqual("bar", response.Value.Items[1].Name);
Assert.AreEqual(456, response.Value.Items[1].Age);
});

[SpectorTest]
public Task PutModelWithUnwrappedModelArray() => Test(async (host) =>
{
var model = new ModelWithUnwrappedModelArray(new[]
{
new SimpleModel("foo", 123),
new SimpleModel("bar", 456)
});
var response = await new XmlClient(host, null).GetModelWithUnwrappedModelArrayValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithRenamedWrappedModelArray() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithRenamedWrappedModelArrayValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(2, response.Value.Items.Count);
Assert.AreEqual("foo", response.Value.Items[0].Name);
Assert.AreEqual(123, response.Value.Items[0].Age);
Assert.AreEqual("bar", response.Value.Items[1].Name);
Assert.AreEqual(456, response.Value.Items[1].Age);
});

[SpectorTest]
public Task PutModelWithRenamedWrappedModelArray() => Test(async (host) =>
{
var model = new ModelWithRenamedWrappedModelArray(new[]
{
new SimpleModel("foo", 123),
new SimpleModel("bar", 456)
});
var response = await new XmlClient(host, null).GetModelWithRenamedWrappedModelArrayValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithRenamedUnwrappedModelArray() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithRenamedUnwrappedModelArrayValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(2, response.Value.Items.Count);
Assert.AreEqual("foo", response.Value.Items[0].Name);
Assert.AreEqual(123, response.Value.Items[0].Age);
Assert.AreEqual("bar", response.Value.Items[1].Name);
Assert.AreEqual(456, response.Value.Items[1].Age);
});

[SpectorTest]
public Task PutModelWithRenamedUnwrappedModelArray() => Test(async (host) =>
{
var model = new ModelWithRenamedUnwrappedModelArray(new[]
{
new SimpleModel("foo", 123),
new SimpleModel("bar", 456)
});
var response = await new XmlClient(host, null).GetModelWithRenamedUnwrappedModelArrayValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithRenamedWrappedAndItemModelArray() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithRenamedWrappedAndItemModelArrayValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(2, response.Value.Books.Count);
Assert.AreEqual("The Great Gatsby", response.Value.Books[0].Title);
Assert.AreEqual("Les Miserables", response.Value.Books[1].Title);
});

[SpectorTest]
public Task PutModelWithRenamedWrappedAndItemModelArray() => Test(async (host) =>
{
var model = new ModelWithRenamedWrappedAndItemModelArray(new[]
{
new Book("The Great Gatsby"),
new Book("Les Miserables")
});
var response = await new XmlClient(host, null).GetModelWithRenamedWrappedAndItemModelArrayValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithRenamedAttribute() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithRenamedAttributeValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(123, response.Value.Id);
Assert.AreEqual("The Great Gatsby", response.Value.Title);
Assert.AreEqual("F. Scott Fitzgerald", response.Value.Author);
});

[SpectorTest]
public Task PutModelWithRenamedAttribute() => Test(async (host) =>
{
var model = new ModelWithRenamedAttribute(123, "The Great Gatsby", "F. Scott Fitzgerald");
var response = await new XmlClient(host, null).GetModelWithRenamedAttributeValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithNamespace() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithNamespaceValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(123, response.Value.Id);
Assert.AreEqual("The Great Gatsby", response.Value.Title);
});

[SpectorTest]
public Task PutModelWithNamespace() => Test(async (host) =>
{
var model = new ModelWithNamespace(123, "The Great Gatsby");
var response = await new XmlClient(host, null).GetModelWithNamespaceValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task GetModelWithNamespaceOnProperties() => Test(async (host) =>
{
var response = await new XmlClient(host, null).GetModelWithNamespaceOnPropertiesValueClient().GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(123, response.Value.Id);
Assert.AreEqual("The Great Gatsby", response.Value.Title);
Assert.AreEqual("F. Scott Fitzgerald", response.Value.Author);
});

[SpectorTest]
public Task PutModelWithNamespaceOnProperties() => Test(async (host) =>
{
var model = new ModelWithNamespaceOnProperties(123, "The Great Gatsby", "F. Scott Fitzgerald");
var response = await new XmlClient(host, null).GetModelWithNamespaceOnPropertiesValueClient().PutAsync(model);
Assert.AreEqual(204, response.GetRawResponse().Status);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Threading.Tasks;
using SpecialWords;
using SpecialWords._ExtensibleStrings;
using SpecialWords._ModelProperties;
using SpecialWords._Models;
using SpecialWordsAssert = SpecialWords._Models.Assert;
Expand Down Expand Up @@ -303,5 +304,13 @@ public Task ModelProperties_WithListAsync() => Test(async (host) =>
var response = await client.WithListAsync(body);
NUnit.Framework.Assert.AreEqual(204, response.GetRawResponse().Status);
});

[SpectorTest]
public Task ExtensibleStrings_PutExtensibleStringValueAsync() => Test(async (host) =>
{
var client = new SpecialWordsClient(host, null).GetExtensibleStringsClient();
var response = await client.PutExtensibleStringValueAsync(ExtensibleString.Class);
NUnit.Framework.Assert.AreEqual(ExtensibleString.Class, response.Value);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Text.Json;

namespace Payload.Pageable._ServerDrivenPagination.AlternateInitialVerb
{
public partial class Filter : IJsonModel<Filter>
{
internal Filter() => throw null;

protected virtual Filter PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) => throw null;

protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) => throw null;

BinaryData IPersistableModel<Filter>.Write(ModelReaderWriterOptions options) => throw null;

Filter IPersistableModel<Filter>.Create(BinaryData data, ModelReaderWriterOptions options) => throw null;

string IPersistableModel<Filter>.GetFormatFromOptions(ModelReaderWriterOptions options) => throw null;

public static implicit operator BinaryContent(Filter filter) => throw null;

void IJsonModel<Filter>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) => throw null;

protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) => throw null;

Filter IJsonModel<Filter>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => throw null;

protected virtual Filter JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => throw null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// <auto-generated/>

#nullable disable

namespace Payload.Pageable._ServerDrivenPagination.AlternateInitialVerb
{
public partial class Filter
{
public Filter(string filterProperty) => throw null;

public string FilterProperty => throw null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using System.ClientModel.Primitives;
using Payload.Pageable._PageSize;
using Payload.Pageable._ServerDrivenPagination;
using Payload.Pageable._ServerDrivenPagination.AlternateInitialVerb;
using Payload.Pageable._ServerDrivenPagination.ContinuationToken;

namespace Payload.Pageable
{
[ModelReaderWriterBuildable(typeof(Filter))]
[ModelReaderWriterBuildable(typeof(Pet))]
[ModelReaderWriterBuildable(typeof(XmlPet))]
public partial class PayloadPageableContext : ModelReaderWriterContext
Expand Down
Loading
Loading