Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol tests issue 31 #3265

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Json.LitJson"#>
<#
AddLicenseHeader();

Expand Down Expand Up @@ -106,7 +107,7 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
if(string.IsNullOrEmpty(operation.XmlNamespacePrefix))
{
#>
xmlWriter.WriteStartElement("<#=operation.RequestStructure.MarshallName#>", "<#=operation.XmlNamespace#>");
xmlWriter.WriteStartElement("<#=operation.RequestStructure.MarshallName#>", "<#=operation.XmlNamespace#>");
<#+
}
else
Expand All @@ -119,7 +120,7 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
}
else
{
if(string.IsNullOrEmpty(operation.XmlNamespacePrefix))
if(string.IsNullOrEmpty(operation.XmlNamespace))
{
#>
if (<#=variableName + ".IsSet" + operation.RequestPayloadMember.PropertyName#>())
Expand All @@ -129,12 +130,23 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
}
else
{
if(string.IsNullOrEmpty(operation.XmlNamespacePrefix))
{
#>
if (<#=variableName + ".IsSet" + operation.RequestPayloadMember.PropertyName#>())
{
xmlWriter.WriteStartElement("<#=operation.RequestPayloadMember.LocationName != operation.RequestStructure.PayloadMemberName ? operation.RequestPayloadMember.LocationName : operation.RequestPayloadMember.Shape.MarshallName#>", "<#=operation.XmlNamespace#>");
<#+
}
else
{
#>
if (<#=variableName + ".IsSet" + operation.RequestPayloadMember.PropertyName#>())
{
xmlWriter.WriteStartElement("<#=operation.RequestPayloadMember.LocationName != operation.RequestStructure.PayloadMemberName ? operation.RequestPayloadMember.LocationName : operation.RequestPayloadMember.Shape.MarshallName#>");
xmlWriter.WriteAttributeString("xmlns", "<#=operation.XmlNamespacePrefix#>", null, "<#=operation.XmlNamespace#>");
xmlWriter.WriteAttributeString("xmlns","<#=operation.XmlNamespacePrefix#>",null,"<#=operation.XmlNamespace#>");
<#+
}
}
level++;
}
Expand Down Expand Up @@ -172,8 +184,20 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
{
#>
<#=new string(' ', level * 4)#> if(<#=variableName#>.IsSet<#=member.PropertyName#>())
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.MarshallName#>", <#=member.PrimitiveMarshaller#>(<#=variableName#>.<#=member.PropertyName#><#=(member.UseNullable ? ".Value" : string.Empty)#>));
<#+
if(string.IsNullOrEmpty(member.XmlNamespace))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.MarshallName#>", <#=member.PrimitiveMarshaller#>(<#=variableName#>.<#=member.PropertyName#><#=(member.UseNullable ? ".Value" : string.Empty)#>));
<#+
}
else
{
string prefix = member.XmlNamespacePrefix != null ? $"{member.XmlNamespacePrefix}" : null;
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString(<#=prefix#>,"<#=member.MarshallName#>","<#=member.XmlNamespace#>", <#=member.PrimitiveMarshaller#>(<#=variableName#>.<#=member.PropertyName#><#=(member.UseNullable ? ".Value" : string.Empty)#>));
<#+
}
if(member.IsIdempotent)
{
#>
Expand Down Expand Up @@ -266,43 +290,88 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
#>
<#=new string(' ', level * 4)#> if (<#=variableName#> != null)
<#=new string(' ', level * 4)#> {
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=marshallName#>");
<#+
if(string.IsNullOrEmpty(shape.XmlNamespace))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=marshallName#>");
<#+
}
else
{
if(string.IsNullOrEmpty(shape.XmlNamespacePrefix))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=marshallName#>", "<#=shape.XmlNamespace#>");
<#+
}
else
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=marshallName#>");
<#=new string(' ', level * 4)#> xmlWriter.WriteAttributeString("xmlns", "<#=marshallName#>",<#=shape.XmlNamespace#>, null );
<#+
}
}
ProcessMembers(level + 1, variableName, shape.Members);
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteEndElement();
<#=new string(' ', level * 4)#> }
<#+
}

// Only namespaces at the top level Shape matter for a structure, so there is no logic for namespaces here.
void ProcessStructure(int level, string variableName, Member member)
{
var shape = member.Shape.IsList ? member.Shape.ListShape : member.Shape ;
variableName = member.Shape.IsList ? variableName : variableName + "." + member.PropertyName;

// Use shape's ListMarshallName if the structure is a list.
var marshallName = member.Shape.IsList ? member.Shape.ListMarshallName ?? "member" : member.MarshallName;
#>
<#=new string(' ', level * 4)#> if (<#=variableName#> != null)
#>
<#=new string(' ', level * 4)#> if (<#=variableName#> != null)
<#=new string(' ', level * 4)#> {
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=marshallName#>");
<#+
ProcessMembers(level + 1, variableName, shape.Members);
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=marshallName#>");
<#+
#>
<#+
ProcessMembers(level + 1, variableName, shape.Members);
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteEndElement();
<#=new string(' ', level * 4)#> }
<#+
}

void ProcessList(int level, string variableName, Member member)
{
var listVariable = (variableName + member.PropertyName).Replace(".",string.Empty);
var listItemVariable = (variableName + member.PropertyName).Replace(".",string.Empty) + "Value";
#>
<#=new string(' ', level * 4)#> var <#=listVariable#> = <#=variableName#>.<#=member.PropertyName#>;
<#=new string(' ', level * 4)#> if (<#=listVariable#> != null && <#=listVariable#>.Count > 0)
<#=new string(' ', level * 4)#> {
<#=new string(' ', level * 4)#> {
<#+
if(string.IsNullOrEmpty(member.XmlNamespace))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=member.MarshallName#>");
<#+
}
else
{
if(string.IsNullOrEmpty(member.XmlNamespacePrefix))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=member.MarshallName#>", "<#=member.XmlNamespace#>");
<#+
}
else
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=member.MarshallName#>");
<#=new string(' ', level * 4)#> xmlWriter.WriteAttributeString("xmlns","<#=member.XmlNamespacePrefix#>",null,"<#=member.XmlNamespace#>");
<#+
}
}
#>
<#=new string(' ', level * 4)#> foreach (var <#=listItemVariable#> in <#=listVariable#>)
<#=new string(' ', level * 4)#> {
<#+
Expand All @@ -325,9 +394,35 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
}
else
{
var listNode = member.Shape.data;
var listMemberNode = listNode[Shape.MemberKey];
// Use shape's ListMarshallName as it's a list structure.
if(listMemberNode == null || listMemberNode[ServiceModel.XmlNamespaceKey] == null || string.IsNullOrEmpty((string)listMemberNode[ServiceModel.XmlNamespaceKey]))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=listMarshallName#>");
<#+
}
else
{
JsonData listMemberXmlNamespaceNode = listMemberNode[ServiceModel.XmlNamespaceKey];
// if it's a string, it has no prefix
if(listMemberXmlNamespaceNode.IsString)
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=listMarshallName#>", "<#=listMemberXmlNamespaceNode#>");

<#+
}
else
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=listMarshallName#>");
<#=new string(' ', level * 4)#> xmlWriter.WriteAttributeString("xmlns","<#=listMemberXmlNamespaceNode[ServiceModel.XmlNamespacePrefixKey]#>",null,"<#=listMemberXmlNamespaceNode[ServiceModel.XmlNamespaceUriKey]#>");
<#+
}
}
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteValue(<#=listItemVariable#>);
<#=new string(' ', level * 4)#> xmlWriter.WriteEndElement();
<#+
Expand All @@ -342,13 +437,58 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations

void ProcessMap(int level, string variableName, Member member)
{
if(string.IsNullOrEmpty(member.XmlNamespace))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=member.MarshallName#>");
<#=new string(' ', level * 4)#> foreach (var kvp in <#=variableName#>.<#=member.PropertyName#>)
<#+
}
else
{
if(string.IsNullOrEmpty(member.XmlNamespacePrefix))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=member.MarshallName#>","<#=member.XmlNamespace#>");

<#+
}
else
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=member.MarshallName#>");
<#=new string(' ', level * 4)#> xmlWriter.WriteAttributeString("xmlns","<#=member.XmlNamespacePrefix#>",null,"<#=member.XmlNamespace#>");
<#+
}
}
#>
<#=new string(' ', level * 4)#> foreach (var kvp in <#=variableName#>.<#=member.PropertyName#>)
<#=new string(' ', level * 4)#> {
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("entry");
<#+
if(string.IsNullOrEmpty(member.Shape.KeyShapeXmlNamespace))
{
#>

<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.Shape.KeyMarshallName#>", kvp.Key);
<#+
}
else
{
var keyNode = member.Shape.data[Shape.KeyKey];
if(keyNode[ServiceModel.XmlNamespaceKey].IsString)
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.Shape.KeyMarshallName#>","<#=member.Shape.KeyShapeXmlNamespace#>",kvp.Key);
<#+
}
else
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=keyNode[ServiceModel.XmlNamespaceKey][ServiceModel.XmlNamespacePrefixKey]#>","<#=member.Shape.KeyMarshallName#>","<#=member.Shape.KeyShapeXmlNamespace#>",kvp.Key);

