private static Rdfs.IProperty GetMappingProperty(DescriptionContext context, ParameterInfo parameter) { IClass description = context[context.Type]; Rdfs.IProperty resultCandidate = null; IResource parameterType = null; foreach (var supportedProperty in description.SupportedProperties) { if (StringComparer.OrdinalIgnoreCase.Equals(supportedProperty.Property.Label, parameter.Name)) { return(supportedProperty.Property); } if (parameterType == null) { parameterType = (context.ContainsType(parameter.ParameterType) ? context[parameter.ParameterType] : context.BuildTypeDescription()); } if (supportedProperty.Property.Range.Any(range => (range.Id == parameterType.Id) || ((range is IClass) && (((IClass)range).SubClassOf.Any(subClass => subClass.Id == parameterType.Id))))) { resultCandidate = supportedProperty.Property; } } return(resultCandidate); }
private IOperation BuildOperation(DescriptionContext context, OperationInfo <Verb> operation, out IIriTemplate template) { IOperation result = operation.AsOperation(context.ApiDocumentation); result.Label = operation.UnderlyingMethod.Name; result.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod); result.Method.Add(operation.ProtocolSpecificCommand.ToString()); template = BuildTemplate(context, operation, result); bool requiresRdf = context.RequiresRdf(SpecializationType); bool isRdfRequired = requiresRdf; var arguments = operation.Arguments.Where(parameter => parameter.Source is FromBodyAttribute).Select(parameter => parameter.Parameter); var results = operation.Results.Where(output => output.Target is ToBodyAttribute).Select(parameter => parameter.Parameter); foreach (var value in arguments) { var expected = (context.ContainsType(value.ParameterType) ? context[value.ParameterType] : context.ForType(value.ParameterType).BuildTypeDescription(out isRdfRequired)); expected = context.SubClass(expected, value.ParameterType); expected.Label = value.Name ?? "instance"; expected.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod, value); result.Expects.Add(expected); requiresRdf |= isRdfRequired; } foreach (var value in results) { var returned = (context.ContainsType(value.ParameterType) ? context[value.ParameterType] : context.ForType(value.ParameterType).BuildTypeDescription(out isRdfRequired)); returned = context.SubClass(returned, value.ParameterType); returned.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod, value); result.Returns.Add(returned); requiresRdf |= isRdfRequired; } BuildStatusCodes(result, operation); BuildOperationMediaType(result, result.Returns, operation, requiresRdf); BuildOperationMediaType(result, result.Expects, operation, requiresRdf); if (!result.MediaTypes.Any()) { BuildOperationMediaType(result, requiresRdf); } return(result); }
/// <inheritdoc /> public IClass BuildTypeDescription(DescriptionContext context, out bool requiresRdf) { if (context == null) { throw new ArgumentNullException("context"); } if (context.ContainsType(context.Type)) { return(context.BuildTypeDescription(out requiresRdf)); } if (System.Reflection.TypeExtensions.IsEnumerable(context.Type)) { if (context.Type.IsList()) { return(CreateListDefinition(context, out requiresRdf, context.Type.IsGenericList())); } return(CreateCollectionDefinition(context, out requiresRdf, context.Type.IsGenericEnumerable())); } requiresRdf = false; Type itemType = context.Type.GetItemType(); if (TypeDescriptions.ContainsKey(itemType)) { return(BuildDatatypeDescription(context, TypeDescriptions[itemType])); } var classUri = itemType.MakeUri(); if (typeof(IEntity).IsAssignableFrom(itemType)) { classUri = context.ApiDocumentation.Context.Mappings.MappingFor(itemType).Classes.Select(item => item.Uri).FirstOrDefault() ?? classUri; requiresRdf = true; } IClass result = context.ApiDocumentation.Context.Create <IClass>(classUri); result.Label = itemType.MakeTypeName(false, true); result.Description = _xmlDocProvider.GetDescription(itemType); if (typeof(EntityId).IsAssignableFrom(itemType)) { context.Describe(result, requiresRdf); return(result); } context.Prescribe(result, requiresRdf); SetupProperties(context.ForType(itemType), result); context.Describe(result, requiresRdf); return(result); }
private IClass CreateCollectionDefinition(DescriptionContext context, out bool requiresRdf, out Type itemType, bool isGeneric = true) { var result = CreateEnumerableDefinition(context, context.ApiDocumentation.Context.Mappings.MappingFor <ICollection>().Classes.First().Uri, out requiresRdf, out itemType, isGeneric); if (!isGeneric) { return(result); } var memberType = (context.ContainsType(itemType) ? context[itemType] : BuildTypeDescription(context.ForType(itemType), out requiresRdf)); result.SubClassOf.Add(result.CreateRestriction(context.ApiDocumentation.Context.Mappings.MappingFor <ICollection>().PropertyFor("Members").Uri, memberType)); return(result); }
/// <inheritdoc /> public IClass BuildTypeDescription(DescriptionContext context, out bool requiresRdf) { if (context == null) { throw new ArgumentNullException("context"); } if (context.ContainsType(context.Type)) { return context.BuildTypeDescription(out requiresRdf); } if (System.Reflection.TypeExtensions.IsEnumerable(context.Type)) { if (context.Type.IsList()) { return CreateListDefinition(context, out requiresRdf, context.Type.IsGenericList()); } return CreateCollectionDefinition(context, out requiresRdf, context.Type.IsGenericEnumerable()); } requiresRdf = false; Type itemType = context.Type.GetItemType(); if (TypeDescriptions.ContainsKey(itemType)) { return BuildDatatypeDescription(context, TypeDescriptions[itemType]); } var classUri = itemType.MakeUri(); if (typeof(IEntity).IsAssignableFrom(itemType)) { classUri = context.ApiDocumentation.Context.Mappings.MappingFor(itemType).Classes.Select(item => item.Uri).FirstOrDefault() ?? classUri; requiresRdf = true; } IClass result = context.ApiDocumentation.Context.Create<IClass>(classUri); result.Label = itemType.MakeTypeName(false, true); result.Description = _xmlDocProvider.GetDescription(itemType); if (typeof(EntityId).IsAssignableFrom(itemType)) { context.Describe(result, requiresRdf); return result; } context.Prescribe(result, requiresRdf); SetupProperties(context.ForType(itemType), result); context.Describe(result, requiresRdf); return result; }
private IClass CreateListDefinition(DescriptionContext context, out bool requiresRdf, bool isGeneric = true) { Type itemType; var result = CreateCollectionDefinition(context, out requiresRdf, out itemType, isGeneric); if (!isGeneric) { return(result); } var memberType = (context.ContainsType(itemType) ? context[itemType] : BuildTypeDescription(context.ForType(itemType), out requiresRdf)); result.SubClassOf.Add(context.ApiDocumentation.Context.Create <IClass>(Rdf.List)); result.SubClassOf.Add(result.CreateRestriction(Rdf.first, memberType)); result.SubClassOf.Add(result.CreateRestriction(Rdf.rest, result)); return(result); }
private ISupportedProperty BuildSupportedProperty(DescriptionContext context, IClass @class, Type declaringType, PropertyInfo property) { var propertyId = GetSupportedPropertyId(property, declaringType); var propertyUri = (!typeof(IEntity).IsAssignableFrom(context.Type) ? @class.Id.Uri.AddName(property.Name) : context.ApiDocumentation.Context.Mappings.MappingFor(context.Type).PropertyFor(property.Name).Uri); var result = context.ApiDocumentation.Context.Create <ISupportedProperty>(propertyId); result.Readable = property.CanRead; result.Writeable = property.CanWrite; result.Required = (property.PropertyType.IsValueType) || (property.GetCustomAttribute <RequiredAttribute>() != null); var isKeyProperty = (property.GetCustomAttribute <KeyAttribute>() != null) || (property.ImplementsGeneric(typeof(IControlledEntity <>), "Key")); result.Property = (isKeyProperty ? context.ApiDocumentation.Context.Create <IInverseFunctionalProperty>(propertyUri) : context.ApiDocumentation.Context.Create <IProperty>(propertyUri)); result.Property.Label = property.Name; result.Property.Description = _xmlDocProvider.GetDescription(property); result.Property.Domain.Add(@class); IClass propertyType; var itemPropertyType = (System.Reflection.TypeExtensions.IsEnumerable(property.PropertyType) ? property.PropertyType : property.PropertyType.GetItemType()); if (!context.ContainsType(itemPropertyType)) { bool requiresRdf; var childContext = context.ForType(itemPropertyType); propertyType = BuildTypeDescription(childContext, out requiresRdf); childContext.Describe(propertyType, requiresRdf); } else { propertyType = context[itemPropertyType]; } result.Property.Range.Add(propertyType); if ((System.Reflection.TypeExtensions.IsEnumerable(property.PropertyType)) && (property.PropertyType != typeof(byte[]))) { return(result); } @class.SubClassOf.Add(@class.CreateRestriction(result.Property, 1u)); return(result); }
private ISupportedProperty BuildSupportedProperty(DescriptionContext context, IClass @class, Type declaringType, PropertyInfo property) { var propertyId = GetSupportedPropertyId(property, declaringType); var propertyUri = (!typeof(IEntity).IsAssignableFrom(context.Type) ? @class.Id.Uri.AddName(property.Name) : context.ApiDocumentation.Context.Mappings.MappingFor(context.Type).PropertyFor(property.Name).Uri); var result = context.ApiDocumentation.Context.Create<ISupportedProperty>(propertyId); result.Readable = property.CanRead; result.Writeable = property.CanWrite; result.Required = (property.PropertyType.IsValueType) || (property.GetCustomAttribute<RequiredAttribute>() != null); var isKeyProperty = (property.GetCustomAttribute<KeyAttribute>() != null) || (property.ImplementsGeneric(typeof(IControlledEntity<>), "Key")); result.Property = (isKeyProperty ? context.ApiDocumentation.Context.Create<IInverseFunctionalProperty>(propertyUri) : context.ApiDocumentation.Context.Create<IProperty>(propertyUri)); result.Property.Label = property.Name; result.Property.Description = _xmlDocProvider.GetDescription(property); result.Property.Domain.Add(@class); IClass propertyType; var itemPropertyType = (System.Reflection.TypeExtensions.IsEnumerable(property.PropertyType) ? property.PropertyType : property.PropertyType.GetItemType()); if (!context.ContainsType(itemPropertyType)) { bool requiresRdf; var childContext = context.ForType(itemPropertyType); propertyType = BuildTypeDescription(childContext, out requiresRdf); childContext.Describe(propertyType, requiresRdf); } else { propertyType = context[itemPropertyType]; } result.Property.Range.Add(propertyType); if ((System.Reflection.TypeExtensions.IsEnumerable(property.PropertyType)) && (property.PropertyType != typeof(byte[]))) { return result; } @class.SubClassOf.Add(@class.CreateRestriction(result.Property, 1u)); return result; }
private IClass CreateCollectionDefinition(DescriptionContext context, out bool requiresRdf, out Type itemType, bool isGeneric = true) { var result = CreateEnumerableDefinition(context, context.ApiDocumentation.Context.Mappings.MappingFor<ICollection>().Classes.First().Uri, out requiresRdf, out itemType, isGeneric); if (!isGeneric) { return result; } var memberType = (context.ContainsType(itemType) ? context[itemType] : BuildTypeDescription(context.ForType(itemType), out requiresRdf)); result.SubClassOf.Add(result.CreateRestriction(context.ApiDocumentation.Context.Mappings.MappingFor<ICollection>().PropertyFor("Members").Uri, memberType)); return result; }
private IClass CreateListDefinition(DescriptionContext context, out bool requiresRdf, bool isGeneric = true) { Type itemType; var result = CreateCollectionDefinition(context, out requiresRdf, out itemType, isGeneric); if (!isGeneric) { return result; } var memberType = (context.ContainsType(itemType) ? context[itemType] : BuildTypeDescription(context.ForType(itemType), out requiresRdf)); result.SubClassOf.Add(context.ApiDocumentation.Context.Create<IClass>(Rdf.List)); result.SubClassOf.Add(result.CreateRestriction(Rdf.first, memberType)); result.SubClassOf.Add(result.CreateRestriction(Rdf.rest, result)); return result; }
private IOperation BuildOperation(DescriptionContext context, OperationInfo<Verb> operation, out IIriTemplate template) { IOperation result = operation.AsOperation(context.ApiDocumentation); result.Label = operation.UnderlyingMethod.Name; result.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod); result.Method.Add(operation.ProtocolSpecificCommand.ToString()); template = BuildTemplate(context, operation, result); bool requiresRdf = context.RequiresRdf(SpecializationType); bool isRdfRequired = requiresRdf; var arguments = operation.Arguments.Where(parameter => parameter.Source is FromBodyAttribute).Select(parameter => parameter.Parameter); var results = operation.Results.Where(output => output.Target is ToBodyAttribute).Select(parameter => parameter.Parameter); foreach (var value in arguments) { var expected = (context.ContainsType(value.ParameterType) ? context[value.ParameterType] : context.ForType(value.ParameterType).BuildTypeDescription(out isRdfRequired)); expected = context.SubClass(expected, value.ParameterType); expected.Label = value.Name ?? "instance"; expected.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod, value); result.Expects.Add(expected); requiresRdf |= isRdfRequired; } foreach (var value in results) { var returned = (context.ContainsType(value.ParameterType) ? context[value.ParameterType] : context.ForType(value.ParameterType).BuildTypeDescription(out isRdfRequired)); returned = context.SubClass(returned, value.ParameterType); returned.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod, value); result.Returns.Add(returned); requiresRdf |= isRdfRequired; } BuildStatusCodes(result, operation); BuildOperationMediaType(result, result.Returns, operation, requiresRdf); BuildOperationMediaType(result, result.Expects, operation, requiresRdf); if (!result.MediaTypes.Any()) { BuildOperationMediaType(result, requiresRdf); } return result; }
private static Rdfs.IProperty GetMappingProperty(DescriptionContext context, ParameterInfo parameter) { IClass description = context[context.Type]; Rdfs.IProperty resultCandidate = null; IResource parameterType = null; foreach (var supportedProperty in description.SupportedProperties) { if (StringComparer.OrdinalIgnoreCase.Equals(supportedProperty.Property.Label, parameter.Name)) { return supportedProperty.Property; } if (parameterType == null) { parameterType = (context.ContainsType(parameter.ParameterType) ? context[parameter.ParameterType] : context.BuildTypeDescription()); } if (supportedProperty.Property.Range.Any(range => (range.Id == parameterType.Id) || ((range is IClass) && (((IClass)range).SubClassOf.Any(subClass => subClass.Id == parameterType.Id))))) { resultCandidate = supportedProperty.Property; } } return resultCandidate; }