コード例 #1
0
        private static string GetDefaultValueAsStringForSchemaType(OpenApiSchema schema)
        {
            var value = ExampleValueGenerator.GetExampleValue(schema);

            switch (value)
            {
            case string valueAsString:
                return(valueAsString);

            default:
                return(value.ToString());
            }
        }
コード例 #2
0
        private static string MapPathWithParameters(string path, IEnumerable <OpenApiParameter> parameters)
        {
            if (parameters == null)
            {
                return(path);
            }

            string newPath = path;

            foreach (var parameter in parameters)
            {
                newPath = newPath.Replace($"{{{parameter.Name}}}", ExampleValueGenerator.GetExampleValue(parameter.Schema).ToString());
            }

            return(newPath);
        }
コード例 #3
0
        private static IDictionary <string, object> MapHeaders(string responseContentType, IDictionary <string, OpenApiHeader> headers)
        {
            var mappedHeaders = headers.ToDictionary(item => item.Key, item => ExampleValueGenerator.GetExampleValue(null));

            if (!string.IsNullOrEmpty(responseContentType))
            {
                if (!mappedHeaders.ContainsKey("Content-Type"))
                {
                    mappedHeaders.Add("Content-Type", responseContentType);
                }
                else
                {
                    mappedHeaders["Content-Type"] = responseContentType;
                }
            }

            return(mappedHeaders.Keys.Any() ? mappedHeaders : null);
        }
コード例 #4
0
        private object MapSchemaToObject(OpenApiSchema schema, string name = null)
        {
            if (schema == null)
            {
                return(null);
            }

            switch (schema.GetSchemaType())
            {
            case SchemaType.Array:
                var jArray = new JArray();
                for (int i = 0; i < _settings.NumberOfArrayItems; i++)
                {
                    if (schema.Items.Properties.Count > 0)
                    {
                        var arrayItem = new JObject();
                        foreach (var property in schema.Items.Properties)
                        {
                            var objectValue = MapSchemaToObject(property.Value, property.Key);
                            if (objectValue is JProperty jp)
                            {
                                arrayItem.Add(jp);
                            }
                            else
                            {
                                arrayItem.Add(new JProperty(property.Key, objectValue));
                            }
                        }

                        jArray.Add(arrayItem);
                    }
                    else
                    {
                        jArray.Add(MapSchemaToObject(schema.Items, name));
                    }
                }

                if (schema.AllOf.Count > 0)
                {
                    jArray.Add(MapSchemaAllOfToObject(schema));
                }

                return(jArray);

            case SchemaType.Boolean:
            case SchemaType.Integer:
            case SchemaType.Number:
            case SchemaType.String:
                return(_exampleValueGenerator.GetExampleValue(schema));

            case SchemaType.Object:
                var propertyAsJObject = new JObject();
                foreach (var schemaProperty in schema.Properties)
                {
                    propertyAsJObject.Add(MapPropertyAsJObject(schemaProperty.Value, schemaProperty.Key));
                }
                if (schema.AllOf.Count > 0)
                {
                    foreach (var property in schema.AllOf)
                    {
                        foreach (var item in property.Properties)
                        {
                            propertyAsJObject.Add(MapPropertyAsJObject(item.Value, item.Key));
                        }
                    }
                }

                return(name != null ? new JProperty(name, propertyAsJObject) : (JToken)propertyAsJObject);

            default:
                return(null);
            }
        }
コード例 #5
0
        private object MapSchemaToObject(OpenApiSchema schema, string name = null)
        {
            if (schema == null)
            {
                return(null);
            }

            switch (schema.GetSchemaType())
            {
            case SchemaType.Array:
                var jArray = new JArray();
                for (int i = 0; i < _settings.NumberOfArrayItems; i++)
                {
                    if (schema.Items.Properties.Count > 0)
                    {
                        var arrayItem = new JObject();
                        foreach (var property in schema.Items.Properties)
                        {
                            var objectValue = MapSchemaToObject(property.Value, property.Key);
                            if (objectValue is JProperty jp)
                            {
                                arrayItem.Add(jp);
                            }
                            else
                            {
                                arrayItem.Add(new JProperty(property.Key, objectValue));
                            }
                        }

                        jArray.Add(arrayItem);
                    }
                    else
                    {
                        jArray.Add(MapSchemaToObject(schema.Items, name));
                    }
                }

                return(jArray);

            case SchemaType.Boolean:
            case SchemaType.Integer:
            case SchemaType.Number:
            case SchemaType.String:
                return(_exampleValueGenerator.GetExampleValue(schema));

            case SchemaType.Object:
                var propertyAsJObject = new JObject();
                foreach (var schemaProperty in schema.Properties)
                {
                    string propertyName  = schemaProperty.Key;
                    var    openApiSchema = schemaProperty.Value;
                    if (openApiSchema.GetSchemaType() == SchemaType.Object)
                    {
                        var mapped = MapSchemaToObject(schemaProperty.Value, schemaProperty.Key);
                        if (mapped is JProperty jp)
                        {
                            propertyAsJObject.Add(jp);
                        }
                    }
                    else
                    {
                        bool propertyIsNullable = openApiSchema.Nullable || (openApiSchema.TryGetXNullable(out bool x) && x);

                        propertyAsJObject.Add(new JProperty(propertyName, _exampleValueGenerator.GetExampleValue(openApiSchema)));
                    }
                }

                return(name != null ? new JProperty(name, propertyAsJObject) : (JToken)propertyAsJObject);

            default:
                return(null);
            }
        }