Skip to content
Open
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 @@ -250,7 +250,10 @@ internal CodeTypeDeclaration ExportEnum(EnumMapping mapping, Type type)
CodeNamespace.Types.Add(codeClass);
for (int i = 0; i < mapping.Constants.Length; i++)
{
ExportConstant(codeClass, mapping.Constants[i], type, mapping.IsFlags, 1L << i);
ConstantMapping constant = mapping.Constants[i];
long defaultValue = mapping.IsFlags ? (1L << i) : i;
bool shouldEmitValue = mapping.IsFlags || constant.Value != defaultValue;
ExportConstant(codeClass, constant, type, shouldEmitValue, constant.Value);
}
if (mapping.IsFlags)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.Xml.Serialization
using Microsoft.Xml.Schema;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Diagnostics;
using Microsoft.CodeDom.Compiler;
Expand Down Expand Up @@ -638,6 +639,7 @@ private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, strin
throw new InvalidOperationException(string.Format(ResXml.XmlInvalidEnumContent, dataType.Content.GetType().Name, identifier));

XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;
int enumIndex = 0;

for (int i = 0; i < restriction.Facets.Count; i++)
{
Expand All @@ -648,7 +650,9 @@ private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, strin
string constantName = CodeIdentifier.MakeValid(enumeration.Value);
constant.Name = constants.AddUnique(constantName, constant);
constant.XmlName = enumeration.Value;
constant.Value = i;
long defaultValue = isList ? (1L << enumIndex) : enumIndex;
constant.Value = TryGetDataContractEnumValue(enumeration.Annotation, out long enumValue) ? enumValue : defaultValue;
enumIndex++;
}
Comment thread
imcarolwang marked this conversation as resolved.
enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
if (isList && enumMapping.Constants.Length > 63)
Expand All @@ -665,6 +669,39 @@ private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, strin
return enumMapping;
}

