Skip to content

Commit

Permalink
Address Pull Request Feedback
Browse files Browse the repository at this point in the history
- Rename IOdataAnnotable -> IODataAnnotable
- Merged SampleJsonGenerator into ODataParser
- Added command line parameter for resource file template
- Extended ParameterDataType to support getting the MarkDown
-
  • Loading branch information
PetRich-MSFT committed Jun 5, 2017
1 parent e81f9e4 commit 40b7a3b
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 118 deletions.
2 changes: 2 additions & 0 deletions ApiDocs.Console/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,5 +549,7 @@ public enum PublishFormat

class GenerateDocsOptions : CheckMetadataOptions
{
[Option("resource-template", HelpText = "Specifies the path to a mustache template file to use for generating documentation for resources (complex and entity types)", Required = false)]
public string ResourceTemplateFile { get; set; }
}
}
2 changes: 1 addition & 1 deletion ApiDocs.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ private static async Task<bool> GenerateDocsAsync(GenerateDocsOptions options)

FancyConsole.WriteLine(FancyConsole.ConsoleSuccessColor, " found {0} schema definitions: {1}", ef.DataServices.Schemas.Count, (from s in ef.DataServices.Schemas select s.Namespace).ComponentsJoinedByString(", "));

DocumentationGenerator docGenerator = new DocumentationGenerator();
DocumentationGenerator docGenerator = new DocumentationGenerator(options.ResourceTemplateFile);
docGenerator.GenerateDocumentationFromEntityFrameworkAsync(ef, options.DocumentationSetPath);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void AddProperty(ComplexType ct, string name, string type = "Edm.Strin

protected ComplexType GetComplexType(Schema schema, string name, string inlineDescription = null, string schemaDescription = null)
{
ComplexType ct = new ComplexType { Name = name };
ComplexType ct = new ComplexType { Name = name, Namespace = schema.Namespace};
if (inlineDescription != null)
{
ct.Annotation.Add(this.GetDescriptionAnnotation(inlineDescription));
Expand All @@ -117,7 +117,7 @@ protected Annotation GetDescriptionAnnotation(string description)

protected EntityType GetEntityType(Schema schema, string name, string inlineDescription = null, string schemaDescription = null)
{
EntityType et = new EntityType { Name = name };
EntityType et = new EntityType { Name = name, Namespace = schema.Namespace};
if (inlineDescription != null)
{
et.Annotation.Add(this.GetDescriptionAnnotation(inlineDescription));
Expand Down
31 changes: 31 additions & 0 deletions ApiDocs.DocumentationGeneration.UnitTests/PropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ public void PropertyTableEntryIsCorrectlyFormattedForComplexType()
Assert.IsTrue(markDown.Contains(expected), "Generated markdown should contain '{0}' Actual:\n{1}", expected, markDown);
}

[Test]
public void PropertyTableEntryIsCorrectlyFormattedForComplexTypeCollection()
{
string propertyType = "propertyComplexType";
ComplexType ct = this.GetComplexType(this.schema, "test");
ComplexType ct2 = this.GetComplexType(this.schema, "propertyComplexType");
string description = "Inline property description";
string propertyName = "ComplexProperty";
this.AddProperty(ct, propertyName, type: $"Collection({this.schema.Namespace}.{propertyType})", inlineDescription: description);

string expected = $"|{propertyName}|[{propertyType}]({propertyType}.md) collection|{description}|";

string markDown = this.documentationGenerator.GetMarkDownForType(this.entityFramework, ct);

Assert.IsTrue(markDown.Contains(expected), "Generated markdown should contain '{0}' Actual:\n{1}", expected, markDown);
}

[Test]
public void PropertyTableEntryIsCorrectlyFormattedForPrimitiveType()
{
Expand All @@ -63,5 +80,19 @@ public void PropertyTableEntryIsCorrectlyFormattedForPrimitiveType()

Assert.IsTrue(markDown.Contains(expected), "Generated markdown should contain '{0}' Actual:\n{1}", expected, markDown);
}

[Test]
public void PropertyTableEntryIsCorrectlyFormattedForPrimitiveTypeCollection()
{
string description = "Inline property description";
string propertyType = "Boolean";
string propertyName = "PrimitiveProperty";
string expected = $"|{propertyName}|{propertyType} collection|{description}|";
ComplexType ct = this.GetComplexType(this.schema, "Test");
this.AddProperty(ct, propertyName, type: $"Collection(Edm.{propertyType})", inlineDescription: description);
string markDown = this.documentationGenerator.GetMarkDownForType(this.entityFramework, ct);

Assert.IsTrue(markDown.Contains(expected), "Generated markdown should contain '{0}' Actual:\n{1}", expected, markDown);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
<DesignTime>True</DesignTime>
<DependentUpon>Templates.resx</DependentUpon>
</Compile>
<Compile Include="SampleJsonGenerator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config">
Expand Down
24 changes: 20 additions & 4 deletions ApiDocs.DocumentationGeneration/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,29 @@ public class DocumentationGenerator
{
private readonly Generator resourceMarkDownGenerator;

public DocumentationGenerator()
public DocumentationGenerator() : this(null)
{
}

public DocumentationGenerator(string resourceTemplateFile)
{
string template = null;
using (var ms = new MemoryStream(Templates.resource))
using (var reader = new StreamReader(ms))

if (string.IsNullOrWhiteSpace(resourceTemplateFile))
{
using (var ms = new MemoryStream(Templates.resourceMarkDown))
using (var reader = new StreamReader(ms))
{
template = reader.ReadToEnd();
}
}
else
{
template = reader.ReadToEnd();
if (!File.Exists(resourceTemplateFile))
{
throw new FileNotFoundException($"Resource MarkDown template not found", resourceTemplateFile);
}
template = File.ReadAllText(resourceTemplateFile);
}

if (string.IsNullOrWhiteSpace(template))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static List<Annotation> GetAnnotationsForProperty(this Property targetPro
return new List<Annotation>();
}

public static List<Annotation> GetAnnotationsForTarget<T>(this T target, EntityFramework entityFramework) where T: IODataNavigable, IOdataAnnotatable
public static List<Annotation> GetAnnotationsForTarget<T>(this T target, EntityFramework entityFramework) where T: IODataNavigable, IODataAnnotatable
{
string targetType = entityFramework.LookupIdentifierForType(target);
var targetNamespace = targetType.NamespaceOnly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public DocumentationComplexType(EntityFramework entityFramework, ComplexType com
{
this.Name = complexType.Name;
this.Description = complexType.GetDescription(entityFramework);
this.Namespace = entityFramework.LookupIdentifierForType(complexType).NamespaceOnly();
this.Namespace = complexType.Namespace;
this.Properties = complexType.Properties.Select(p => p.ToDocumentationProperty(entityFramework, complexType)).ToList();
this.Json = SampleJsonGenerator.GetSampleJson(entityFramework, complexType).ToString(Formatting.Indented);
this.Json = ODataParser.BuildJsonExample(complexType, entityFramework.DataServices.Schemas);//SampleJsonGenerator.GetSampleJson(entityFramework, complexType).ToString(Formatting.Indented);


if (complexType.BaseType != null)
Expand Down
25 changes: 23 additions & 2 deletions ApiDocs.DocumentationGeneration/Model/DocumentationProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using ApiDocs.Validation;

namespace ApiDocs.DocumentationGeneration.Model
{
using System.Collections.Generic;
Expand All @@ -35,14 +37,33 @@ public class DocumentationProperty
public DocumentationProperty(EntityFramework entityFramework, ComplexType complexType, Property property)
{
this.Name = property.Name;
this.Type = property.GetTypeNameMarkdown(entityFramework);

string typeName = property.Type;
bool isCollection = property.Type.IsCollection();
if (isCollection)
{
typeName = property.Type.ElementName();
}

var simpleType = typeName.ToODataSimpleType();
if (simpleType != SimpleDataType.None)
{
this.Type = new ParameterDataType(simpleType, isCollection);
}
else
{
this.Type = new ParameterDataType(typeName, isCollection);
}
this.TypeMarkDown = this.Type.GetMarkDown();
this.Description = property.GetDescription(entityFramework, complexType);
}

public string Description { get; private set; }

public string Name { get; private set; }

public string Type { get; private set; }
public ParameterDataType Type { get; private set; }

public string TypeMarkDown { get; private set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ApiDocs.DocumentationGeneration/Properties/Templates.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="resource" type="System.Resources.ResXFileRef, System.Windows.Forms">
<data name="resourceMarkDown" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\templates\resource.md.mustache;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
90 changes: 0 additions & 90 deletions ApiDocs.DocumentationGeneration/SampleJsonGenerator.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

| Name | Type | Description |
|:---|:---|:---|
{{#each Properties}}|{{Name}}|{{Type}}|{{Description}}|
{{#each Properties}}|{{Name}}|{{TypeMarkDown}}|{{Description}}|
{{/each}}{{/if}}{{#if NavigationProperties}}
## Relationships

| Name | Type | Description |
|:---|:---|:---|
{{#each NavigationProperties}}|{{Name}}|{{Type}}|{{Description}}|
{{#each NavigationProperties}}|{{Name}}|{{TypeMarkDown}}|{{Description}}|
{{/each}}{{/if}}
## JSON representation

Expand Down
2 changes: 1 addition & 1 deletion ApiDocs.Publishing/CSDL/CsdlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ private void MergeInstanceAnnotationsAndRecordTerms(string typeName, IEnumerable
return prop;
}

private static void AddDescriptionAnnotation<T>(string typeName, T targetProperty, ParameterDefinition sourceParameter, string termForDescription = Term.LongDescriptionTerm) where T: Property, IOdataAnnotatable
private static void AddDescriptionAnnotation<T>(string typeName, T targetProperty, ParameterDefinition sourceParameter, string termForDescription = Term.LongDescriptionTerm) where T: Property, IODataAnnotatable
{
if (!string.IsNullOrEmpty(sourceParameter.Description))
{
Expand Down
5 changes: 4 additions & 1 deletion ApiDocs.Validation/OData/ComplexType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ApiDocs.Validation.OData
using Transformation;

[XmlRoot("ComplexType", Namespace = ODataParser.EdmNamespace)]
public class ComplexType : XmlBackedTransformableObject, IODataNavigable, IOdataAnnotatable
public class ComplexType : XmlBackedTransformableObject, IODataNavigable, IODataAnnotatable
{
public ComplexType()
{
Expand Down Expand Up @@ -112,5 +112,8 @@ public override void ApplyTransformation(BaseModifications mods, EntityFramework
[XmlIgnore]
public override string ElementIdentifier { get { return this.Name; } set { this.Name = value; } }

[XmlIgnore]
public string Namespace { get; set; }

}
}
2 changes: 1 addition & 1 deletion ApiDocs.Validation/OData/EnumType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace ApiDocs.Validation.OData
using Transformation;

[XmlRoot("EnumType", Namespace = ODataParser.EdmNamespace)]
public class EnumType : XmlBackedTransformableObject, IOdataAnnotatable, IODataNavigable
public class EnumType : XmlBackedTransformableObject, IODataAnnotatable, IODataNavigable
{
public EnumType()
{
Expand Down
17 changes: 17 additions & 0 deletions ApiDocs.Validation/OData/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.ComponentModel;

namespace ApiDocs.Validation.OData
{
using System;
Expand Down Expand Up @@ -313,5 +315,20 @@ public static SimpleDataType ToODataSimpleType(this string typeName)
return SimpleDataType.Object;
}

public static bool IsCollection(this string typeName)
{
return typeName.StartsWith($"{ODataParser.CollectionPrefix}", StringComparison.Ordinal) && typeName.EndsWith(")", StringComparison.Ordinal);
}

public static string ElementName(this string collection)
{
if (!collection.IsCollection())
{
throw new ArgumentOutOfRangeException(nameof(collection), $"'{collection}' is not a collction.");
}

return collection.Substring(ODataParser.CollectionPrefix.Length, collection.Length - ODataParser.CollectionPrefix.Length -1);
}

}
}
2 changes: 1 addition & 1 deletion ApiDocs.Validation/OData/IOdataAnnotatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ApiDocs.Validation.OData
/// <summary>
/// Interface for elements that can be annotated;
/// </summary>
public interface IOdataAnnotatable : IODataNamedElement
public interface IODataAnnotatable : IODataNamedElement
{
List<Annotation> Annotation { get; set; }
}
Expand Down
Loading

0 comments on commit 40b7a3b

Please sign in to comment.