Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AutoSDK.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<Project Path="src/helpers/GenerateDocs/GenerateDocs.csproj" />
</Folder>
<Folder Name="/libs/">
<File Path="src/libs/Directory.Build.props" />
<Project Path="src/libs/AutoSDK.CLI/AutoSDK.CLI.csproj" />
<Project Path="src/libs/AutoSDK.SourceGenerators/AutoSDK.SourceGenerators.csproj" />
<Project Path="src/libs/AutoSDK/AutoSDK.csproj" />
<File Path="src/libs/Directory.Build.props" />
</Folder>
<Folder Name="/Solution Items/">
<File Path=".github/dependabot.yml" />
Expand Down Expand Up @@ -57,6 +57,7 @@
<File Path=".github/workflows/pull-request.yml" />
</Folder>
<Folder Name="/tests/">
<Project Path="src/tests/AutoSDK.GeneratorTests/AutoSDK.GeneratorTests.csproj" Id="d50f68ac-4bdf-427f-b7b3-629841d2afa0" />
<Project Path="src/tests/AutoSDK.IntegrationTests.Cli/AutoSDK.IntegrationTests.Cli.csproj" />
<Project Path="src/tests/AutoSDK.SnapshotTests/AutoSDK.SnapshotTests.csproj" />
<Project Path="src/tests/AutoSDK.UnitTests/AutoSDK.UnitTests.csproj" />
Expand Down
6 changes: 3 additions & 3 deletions specs/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
$ref: "#/components/schemas/PetStore.Pet"
default:
description: unexpected error
content:
Expand All @@ -82,7 +82,7 @@ paths:
$ref: "#/components/schemas/Error"
components:
schemas:
Pet:
PetStore.Pet:
type: object
required:
- id
Expand All @@ -99,7 +99,7 @@ components:
type: array
maxItems: 100
items:
$ref: "#/components/schemas/Pet"
$ref: "#/components/schemas/PetStore.Pet"
Error:
type: object
required:
Expand Down
92 changes: 52 additions & 40 deletions src/libs/AutoSDK/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Globalization;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;

namespace AutoSDK.Extensions;

Expand All @@ -25,7 +23,7 @@ public static string Inject(this IEnumerable<string> values, string emptyValue =

return text;
}

/// <summary>
/// Makes the first letter of the name uppercase.
/// </summary>
Expand All @@ -46,7 +44,7 @@ public static string ToPropertyName(this string input)
#endif
};
}

/// <summary>
/// Makes the first letter of the name lowercase.
/// </summary>
Expand Down Expand Up @@ -75,7 +73,7 @@ public static string ToParameterName(this string input)
#pragma warning restore CA1308 // Normalize strings to uppercase
};
}

/// <summary>
/// Normalizes line endings to '\n' or your endings.
/// </summary>
Expand All @@ -99,7 +97,7 @@ public static string NormalizeLineEndings(

return newText;
}

private static readonly char[] Separator = { '\n' };

/// <summary>
Expand All @@ -121,85 +119,85 @@ public static string RemoveBlankLinesWhereOnlyWhitespaces(this string text)
.Split(Separator, StringSplitOptions.None)
.Where(static line => line.Length == 0 || !line.All(char.IsWhiteSpace)));
}

public static string AsArray(this string type)
{
return $"global::System.Collections.Generic.IList<{type}>";
}

public static string? WithPostfix(this string? type, string postfix)
{
if (type == null)
{
return null;
}

return type + postfix;
}

public static string ToXmlDocumentation(
this string text,
int level = 4,
bool returnIfSingleLine = false)
{
text = text ?? throw new ArgumentNullException(nameof(text));

var lines = text.Split(NewLine, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length == 0)
{
lines = [string.Empty];
}

if (returnIfSingleLine && lines.Length == 1)
{
return $"{lines[0]}";
}

var spaces = new string(' ', level);
var value = string.Join("\n", lines
.Select((line, i) => $"{spaces}/// {line}{(i != lines.Length - 1 ? "<br/>" : string.Empty)}"));

return value;
}

public static string ToXmlDocumentationSummary(
this string text,
int level = 4)
{
text = text ?? throw new ArgumentNullException(nameof(text));

var spaces = new string(' ', level);
var value = ToXmlDocumentation(text, level);

return $@"/// <summary>
{value}
{spaces}/// </summary>";
}