private static bool TryGetDataContractEnumValue(XmlSchemaAnnotation annotation, out long value)
{
value = default;

if (annotation?.Items == null)
{
return false;
}

foreach (XmlSchemaObject annotationItem in annotation.Items)
{
XmlSchemaAppInfo appInfo = annotationItem as XmlSchemaAppInfo;
if (appInfo?.Markup == null)
{
continue;
}

foreach (XmlNode node in appInfo.Markup)
{
XmlElement element = node as XmlElement;
if (element != null
&& element.LocalName == "EnumerationValue"
&& element.NamespaceURI == "http://schemas.microsoft.com/2003/10/Serialization/"
&& long.TryParse(element.InnerText, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
{
return true;
}
}
}

return false;
}

private PrimitiveMapping ImportPrimitiveDataType(XmlSchemaSimpleType dataType)
{
TypeDesc sourceTypeDesc = GetDataTypeSource(dataType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,7 @@ private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, strin
if (content is XmlSchemaSimpleTypeRestriction)
{
XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)content;
int enumIndex = 0;
for (int i = 0; i < restriction.Facets.Count; i++)
{
object facet = restriction.Facets[i];
Expand All @@ -1869,7 +1870,9 @@ private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, strin
string constantName = CodeIdentifier.MakeValid(enumeration.Value);
constant.Name = constants.AddUnique(constantName, constant);
constant.XmlName = enumeration.Value;
constant.Value = i;
long defaultValue = isList ? (1L << enumIndex) : enumIndex;
constant.Value = TryGetDataContractEnumValue(enumeration.Annotation, out long enumValue) ? enumValue : defaultValue;
enumIndex++;
}
Comment thread
imcarolwang marked this conversation as resolved.
}
enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
Expand All @@ -1885,6 +1888,39 @@ private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, strin
return enumMapping;
}

private static bool TryGetDataContractEnumValue(XmlSchemaAnnotation annotation, out long value)
{
value = default;

if (annotation?.Items == null)
{
return false;
}

foreach (XmlSchemaObject annotationItem in annotation.Items)
{
XmlSchemaAppInfo appInfo = annotationItem as XmlSchemaAppInfo;
if (appInfo?.Markup == null)
{
continue;
}

foreach (XmlNode node in appInfo.Markup)
{
XmlElement element = node as XmlElement;
if (element != null
&& element.LocalName == "EnumerationValue"
&& element.NamespaceURI == "http://schemas.microsoft.com/2003/10/Serialization/"
&& long.TryParse(element.InnerText, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
{
return true;
}
}
}

return false;
}

internal class ElementComparer : IComparer
{
public int Compare(object o1, object o2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1140,145 +1140,145 @@ public enum HttpStatusCode
{

/// <remarks/>
Continue,
Continue = 100,

/// <remarks/>
SwitchingProtocols,
SwitchingProtocols = 101,

/// <remarks/>
OK,
OK = 200,

/// <remarks/>
Created,
Created = 201,

/// <remarks/>
Accepted,
Accepted = 202,

/// <remarks/>
NonAuthoritativeInformation,
NonAuthoritativeInformation = 203,

/// <remarks/>
NoContent,
NoContent = 204,

/// <remarks/>
ResetContent,
ResetContent = 205,

/// <remarks/>
PartialContent,
PartialContent = 206,

/// <remarks/>
MultipleChoices,
MultipleChoices = 300,

/// <remarks/>
Ambiguous,
Ambiguous = 300,

/// <remarks/>
MovedPermanently,
MovedPermanently = 301,

/// <remarks/>
Moved,
Moved = 301,

/// <remarks/>
Found,
Found = 302,

/// <remarks/>
Redirect,
Redirect = 302,

/// <remarks/>
SeeOther,
SeeOther = 303,

/// <remarks/>
RedirectMethod,
RedirectMethod = 303,

/// <remarks/>
NotModified,
NotModified = 304,

/// <remarks/>
UseProxy,
UseProxy = 305,

/// <remarks/>
Unused,
Unused = 306,

/// <remarks/>
TemporaryRedirect,
TemporaryRedirect = 307,

/// <remarks/>
RedirectKeepVerb,
RedirectKeepVerb = 307,

/// <remarks/>
BadRequest,
BadRequest = 400,

/// <remarks/>
Unauthorized,
Unauthorized = 401,

/// <remarks/>
PaymentRequired,
PaymentRequired = 402,

/// <remarks/>
Forbidden,
Forbidden = 403,

/// <remarks/>
NotFound,
NotFound = 404,

/// <remarks/>
MethodNotAllowed,
MethodNotAllowed = 405,

/// <remarks/>
NotAcceptable,
NotAcceptable = 406,

/// <remarks/>
ProxyAuthenticationRequired,
ProxyAuthenticationRequired = 407,

/// <remarks/>
RequestTimeout,
RequestTimeout = 408,

/// <remarks/>
Conflict,
Conflict = 409,

/// <remarks/>
Gone,
Gone = 410,

/// <remarks/>
LengthRequired,
LengthRequired = 411,

/// <remarks/>
PreconditionFailed,
PreconditionFailed = 412,

/// <remarks/>
RequestEntityTooLarge,
RequestEntityTooLarge = 413,

/// <remarks/>
RequestUriTooLong,
RequestUriTooLong = 414,

/// <remarks/>
UnsupportedMediaType,
UnsupportedMediaType = 415,

/// <remarks/>
RequestedRangeNotSatisfiable,
RequestedRangeNotSatisfiable = 416,

/// <remarks/>
ExpectationFailed,
ExpectationFailed = 417,

/// <remarks/>
UpgradeRequired,
UpgradeRequired = 426,

/// <remarks/>
InternalServerError,
InternalServerError = 500,

/// <remarks/>
NotImplemented,
NotImplemented = 501,

/// <remarks/>
BadGateway,
BadGateway = 502,

/// <remarks/>
ServiceUnavailable,
ServiceUnavailable = 503,

/// <remarks/>
GatewayTimeout,
GatewayTimeout = 504,

/// <remarks/>
HttpVersionNotSupported,
HttpVersionNotSupported = 505,
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
Expand Down
Loading