Skip to content

Commit

Permalink
Add required properties to Required array in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
xplicit committed Jun 18, 2017
1 parent 50cf051 commit a969ffd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/ServiceStack.Api.OpenApi/OpenApiService.cs
Expand Up @@ -401,7 +401,8 @@ private void ParseDefinitions(IDictionary<string, OpenApiSchema> schemas, Type s
Type = OpenApiType.Object,
Title = schemaType.Name,
Description = schemaType.GetDescription() ?? GetSchemaTypeName(schemaType),
Properties = new OrderedDictionary<string, OpenApiProperty>()
Properties = new OrderedDictionary<string, OpenApiProperty>(),
Required = new List<string>()
};
parseProperties = schemaType.IsUserType();
}
Expand Down Expand Up @@ -453,27 +454,32 @@ private void ParseDefinitions(IDictionary<string, OpenApiSchema> schemas, Type s

if (apiMembers.Any(x => x.ExcludeInSchema))
continue;
var schemaProp = GetOpenApiProperty(schemas, prop.PropertyType, route, verb);
var schemaProperty = GetOpenApiProperty(schemas, prop.PropertyType, route, verb);
var schemaPropertyName = GetSchemaPropertyName(prop);

schemaProp.Description = prop.GetDescription() ?? apiDoc?.Description;
schemaProperty.Description = prop.GetDescription() ?? apiDoc?.Description;

var propAttr = prop.FirstAttribute<ApiMemberAttribute>();
if (propAttr != null)
{
if (propAttr.DataType != null)
schemaProp.Type = propAttr.DataType;
schemaProperty.Type = propAttr.DataType;

if (propAttr.Format != null)
schemaProp.Format = propAttr.Format;
schemaProperty.Format = propAttr.Format;

schemaProp.Required = propAttr.IsRequired;
if (propAttr.IsRequired)
{
schemaProperty.Required = true;
schema.Required.Add(schemaPropertyName);
}
}

schemaProp.Enum = GetEnumValues(prop.FirstAttribute<ApiAllowableValuesAttribute>());
schemaProperty.Enum = GetEnumValues(prop.FirstAttribute<ApiAllowableValuesAttribute>());

SchemaPropertyFilter?.Invoke(schemaProp);
SchemaPropertyFilter?.Invoke(schemaProperty);

schema.Properties[GetSchemaPropertyName(prop)] = schemaProp;
schema.Properties[schemaPropertyName] = schemaProperty;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/ServiceStack.Api.OpenApi/Specification/OpenApiSchema.cs
@@ -1,4 +1,5 @@
using ServiceStack.Api.OpenApi.Support;
using System.Collections.Generic;
using ServiceStack.Api.OpenApi.Support;
using System.Runtime.Serialization;

namespace ServiceStack.Api.OpenApi.Specification
Expand All @@ -19,6 +20,9 @@ public class OpenApiSchema : OpenApiDataTypeSchema
[DataMember(Name = "example")]
public string Example { get; set; }

[DataMember(Name = "required")]
public new List<string> Required { get; set; }

[DataMember(Name = "allOf")]
public OpenApiSchema AllOf { get; set; }
[DataMember(Name = "properties")]
Expand Down

0 comments on commit a969ffd

Please sign in to comment.