public static string ToXmlDocumentationExample(
this string text,
int level = 4)
{
text = text ?? throw new ArgumentNullException(nameof(text));

if (string.IsNullOrWhiteSpace(text))
{
return string.Empty;
}

var value = ToXmlDocumentation(text, level, returnIfSingleLine: true);
if (!value.Contains("\n"))
{
return $"/// <example>{value}</example>";
}

var spaces = new string(' ', level);

return $@"/// <example>
{value}
{spaces}/// </example>";
}

public static string ToXmlDocumentationDefault(
this string text,
int level = 4)
Expand All @@ -210,58 +208,58 @@ public static string ToXmlDocumentationDefault(
{
return string.Empty;
}

var value = ToXmlDocumentation(text, level, returnIfSingleLine: true);
if (!value.Contains("\n"))
{
return $"/// <default>{value}</default>";
}

var spaces = new string(' ', level);

return $@"/// <default>
{value}
{spaces}/// </default>";
}

public static string ToXmlDocumentationForParam(
this string text,
string parameterName,
int level = 4)
{
text = text ?? throw new ArgumentNullException(nameof(text));
parameterName = parameterName ?? throw new ArgumentNullException(nameof(parameterName));

var spaces = new string(' ', level);
var value = ToXmlDocumentation(text, level);

return string.IsNullOrWhiteSpace(text)
? $@"/// <param name=""{parameterName.TrimStart('@')}""></param>"
: $@"/// <param name=""{parameterName.TrimStart('@')}"">
{value}
{spaces}/// </param>";
}

public static string UseWordSeparator(
this string propertyName,
params char[] separator)
{
propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));

if (!separator.Any(propertyName.Contains))
{
return propertyName;
}

return string.Join(
string.Empty,
propertyName
.Split(separator)
.Select(word => word.ToPropertyName()));
}

private readonly static string[] NewLine = { "\n" };

public static string AddIndent(
this string text,
int level)
Expand All @@ -271,14 +269,14 @@ public static string AddIndent(
{
return text;
}

var lines = text.Split(NewLine, StringSplitOptions.None);
var spaces = new string(' ', level * 4);

return string.Join("\n", lines
.Select(line => string.IsNullOrEmpty(line) ? line : $"{spaces}{line}"));
}

public static string FixPropertyName(
this string propertyName,
string className)
Expand All @@ -287,15 +285,29 @@ public static string FixPropertyName(
? $"{propertyName}1"
: propertyName;
}

public static string ToClassName(
this string text)
{
return text
.ToPropertyName()
.UseWordSeparator('_', '-', '.', ' ', '\\', '/', '[', ']');
.UseWordSeparator('_', '-', ' ', '\\', '/', '[', ']')
.Split('.')
.Last();
}


public static string ToNamespace(
this string text)
{
if (string.IsNullOrWhiteSpace(text))
{
throw new ArgumentNullException(nameof(text));
}
return text
.Split('.')
.Last();
}

public static string ReplaceIfEquals(
this string text,
string oldValue,
Expand Down
16 changes: 7 additions & 9 deletions src/libs/AutoSDK/Models/AnyOfData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Collections.Immutable;
using AutoSDK.Extensions;
using AutoSDK.Naming.AnyOfs;
using AutoSDK.Naming.Properties;
using AutoSDK.Serialization.Json;
using System.Collections.Immutable;

namespace AutoSDK.Models;

Expand All @@ -20,11 +19,11 @@ Settings Settings
)
{
public bool IsNamed => !string.IsNullOrWhiteSpace(Name);

public static AnyOfData FromSchemaContext(SchemaContext context)
{
context = context ?? throw new ArgumentNullException(nameof(context));

var children = context.Children
.Where(x => x.Hint == (context.IsAnyOf
? Hint.AnyOf
Expand All @@ -35,15 +34,14 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
var className = context.Id.ToClassName();
TypeData? discriminatorType = null;
string? discriminatorPropertyName = null;

if (context.Schema.Discriminator != null &&
context.Schema.Discriminator.Mapping.Count != 0)
{
discriminatorType = context.Children.FirstOrDefault(x => x.Hint == Hint.Discriminator)?.TypeData;
discriminatorPropertyName = context.Schema.Discriminator.PropertyName.ToPropertyName()
.ToCSharpName(context.Settings, context.Parent);
discriminatorPropertyName = context.Schema.Discriminator.PropertyName.ToPropertyName();
}

var count = context.IsAnyOf
? context.Schema.AnyOf.Count
: context.IsOneOf
Expand Down Expand Up @@ -86,7 +84,7 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
})
.ToImmutableArray().AsEquatableArray();
}

return new AnyOfData(
SubType: context.IsAnyOf
? "AnyOf"
Expand Down
Loading
Loading