public override OpenApiSchema CreateDefinitionSchema(Type type, SchemaRepository schemaRepository)
        {
            var jsonContract = _contractResolver.ResolveContract(type);

            var isNullable = type.IsNullable(out Type innerType);

            var enumType = isNullable
                ? innerType
                : type;

            var stringEnumConverter = (jsonContract.Converter as StringEnumConverter)
                                      ?? _serializerSettings.Converters.OfType <StringEnumConverter>().FirstOrDefault();

            // Temporary shim to support obsolete config options
            if (stringEnumConverter == null && _generatorOptions.DescribeAllEnumsAsStrings)
            {
                stringEnumConverter = new StringEnumConverter(_generatorOptions.DescribeStringEnumsInCamelCase);
            }

            var schema = (stringEnumConverter != null)
                ? EnumTypeMap[typeof(string)]()
                : EnumTypeMap[enumType.GetEnumUnderlyingType()]();

            if (stringEnumConverter != null)
            {
                schema.Enum = enumType.GetMembers(BindingFlags.Public | BindingFlags.Static)
                              .Select(member =>
                {
                    var memberAttribute = member.GetCustomAttributes <EnumMemberAttribute>().FirstOrDefault();
                    var stringValue     = GetConvertedEnumName(stringEnumConverter, (memberAttribute?.Value ?? member.Name), (memberAttribute?.Value != null));
                    return(OpenApiAnyFactory.CreateFor(schema, stringValue));
                })
                              .ToList();
            }
            else
            {
                schema.Enum = enumType.GetEnumValues()
                              .Cast <object>()
                              .Select(value => OpenApiAnyFactory.CreateFor(schema, value))
                              .ToList();
            }

            schema.Nullable = isNullable;

            return(schema);
        }
Esempio n. 2
0
        private OpenApiSchema GeneratePrimitiveSchema(DataContract dataContract)
        {
            var schema = new OpenApiSchema
            {
                Type   = dataContract.DataType.ToString().ToLower(CultureInfo.InvariantCulture),
                Format = dataContract.Format
            };

            if (dataContract.EnumValues != null)
            {
                schema.Enum = dataContract.EnumValues
                              .Distinct()
                              .Select(value => OpenApiAnyFactory.CreateFor(schema, value))
                              .ToList();
            }

            return(schema);
        }
Esempio n. 3
0
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
            {
                return;
            }

            var apiVersionParam            = operation.Parameters.FirstOrDefault(p => p.Name == "api-version");
            var apiVersionParamDescription = context.ApiDescription.ParameterDescriptions.FirstOrDefault(p => p.Name == "api-version");

            if (apiVersionParam != null)
            {
                if (apiVersionParam.Schema.Default == null && apiVersionParamDescription.DefaultValue != null)
                {
                    apiVersionParam.Schema.Default = OpenApiAnyFactory.CreateFor(apiVersionParam.Schema, apiVersionParamDescription.DefaultValue);
                }
            }
        }
Esempio n. 4
0
        private void ApplyParameterMetadata(OpenApiSchema schema, Type type, ParameterInfo parameterInfo)
        {
            if (schema.Reference != null && _generatorOptions.UseAllOfToExtendReferenceSchemas)
            {
                schema.AllOf = new[] { new OpenApiSchema {
                                           Reference = schema.Reference
                                       } };
                schema.Reference = null;
            }

            if (schema.Reference == null)
            {
                schema.Nullable = type.IsReferenceOrNullableType();

                schema.ApplyCustomAttributes(parameterInfo.GetCustomAttributes());

                if (parameterInfo.HasDefaultValue)
                {
                    schema.Default = OpenApiAnyFactory.CreateFor(schema, parameterInfo.DefaultValue);
                }
            }
        }
Esempio n. 5
0
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
            {
                operation.Parameters = new List <OpenApiParameter>();
            }

            var schema = new OpenApiSchema
            {
                Type = "String"
            };

            operation.Parameters.Add(new OpenApiParameter
            {
                Name        = "X-Tenant-Id",
                Description = "Tenant id for the request",
                In          = ParameterLocation.Header,
                Required    = false,
                Example     = OpenApiAnyFactory.CreateFor(schema, "first-tenant"),
                Schema      = schema
            });
        }
Esempio n. 6
0
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (schema.Properties == null || schema.Properties.Count == 0)
            {
                return;
            }

            //if (context.ApiModel == null || context.ApiModel.Type == null)
            //    return;

            var propertyInfos = context.Type.GetProperties();

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                var attribute = propertyInfo.GetCustomAttribute <ExampleAttribute>();
                if (attribute != null)
                {
                    foreach (KeyValuePair <string, OpenApiSchema> property in schema.Properties)
                    {
                        if (ToCamelCase(propertyInfo.Name) == property.Key)
                        {
                            if (attribute.Value == null)
                            {
                                property.Value.Example = new OpenApiNull();
                                break;
                            }

                            var ss = OpenApiAnyFactory.CreateFor(property.Value, attribute.Value);

                            property.Value.Example = OpenApiAnyFactory.CreateFor(property.Value, attribute.Value);

                            //property.Value.Example = OpenApiAnyFactory.TryCreateFor(property.Value, attribute.Value, out IOpenApiAny openApiAny)
                            //    ? openApiAny
                            //    : null;
                        }
                    }
                }
            }
        }
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
            {
                operation.Parameters = new List <OpenApiParameter>();
            }

            operation.Parameters.Add(new OpenApiParameter
            {
                Name        = "Accept-Language",
                In          = ParameterLocation.Header,
                Description = "Supported languages",
                Schema      = new OpenApiSchema
                {
                    Default = new OpenApiString("en"),
                    Type    = "string",
                    Enum    = Localization.SupportedCultures
                              .Select(c => OpenApiAnyFactory.CreateFor(new OpenApiSchema {
                        Type = "string"
                    }, c)).ToList()
                },
                Required = false
            });
        }