public OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var jsonDictionaryContract = (JsonDictionaryContract)_jsonContractResolver.ResolveContract(type); var keyType = jsonDictionaryContract.DictionaryKeyType ?? typeof(object); var valueType = jsonDictionaryContract.DictionaryValueType ?? typeof(object); if (keyType.IsEnum) { // This is a special case where we can include named properties based on the enum values return(new OpenApiSchema { Type = "object", Properties = jsonDictionaryContract.DictionaryKeyType.GetEnumNames() .ToDictionary( name => name, name => _schemaGenerator.GenerateSchemaFor(valueType, schemaRepository) ) }); } return(new OpenApiSchema { Type = "object", AdditionalPropertiesAllowed = true, AdditionalProperties = _schemaGenerator.GenerateSchemaFor(valueType, schemaRepository) }); }
private OpenApiParameter GenerateParameter( ApiDescription apiDescription, ApiParameterDescription apiParameter, SchemaRepository schemaRepository) { apiParameter.GetAdditionalMetadata( apiDescription, out ParameterInfo parameterInfo, out PropertyInfo propertyInfo, out IEnumerable <object> parameterOrPropertyAttributes); var name = _options.DescribeAllParametersInCamelCase ? apiParameter.Name.ToCamelCase() : apiParameter.Name; var location = ParameterLocationMap.ContainsKey(apiParameter.Source) ? ParameterLocationMap[apiParameter.Source] : ParameterLocation.Query; var isRequired = (apiParameter.IsFromPath()) || parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType())); var schema = (apiParameter.ModelMetadata != null) ? _schemaGenerator.GenerateSchemaFor(apiParameter.Type, schemaRepository) : new OpenApiSchema { Type = "string" }; // If it corresponds to an optional action parameter, assign the default value if (parameterInfo?.DefaultValue != null && schema.Reference == null) { schema.Default = OpenApiAnyFactory.TryCreateFrom(parameterInfo.DefaultValue, out IOpenApiAny openApiAny) ? openApiAny : null; } var parameter = new OpenApiParameter { Name = name, In = location, Required = isRequired, Schema = schema }; var filterContext = new ParameterFilterContext(apiParameter, _schemaGenerator, schemaRepository, parameterInfo, propertyInfo); foreach (var filter in _options.ParameterFilters) { filter.Apply(parameter, filterContext); } return(parameter); }
public OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var jsonObjectContract = (JsonObjectContract)_jsonContractResolver.ResolveContract(type); var requiredPropertyNames = new List <string>(); var properties = new Dictionary <string, OpenApiSchema>(); foreach (var jsonProperty in jsonObjectContract.Properties) { if (jsonProperty.Ignored) { continue; } var attributes = jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo) ? memberInfo.GetCustomAttributes(true) : new object[] { }; if (_options.IgnoreObsoleteProperties && attributes.OfType <ObsoleteAttribute>().Any()) { continue; } if (jsonProperty.Required == Required.AllowNull || jsonProperty.Required == Required.Always || attributes.OfType <RequiredAttribute>().Any()) { requiredPropertyNames.Add(jsonProperty.PropertyName); } properties.Add(jsonProperty.PropertyName, GeneratePropertySchema(jsonProperty, attributes, schemaRepository)); } var additionalProperties = (jsonObjectContract.ExtensionDataValueType != null) ? _schemaGenerator.GenerateSchemaFor(jsonObjectContract.ExtensionDataValueType, schemaRepository) : null; var schema = new OpenApiSchema { Type = "object", Properties = properties, Required = new SortedSet <string>(requiredPropertyNames), AdditionalPropertiesAllowed = (additionalProperties != null), AdditionalProperties = additionalProperties }; return(schema); }
public OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var subTypes = _options.SubTypesResolver(type); return(new OpenApiSchema { OneOf = subTypes .Select(subType => _schemaGenerator.GenerateSchemaFor(subType, schemaRepository)) .ToList() }); }
public OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var jsonArrayContract = (JsonArrayContract)_jsonContractResolver.ResolveContract(type); return(new OpenApiSchema { Type = "array", Items = _schemaGenerator.GenerateSchemaFor(jsonArrayContract.CollectionItemType, schemaRepository), UniqueItems = jsonArrayContract.UnderlyingType.IsSet() ? (bool?)true : null }); }