<#+
}
}
if(member.Shape.ValueShape.IsMap)
{
ProcessSubMap(level + 1, member.Shape.ValueShape, "kvp");
Expand All @@ -357,7 +497,6 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=member.Shape.ValueMarshallName#>");

<#+
ProcessStructureAsMapValue(level + 2, "kvp.Value", member.Shape.ValueShape);
#>
Expand All @@ -366,9 +505,27 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
}
else
{
if(string.IsNullOrEmpty(member.Shape.ValueShapeXmlNamespace))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.Shape.ValueMarshallName#>", kvp.Value<#=member.Shape.ValueShape.IsString ? "" : ".ToString()"#>);
<#+
}
else
{
var valueNode = member.Shape.data[Shape.ValueKey];
if(valueNode[ServiceModel.XmlNamespaceKey].IsString)
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.Shape.ValueMarshallName#>", "<#=member.Shape.ValueShapeXmlNamespace#>",kvp.Value<#=member.Shape.ValueShape.IsString ? "" : ".ToString()"#>);
<#+ }
else
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=valueNode[ServiceModel.XmlNamespaceKey][ServiceModel.XmlNamespacePrefixKey]#>","<#=member.Shape.ValueMarshallName#>", "<#=member.Shape.ValueShapeXmlNamespace#>",kvp.Value<#=member.Shape.ValueShape.IsString ? "" : ".ToString()"#>);
<#+
}
}
}
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteEndElement();
Expand All @@ -385,8 +542,19 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
<#=new string(' ', level * 4)#> foreach (var kvp in <#=variableName#>.<#=member.PropertyName#>)
<#=new string(' ', level * 4)#> {
<#=new string(' ', level * 4)#> xmlWriter.WriteStartElement("<#=member.MarshallName#>");
<#+
if(string.IsNullOrEmpty(member.Shape.KeyShapeXmlNamespace))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.Shape.KeyMarshallName#>", kvp.Key);
<#+
}
else
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.Shape.KeyMarshallName#>","<#=member.Shape.KeyShapeXmlNamespace#>", kvp.Key);
<#+
}
if(member.Shape.ValueShape.IsMap)
{
ProcessSubMap(level + 1, member.Shape.ValueShape, "kvp");
Expand All @@ -403,10 +571,20 @@ namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
}
else
{
if(string.IsNullOrEmpty(member.Shape.ValueShapeXmlNamespace))
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.Shape.ValueMarshallName#>", kvp.Value<#=member.Shape.ValueShape.IsString ? "" : ".ToString()"#>);
<#=new string(' ', level * 4)#> xmlWriter.WriteEndElement();
<#+
}
else
{
#>
<#=new string(' ', level * 4)#> xmlWriter.WriteElementString("<#=member.Shape.ValueMarshallName#>", "<#=member.Shape.ValueShapeXmlNamespace#>", kvp.Value<#=member.Shape.ValueShape.IsString ? "" : ".ToString()"#>);
<#=new string(' ', level * 4)#> xmlWriter.WriteEndElement();
<#+
}
}
#>
<#=new string(' ', level * 4)#> }
Expand Down
35 changes: 34 additions & 1 deletion generator/ServiceClientGeneratorLib/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,40 @@ public Shape KeyShape
return this.model.FindShape(extendsNode.ToString());
}
}

/// <summary>
/// Gets the map's key node's xmlnamespace.
/// </summary>
public string KeyShapeXmlNamespace
{
/*
"type": "map",
"key": {
"shape": "String",
"locationName": "K",
"xmlNamespace": "https://the-key.example.com"
},
*/
get
{
var keyNode = this.data[KeyKey];
if (keyNode == null || keyNode[ServiceModel.XmlNamespaceKey] == null)
return "";
return (string)keyNode[ServiceModel.XmlNamespaceKey];
}
}
/// <summary>
/// Gets the map's value node's xmlnamespace
/// </summary>
public string ValueShapeXmlNamespace
{
get
{
var valueNode = this.data[ValueKey];
if (valueNode == null || valueNode[ServiceModel.XmlNamespaceKey] == null)
return "";
return (string)valueNode[ServiceModel.XmlNamespaceKey];
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't check for IsMap. Should the base methods be modified instead with specific instructions for IsMap type shapes/members"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I think that might be better actually, let me try to make that change and see if it works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spoke to Bo offline and we agreed that since this is only valid on a shape, it shouldn't be on the base class. Instead I just added a check for ISMap

/// <summary>
/// The marshall name used for the key part of a dictionary.
/// </summary>
Expand Down
Loading