コード例 #1
0
        private EnumBuilder FindMatchingEnumBuilder(AnonymousEnumBuilder anonEnumBuilder)
        {
            EnumBuilder enumBuilder = null;

            foreach (var realEnumBuilder in _generatedTypes.Values.IsType <EnumBuilder>())
            {
                if (anonEnumBuilder.Fields.Count != realEnumBuilder.Fields.Count)
                {
                    continue;
                }

                if (anonEnumBuilder.IsFlags != realEnumBuilder.IsFlags)
                {
                    continue;
                }

                var pairs = anonEnumBuilder.Fields.OrderBy(f => f.Name).Zip(realEnumBuilder.Fields.OrderBy(f => f.Name), (a, b) => new Tuple <EnumFieldBuilder, EnumFieldBuilder>(a, b));

                if (!pairs.All(x => x.Item1.Name == x.Item2.Name && x.Item1.NumericValue == x.Item2.NumericValue))
                {
                    continue;
                }

                if (enumBuilder != null)
                {
                    throw new Exception("Two enums found.");
                }

                enumBuilder = realEnumBuilder;
            }

            return(enumBuilder);
        }
コード例 #2
0
        public TypeReference ProcessPropertyType(OpenApiSchema jsonPropertyValue)
        {
            string jsonDollarRef = jsonPropertyValue.Reference?.ReferenceV3;

            if (jsonDollarRef != null)
            {
                return(new TypeReference(jsonDollarRef));
            }
            var jsonType     = jsonPropertyValue.Type;
            var jsonFormat   = jsonPropertyValue.Format;
            var jsonNullable = jsonPropertyValue.Nullable;

            jsonPropertyValue.Extensions.TryGetValue("x-enum-reference", out var enumReferenceTemp);

            if (jsonType == "boolean")
            {
                return(jsonNullable ? typeof(bool?) : typeof(bool));
            }

            if (jsonType == "integer")
            {
                if (jsonPropertyValue.Enum != null && jsonPropertyValue.Enum.Count > 0)
                {
                    AnonymousEnumBuilder enumBuilder = new AnonymousEnumBuilder();
                    enumBuilder.IsFlags        = false;
                    enumBuilder.UnderlyingType = GetEnumUnderlyingType(jsonFormat);

                    ProcessEnumFields(jsonPropertyValue, enumBuilder);

                    if (jsonNullable)
                    {
                        return new NullableEnumBuilder()
                               {
                                   EnumBuilder = enumBuilder
                               }
                    }
                    ;

                    return(enumBuilder);
                }

                if (enumReferenceTemp is OpenApiObject enumReference)
                {
                    jsonDollarRef = ((OpenApiString)enumReference["$ref"]).Value;
                    return(new TypeReference(jsonDollarRef));
                }

                if (jsonFormat == "byte")
                {
                    return(jsonNullable ? typeof(byte?) : typeof(byte));
                }
                else if (jsonFormat == "sbyte")
                {
                    return(jsonNullable ? typeof(sbyte?) : typeof(sbyte));
                }
                else if (jsonFormat == "int16")
                {
                    return(jsonNullable ? typeof(short?) : typeof(short));
                }
                else if (jsonFormat == "uint16")
                {
                    return(jsonNullable ? typeof(ushort?) : typeof(ushort));
                }
                else if (jsonFormat == "int32")
                {
                    return(jsonNullable ? typeof(int?) : typeof(int));
                }
                else if (jsonFormat == "uint32")
                {
                    return(jsonNullable ? typeof(uint?) : typeof(uint));
                }
                else if (jsonFormat == "int64")
                {
                    return(jsonNullable ? typeof(long?) : typeof(long));
                }
                else if (jsonFormat == "uint64")
                {
                    return(jsonNullable ? typeof(ulong?) : typeof(ulong));
                }
            }
            else if (jsonType == "number")
            {
                if (jsonFormat == "float")
                {
                    return(jsonNullable ? typeof(decimal?) : typeof(decimal));                     //return jsonNullable ? typeof(float?) : typeof(float);
                }
                else if (jsonFormat == "double")
                {
                    return(jsonNullable ? typeof(decimal?) : typeof(decimal)); //return jsonNullable ? typeof(double?) : typeof(double);
                }
                else                                                           //if (jsonFormat == "decimal")
                {
                    return(jsonNullable ? typeof(decimal?) : typeof(decimal));
                }
            }
            else if (jsonType == "string")
            {
                if (jsonFormat == "date-time")
                {
                    return(jsonNullable ? typeof(DateTime?) : typeof(DateTime));
                }
                else if (jsonFormat == "byte")
                {
                    return(jsonNullable ? typeof(byte?) : typeof(byte));
                }
                else
                {
                    return(typeof(string));
                }
            }
            else if (jsonType == "object")
            {
                if (jsonPropertyValue.Extensions.TryGetValue("x-dictionary-key", out var dictionaryKeyTemp))
                {
                    var dictionaryKey   = TranslateDictionaryKeyExtensionToSchema(dictionaryKeyTemp);
                    var dictionaryValue = jsonPropertyValue.AdditionalProperties;

                    return(new DictionaryBuilder()
                    {
                        KeyType = ProcessPropertyType(dictionaryKey),
                        ItemType = ProcessPropertyType(dictionaryValue)
                    });
                }

                var allOf = jsonPropertyValue.AllOf;
                if (allOf != null && allOf.Count > 0)
                {
                    return(ProcessPropertyType(allOf[0]));
                }

                return(default);