Example #1
0
        private RestEaseParameter BuildValidParameter(string identifier, OpenApiSchema schema, bool required, string description, ParameterLocation? parameterLocation, params string[] extraAttributes)
        {
            var attributes = new List<string>();
            string validIdentifier = CSharpUtils.CreateValidIdentifier(identifier, CasingType.Camel);

            string restEaseParameterAnnotation = parameterLocation != null ? parameterLocation.ToString() : string.Empty;
            string isNullPostfix = !required && Settings.MakeNonRequiredParametersOptional ? " = null" : string.Empty;

            if (parameterLocation == ParameterLocation.Header)
            {
                attributes.Add($"\"{identifier}\"");
            }

            object identifierWithType;
            if (identifier != validIdentifier)
            {
                switch (parameterLocation)
                {
                    case ParameterLocation.Path:
                    case ParameterLocation.Query:
                        attributes.Add($"Name = \"{identifier}\"");
                        break;
                }

                attributes.AddRange(extraAttributes);

                identifierWithType = MapSchema(schema, validIdentifier, !required, false, null);

                return new RestEaseParameter
                {
                    Required = required,
                    Identifier = validIdentifier,
                    SchemaType = schema.GetSchemaType(),
                    SchemaFormat = schema.GetSchemaFormat(),
                    IdentifierWithType = $"{identifierWithType}",
                    IdentifierWithRestEase = $"[{restEaseParameterAnnotation}({string.Join(", ", attributes)})] {identifierWithType}{isNullPostfix}",
                    Summary = description
                };
            }

            string extraAttributesBetweenParentheses = extraAttributes.Length == 0 ? string.Empty : $"({string.Join(", ", extraAttributes)})";
            identifierWithType = MapSchema(schema, identifier, !required, false, null);

            return new RestEaseParameter
            {
                Required = required,
                Identifier = identifier,
                SchemaType = schema.GetSchemaType(),
                SchemaFormat = schema.GetSchemaFormat(),
                IdentifierWithType = $"{identifierWithType}",
                IdentifierWithRestEase = $"[{restEaseParameterAnnotation}{extraAttributesBetweenParentheses}] {identifierWithType}{isNullPostfix}",
                Summary = description
            };
        }
