Skip to content

Commit 226d5f6

Browse files
committed
Added new options:
* Enable / Disable ODATA completely * Enable XML Comments for swagger Fixed * Fixed a bug that prevented the service to start when the connectionstring wasn't set despite using InMemory
1 parent d57c104 commit 226d5f6

File tree

6 files changed

+43
-19
lines changed

6 files changed

+43
-19
lines changed

sample/ApiGeneratorSampleApp/Model/Person.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class Person : Trackable,
2222
IEntityTypeConfiguration<Person> // Configure Table Options yourself
2323
{
2424
public string Name { get; set; }
25+
26+
2527
public DateTime Date { get; set; }
2628
public string Description { get; set; }
2729
public int Age { get; set; }

sample/ApiGeneratorSampleApp/appsettings.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform
1212

1313
"CallbackPath": "/signin-oidc"
1414
},
15-
"ConnectionStrings": {
16-
"ApiGeneratorDatabase": "Server=localhost;database=tcdev_dev_222;user=sa;password=Password!23;"
17-
},
15+
1816
"Logging": {
1917
"LogLevel": {
2018
"Default": "Information",
@@ -25,10 +23,6 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform
2523
"AllowedHosts": "*",
2624
//Sample Config for API Generator
2725
"Api": {
28-
"Database": {
29-
"DatabaseType": "SQL"
30-
}
31-
}
3226
"Swagger": {
3327
"EnableProduction": "false", // Enable/Disable for production builds
3428
"Description": "Sample Swagger Config",
@@ -38,9 +32,10 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform
3832
"ContactUri": "https://www.myuri.com"
3933
},
4034
"Database": {
41-
"DatabaseType": "SQL|InMemory|SQLite"
35+
"DatabaseType": "InMemory"
4236
},
4337
"Odata": {
38+
"EnableOData": false,
4439
"EnableSelect": true,
4540
"EnableFilter": true,
4641
"EnableSort": true

src/TCDev.APIGenerator/Data/GenericDbContext.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
4343
{
4444
if (!optionsBuilder.IsConfigured)
4545
{
46-
var config = new ApiGeneratorConfig(null);
4746
var configuration = new ConfigurationBuilder()
4847
.SetBasePath(Directory.GetCurrentDirectory())
4948
.AddJsonFile("appsettings.json")
49+
.AddJsonFile("secrets.json")
5050
.Build();
51-
var connectionString = configuration.GetConnectionString("ApiGeneratorDatabase");
52-
51+
var config = new ApiGeneratorConfig(configuration);
5352
// Add Database Context
5453

5554
switch (config.DatabaseOptions.DatabaseType)
@@ -58,10 +57,12 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
5857
optionsBuilder.UseInMemoryDatabase("ApiGeneratorDB");
5958
break;
6059
case DBType.SQL:
61-
optionsBuilder.UseSqlServer(connectionString);
60+
var connectionStringSQL = configuration.GetConnectionString("ApiGeneratorDatabase");
61+
optionsBuilder.UseSqlServer(connectionStringSQL);
6262
break;
6363
case DBType.SQLite:
64-
optionsBuilder.UseSqlite(connectionString);
64+
var connectionStringSQLite = configuration.GetConnectionString("ApiGeneratorDatabase");
65+
optionsBuilder.UseSqlite(connectionStringSQLite);
6566
break;
6667
default:
6768
throw new Exception("Database Type Unkown");

src/TCDev.APIGenerator/Extension/ApiGeneratorConfig.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,32 @@ public ApiGeneratorConfig(IConfiguration config)
2424
}
2525

2626
//Load Options
27+
configuration.Bind("API:Basic", APIOptions);
2728
configuration.Bind("Api:Cache", CacheOptions);
2829
configuration.Bind("Api:Swagger", SwaggerOptions);
2930
configuration.Bind("Api:Database", DatabaseOptions);
31+
configuration.Bind("Api:Odata", ODataOptions);
3032
}
3133

3234
private readonly IConfigurationRoot Configuration;
3335
public CacheOptions CacheOptions { get; set; } = new CacheOptions();
36+
37+
public APIOptions APIOptions { get; set; } = new APIOptions();
38+
3439
public SwaggerOptions SwaggerOptions { get; set; } = new SwaggerOptions();
3540
public DatabaseOptions DatabaseOptions { get; set; } = new DatabaseOptions();
41+
public ODataFunctions ODataOptions { get; set; } = new ODataFunctions();
3642

3743
public string MetadataRoute { get; set; } = "odata";
3844
}
3945

46+
47+
public class APIOptions
48+
{
49+
public bool UseXMLComments { get; set; } = false;
50+
public string XMLCommentsFile { get; set; } = string.Empty;
51+
}
52+
4053
public class CacheOptions
4154
{
4255
public bool Enabled { get; set; } = true;
@@ -61,6 +74,7 @@ public class DatabaseOptions
6174

6275
public class ODataFunctions
6376
{
77+
public bool EnableOData = false;
6478
public bool EnableSelect { get; set; } = true;
6579
public bool EnableFilter { get; set; } = true;
6680
public bool EnableSort { get; set; } = true;

src/TCDev.APIGenerator/Extension/ApiGeneratorExtension.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,31 @@ public static IServiceCollection AddApiGeneratorServices(
8787
});
8888

8989
c.DocumentFilter<ShowInSwaggerFilter>();
90-
//c.IncludeXmlComments($"{assembly.GetName().Name}.xml", true);
90+
if(ApiGeneratorConfig.APIOptions.UseXMLComments)
91+
{
92+
if (!string.IsNullOrEmpty(ApiGeneratorConfig.APIOptions.XMLCommentsFile))
93+
{
94+
throw new Exception("You need to set XMLCommentsFile option when using XMl Comments");
95+
} else {
96+
c.IncludeXmlComments(ApiGeneratorConfig.APIOptions.XMLCommentsFile, true);
97+
}
98+
}
99+
91100
});
92101

93102

94-
services.AddControllers().AddOData(opt =>
103+
if(ApiGeneratorConfig.ODataOptions.EnableOData)
104+
{
105+
services.AddControllers().AddOData(opt =>
95106
{
96107
opt.AddRouteComponents("odata", GenericDbContext.EdmModel);
97108
opt.EnableNoDollarQueryOptions = true;
98109
opt.EnableQueryFeatures(20000);
99110
opt.Select().Expand().Filter();
100-
}
101-
);
102-
111+
});
112+
} else {
113+
services.AddControllers();
114+
}
103115

104116
return services;
105117
}

src/TCDev.APIGenerator/TCDev.APIGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<PackageId>TCDev.APIGenerator</PackageId>
6-
<Version>0.0.5-alpha</Version>
6+
<Version>0.0.8-alpha</Version>
77
<Authors>Tim Cadenbach</Authors>
88
<Company>TCDev</Company>
99
<Description>Creates fully working CRUD Apis from just class files</Description>

0 commit comments

Comments
 (0)