Example #2
0
        public object GetExampleValue(OpenApiSchema schema)
        {
            switch (schema?.GetSchemaType())
            {
            case SchemaType.Boolean:
                return(_settings.ExampleValues.Boolean);

            case SchemaType.Integer:
                return(_settings.ExampleValues.Integer);

            case SchemaType.Number:
                switch (schema?.GetSchemaFormat())
                {
                case SchemaFormat.Float:
                    return(_settings.ExampleValues.Float);

                default:
                    return(_settings.ExampleValues.Double);
                }

            default:
                switch (schema?.GetSchemaFormat())
                {
                case SchemaFormat.Date:
                    return(DateTimeUtils.ToRfc3339Date(_settings.ExampleValues.Date()));

                case SchemaFormat.DateTime:
                    return(DateTimeUtils.ToRfc3339DateTime(_settings.ExampleValues.DateTime()));

                case SchemaFormat.Byte:
                    return(_settings.ExampleValues.Bytes);

                case SchemaFormat.Binary:
                    return(_settings.ExampleValues.Object);

                default:
                    return(_settings.ExampleValues.String);
                }
            }
        }
        public static object GetExampleValue(OpenApiSchema schema)
        {
            switch (schema?.GetSchemaType())
            {
            case SchemaType.Boolean:
                return(true);

            case SchemaType.Integer:
                return(42);

            case SchemaType.Number:
                switch (schema?.GetSchemaFormat())
                {
                case SchemaFormat.Float:
                    return(4.2f);

                default:
                    return(4.2d);
                }

            default:
                switch (schema?.GetSchemaFormat())
                {
                case SchemaFormat.Date:
                    return(DateTimeUtils.ToRfc3339Date(DateTime.UtcNow));

                case SchemaFormat.DateTime:
                    return(DateTimeUtils.ToRfc3339DateTime(DateTime.UtcNow));

                case SchemaFormat.Byte:
                    return(new byte[] { 48, 49, 50 });

                case SchemaFormat.Binary:
                    return("example-object");

                default:
                    return("example-string");
                }
            }
        }
        protected object MapSchema(OpenApiSchema schema, string name, bool isNullable, bool pascalCase, OpenApiSpecVersion?openApiSpecVersion)
        {
            if (schema == null)
            {
                return(null);
            }

            string nameCamelCase = string.IsNullOrEmpty(name) ? string.Empty : $" {(pascalCase ? name.ToPascalCase() : name)}";

            bool   nullableForOpenApi20 = openApiSpecVersion == OpenApiSpecVersion.OpenApi2_0 && Settings.GeneratePrimitivePropertiesAsNullableForOpenApi20;
            string nullable             = nullableForOpenApi20 || isNullable ? "?" : string.Empty;

            switch (schema.GetSchemaType())
            {
            case SchemaType.Array:
                switch (schema.Items.GetSchemaType())
                {
                case SchemaType.Object:
                    return(schema.Items.Reference != null ?
                           $"{MapArrayType(MakeValidModelName(schema.Items.Reference.Id))}{nameCamelCase}" :
                           $"{MapArrayType("object")}{nameCamelCase}");

                case SchemaType.Unknown:
                    return($"{MapArrayType("object")}{nameCamelCase}");

                default:
                    return($"{MapArrayType(MapSchema(schema.Items, null, schema.Items.Nullable, true, openApiSpecVersion))}{nameCamelCase}");
                }

            case SchemaType.Boolean:
                return($"bool{nullable}{nameCamelCase}");

            case SchemaType.Integer:
                switch (schema.GetSchemaFormat())
                {
                case SchemaFormat.Int64:
                    return($"long{nullable}{nameCamelCase}");

                default:
                    return($"int{nullable}{nameCamelCase}");
                }

            case SchemaType.Number:
                switch (schema.GetSchemaFormat())
                {
                case SchemaFormat.Float:
                    return($"float{nullable}{nameCamelCase}");

                default:
                    return($"double{nullable}{nameCamelCase}");
                }

            case SchemaType.String:
                switch (schema.GetSchemaFormat())
                {
                case SchemaFormat.Date:
                case SchemaFormat.DateTime:
                    return($"{DateTime}{nullable}{nameCamelCase}");

                case SchemaFormat.Byte:
                case SchemaFormat.Binary:
                    switch (Settings.ApplicationOctetStreamType)
                    {
                    case ApplicationOctetStreamType.Stream:
                        return($"System.IO.Stream{nameCamelCase}");

                    default:
                        return($"byte[]{nameCamelCase}");
                    }

                default:
                    return($"string{nameCamelCase}");
                }

            case SchemaType.Object:
                var list = new List <string>();

                foreach (var schemaProperty in schema.Properties)
                {
                    var openApiSchema = schemaProperty.Value;
                    if (openApiSchema.GetSchemaType() == SchemaType.Object)
                    {
                        string objectName = pascalCase ? schemaProperty.Key.ToPascalCase() : schemaProperty.Key;
                        string objectType = openApiSchema.Reference != null?MakeValidModelName(openApiSchema.Reference.Id) : "object";

                        list.Add($"{objectType} {objectName}");
                    }
                    else
                    {
                        bool propertyIsNullable = openApiSchema.Nullable ||
                                                  Settings.SupportExtensionXNullable && openApiSchema.TryGetXNullable(out bool x) && x;
                        var property = MapSchema(openApiSchema, schemaProperty.Key, propertyIsNullable, true, openApiSpecVersion);
                        if (property != null && property is string propertyAsString)
                        {
                            list.Add(propertyAsString);
                        }
                    }
                }

                return(list);

            case SchemaType.File:
                switch (Settings.MultipartFormDataFileType)
                {
                case MultipartFormDataFileType.Stream:
                    return($"System.IO.Stream{nameCamelCase}");

                default:
                    return($"byte[]{nameCamelCase}");
                }

            default:
                return(null);
            }
        }