Skip to content

Commit fe25356

Browse files
vbilopavclaude
andcommitted
Add RoutineOptions:Enabled flag, disable CrudSource by default, fix SqlFileSource blocked by CrudSource
- RoutineOptions now supports Enabled setting (default true) to skip routine source - CrudSource defaults to Enabled: false (was true) - must be explicitly enabled - CrudSource disabled no longer blocks SqlFileSource from being registered - RoutineType.SqlFile enum value for clearer log messages - Updated changelog Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5660055 commit fe25356

6 files changed

Lines changed: 91 additions & 46 deletions

File tree

NpgsqlRestClient/App.cs

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -523,56 +523,62 @@ public List<IEndpointSource> CreateEndpointSources()
523523
{
524524
var sources = new List<IEndpointSource>(2);
525525

526-
var source = new RoutineSource();
527526
var routineOptionsCfg = _config.NpgsqlRestCfg.GetSection("RoutineOptions");
528-
if (routineOptionsCfg.Exists() is true)
527+
if (routineOptionsCfg.Exists() is true && _config.GetConfigBool("Enabled", routineOptionsCfg, true) is false)
529528
{
530-
var customTypeParameterSeparator = _config.GetConfigStr("CustomTypeParameterSeparator", routineOptionsCfg);
531-
if (customTypeParameterSeparator is not null)
532-
{
533-
source.CustomTypeParameterSeparator = customTypeParameterSeparator;
534-
}
535-
var includeLanguages = _config.GetConfigEnumerable("IncludeLanguages", routineOptionsCfg);
536-
if (includeLanguages is not null)
537-
{
538-
source.IncludeLanguages = [.. includeLanguages];
539-
}
540-
var excludeLanguages = _config.GetConfigEnumerable("ExcludeLanguages", routineOptionsCfg);
541-
if (excludeLanguages is not null)
529+
_builder.ClientLogger?.LogDebug("{name} PostgreSQL Source is disabled", nameof(RoutineSource));
530+
}
531+
else
532+
{
533+
var source = new RoutineSource();
534+
if (routineOptionsCfg.Exists() is true)
542535
{
543-
source.ExcludeLanguages = [.. excludeLanguages];
536+
var customTypeParameterSeparator = _config.GetConfigStr("CustomTypeParameterSeparator", routineOptionsCfg);
537+
if (customTypeParameterSeparator is not null)
538+
{
539+
source.CustomTypeParameterSeparator = customTypeParameterSeparator;
540+
}
541+
var includeLanguages = _config.GetConfigEnumerable("IncludeLanguages", routineOptionsCfg);
542+
if (includeLanguages is not null)
543+
{
544+
source.IncludeLanguages = [.. includeLanguages];
545+
}
546+
var excludeLanguages = _config.GetConfigEnumerable("ExcludeLanguages", routineOptionsCfg);
547+
if (excludeLanguages is not null)
548+
{
549+
source.ExcludeLanguages = [.. excludeLanguages];
550+
}
551+
source.NestedJsonForCompositeTypes = _config.GetConfigBool("NestedJsonForCompositeTypes", routineOptionsCfg);
552+
source.ResolveNestedCompositeTypes = _config.GetConfigBool("ResolveNestedCompositeTypes", routineOptionsCfg, true);
544553
}
545-
source.NestedJsonForCompositeTypes = _config.GetConfigBool("NestedJsonForCompositeTypes", routineOptionsCfg);
546-
source.ResolveNestedCompositeTypes = _config.GetConfigBool("ResolveNestedCompositeTypes", routineOptionsCfg, true);
554+
sources.Add(source);
555+
_builder.ClientLogger?.LogDebug("Using {name} PostrgeSQL Source", nameof(RoutineSource));
547556
}
548-
sources.Add(source);
549-
_builder.ClientLogger?.LogDebug("Using {name} PostrgeSQL Source", nameof(RoutineSource));
550557

551558
var crudSourceCfg = _config.NpgsqlRestCfg.GetSection("CrudSource");
552-
if (crudSourceCfg.Exists() is false || _config.GetConfigBool("Enabled", crudSourceCfg, true) is false)
553-
{
554-
return sources;
555-
}
556-
sources.Add(new CrudSource()
557-
{
558-
SchemaSimilarTo = _config.GetConfigStr("SchemaSimilarTo", crudSourceCfg),
559-
SchemaNotSimilarTo = _config.GetConfigStr("SchemaNotSimilarTo", crudSourceCfg),
560-
IncludeSchemas = _config.GetConfigEnumerable("IncludeSchemas", crudSourceCfg)?.ToArray(),
561-
ExcludeSchemas = _config.GetConfigEnumerable("ExcludeSchemas", crudSourceCfg)?.ToArray(),
562-
NameSimilarTo = _config.GetConfigStr("NameSimilarTo", crudSourceCfg),
563-
NameNotSimilarTo = _config.GetConfigStr("NameNotSimilarTo", crudSourceCfg),
564-
IncludeNames = _config.GetConfigEnumerable("IncludeNames", crudSourceCfg)?.ToArray(),
565-
ExcludeNames = _config.GetConfigEnumerable("ExcludeNames", crudSourceCfg)?.ToArray(),
566-
CommentsMode = _config.GetConfigEnum<CommentsMode?>("CommentsMode", crudSourceCfg),
567-
CrudTypes = _config.GetConfigFlag<CrudCommandType>("CrudTypes", crudSourceCfg),
568-
569-
ReturningUrlPattern = _config.GetConfigStr("ReturningUrlPattern", crudSourceCfg) ?? "{0}/returning",
570-
OnConflictDoNothingUrlPattern = _config.GetConfigStr("OnConflictDoNothingUrlPattern", crudSourceCfg) ?? "{0}/on-conflict-do-nothing",
571-
OnConflictDoNothingReturningUrlPattern = _config.GetConfigStr("OnConflictDoNothingReturningUrlPattern", crudSourceCfg) ?? "{0}/on-conflict-do-nothing/returning",
572-
OnConflictDoUpdateUrlPattern = _config.GetConfigStr("OnConflictDoUpdateUrlPattern", crudSourceCfg) ?? "{0}/on-conflict-do-update",
573-
OnConflictDoUpdateReturningUrlPattern = _config.GetConfigStr("OnConflictDoUpdateReturningUrlPattern", crudSourceCfg) ?? "{0}/on-conflict-do-update/returning",
574-
});
575-
_builder.ClientLogger?.LogDebug("Using {name} PostrgeSQL Source", nameof(CrudSource));
559+
if (crudSourceCfg.Exists() is true && _config.GetConfigBool("Enabled", crudSourceCfg, true) is true)
560+
{
561+
sources.Add(new CrudSource()
562+
{
563+
SchemaSimilarTo = _config.GetConfigStr("SchemaSimilarTo", crudSourceCfg),
564+
SchemaNotSimilarTo = _config.GetConfigStr("SchemaNotSimilarTo", crudSourceCfg),
565+
IncludeSchemas = _config.GetConfigEnumerable("IncludeSchemas", crudSourceCfg)?.ToArray(),
566+
ExcludeSchemas = _config.GetConfigEnumerable("ExcludeSchemas", crudSourceCfg)?.ToArray(),
567+
NameSimilarTo = _config.GetConfigStr("NameSimilarTo", crudSourceCfg),
568+
NameNotSimilarTo = _config.GetConfigStr("NameNotSimilarTo", crudSourceCfg),
569+
IncludeNames = _config.GetConfigEnumerable("IncludeNames", crudSourceCfg)?.ToArray(),
570+
ExcludeNames = _config.GetConfigEnumerable("ExcludeNames", crudSourceCfg)?.ToArray(),
571+
CommentsMode = _config.GetConfigEnum<CommentsMode?>("CommentsMode", crudSourceCfg),
572+
CrudTypes = _config.GetConfigFlag<CrudCommandType>("CrudTypes", crudSourceCfg),
573+
574+
ReturningUrlPattern = _config.GetConfigStr("ReturningUrlPattern", crudSourceCfg) ?? "{0}/returning",
575+
OnConflictDoNothingUrlPattern = _config.GetConfigStr("OnConflictDoNothingUrlPattern", crudSourceCfg) ?? "{0}/on-conflict-do-nothing",
576+
OnConflictDoNothingReturningUrlPattern = _config.GetConfigStr("OnConflictDoNothingReturningUrlPattern", crudSourceCfg) ?? "{0}/on-conflict-do-nothing/returning",
577+
OnConflictDoUpdateUrlPattern = _config.GetConfigStr("OnConflictDoUpdateUrlPattern", crudSourceCfg) ?? "{0}/on-conflict-do-update",
578+
OnConflictDoUpdateReturningUrlPattern = _config.GetConfigStr("OnConflictDoUpdateReturningUrlPattern", crudSourceCfg) ?? "{0}/on-conflict-do-update/returning",
579+
});
580+
_builder.ClientLogger?.LogDebug("Using {name} PostrgeSQL Source", nameof(CrudSource));
581+
}
576582

577583
var sqlFileSourceCfg = _config.NpgsqlRestCfg.GetSection("SqlFileSource");
578584
if (sqlFileSourceCfg.Exists() is true && _config.GetConfigBool("Enabled", sqlFileSourceCfg, true) is true)

NpgsqlRestClient/ConfigDefaults.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ private static JsonObject GetNpgsqlRestDefaults()
683683

684684
["RoutineOptions"] = new JsonObject
685685
{
686+
["Enabled"] = true,
686687
["CustomTypeParameterSeparator"] = null,
687688
["IncludeLanguages"] = null,
688689
["ExcludeLanguages"] = null,
@@ -916,7 +917,7 @@ private static JsonObject GetCrudSourceDefaults()
916917
{
917918
return new JsonObject
918919
{
919-
["Enabled"] = true,
920+
["Enabled"] = false,
920921
["SchemaSimilarTo"] = null,
921922
["SchemaNotSimilarTo"] = null,
922923
["IncludeSchemas"] = null,

NpgsqlRestClient/ConfigSchemaGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ public static partial class ConfigSchemaGenerator
289289
["NpgsqlRest:DefaultServerSentEventsEventNoticeLevel"] = "Default server-sent event notice message level: INFO, NOTICE, WARNING.\nWhen SSE path is set, generate SSE events for PostgreSQL notice messages with this level or higher.\nThis can be overridden for individual endpoints using comment annotations.",
290290
["NpgsqlRest:ServerSentEventsResponseHeaders"] = "Collection of custom server-sent events response headers that will be added to the response when connected to the endpoint that is configured to return server-sent events.",
291291
["NpgsqlRest:RoutineOptions"] = "Options for handling PostgreSQL routines (functions and procedures)",
292+
["NpgsqlRest:RoutineOptions:Enabled"] = "Set to false to disable the routine source (PostgreSQL functions and procedures). Default is true.",
292293
["NpgsqlRest:RoutineOptions:CustomTypeParameterSeparator"] = "Name separator for parameter names when using custom type parameters.\nParameter names will be in the format: {ParameterName}{CustomTypeParameterSeparator}{CustomTypeFieldName}. When NULL, default underscore is used.\nThis is used when using custom types for parameters. For example: with \"create type custom_type1 as (value text);\" and parameter \"_p custom_type1\", this name will be merged into \"_p_value\"",
293294
["NpgsqlRest:RoutineOptions:IncludeLanguages"] = "List of PostgreSQL routine language names to include. If NULL, all languages are included. Names are case-insensitive.",
294295
["NpgsqlRest:RoutineOptions:ExcludeLanguages"] = "List of PostgreSQL routine language names to exclude. If NULL, \"C\" and \"INTERNAL\" are excluded by default. Names are case-insensitive.",

NpgsqlRestClient/ConfigTemplate.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,10 @@ public static partial class ConfigSchemaGenerator
17781778
// Options for handling PostgreSQL routines (functions and procedures)
17791779
//
17801780
"RoutineOptions": {
1781+
//
1782+
// Set to false to disable the routine source (PostgreSQL functions and procedures). Default is true.
1783+
//
1784+
"Enabled": true,
17811785
//
17821786
// Name separator for parameter names when using custom type parameters.
17831787
// Parameter names will be in the format: {ParameterName}{CustomTypeParameterSeparator}{CustomTypeFieldName}. When NULL, default underscore is used.
@@ -2502,7 +2506,7 @@ public static partial class ConfigSchemaGenerator
25022506
//
25032507
// Enable or disable the creation of the endpoints for the PostgreSQL tables and views.
25042508
//
2505-
"Enabled": true,
2509+
"Enabled": false,
25062510
//
25072511
// Filter schema names similar to this parameter or `null` to ignore this parameter.
25082512
//

NpgsqlRestClient/appsettings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,10 @@
17691769
// Options for handling PostgreSQL routines (functions and procedures)
17701770
//
17711771
"RoutineOptions": {
1772+
//
1773+
// Set to false to disable the routine source (PostgreSQL functions and procedures). Default is true.
1774+
//
1775+
"Enabled": true,
17721776
//
17731777
// Name separator for parameter names when using custom type parameters.
17741778
// Parameter names will be in the format: {ParameterName}{CustomTypeParameterSeparator}{CustomTypeFieldName}. When NULL, default underscore is used.
@@ -2493,7 +2497,7 @@
24932497
//
24942498
// Enable or disable the creation of the endpoints for the PostgreSQL tables and views.
24952499
//
2496-
"Enabled": true,
2500+
"Enabled": false,
24972501
//
24982502
// Filter schema names similar to this parameter or `null` to ignore this parameter.
24992503
//

changelog.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,34 @@ export async function processOrder(
356356

357357
---
358358

359+
### RoutineSource: `Enabled` Configuration Option
360+
361+
The `RoutineOptions` section now supports an `Enabled` setting (default `true`). Set to `false` to disable automatic endpoint creation from PostgreSQL functions and procedures:
362+
363+
```json
364+
"RoutineOptions": {
365+
"Enabled": false
366+
}
367+
```
368+
369+
This is useful for SQL-files-only deployments where the overhead of querying the PostgreSQL catalog for routines is unnecessary.
370+
371+
---
372+
373+
### CrudSource Disabled by Default
374+
375+
The `CrudSource:Enabled` setting now defaults to `false` (was `true`).
376+
377+
CrudSource auto-generates CRUD endpoints for all PostgreSQL tables and views, which is rarely desired in production without explicit configuration. Users who need CRUD endpoints should explicitly set `"CrudSource": { "Enabled": true }`.
378+
379+
---
380+
381+
### CrudSource No Longer Blocks SqlFileSource
382+
383+
Previously, when `CrudSource` was disabled (or its config section was missing), `CreateEndpointSources()` returned early, preventing `SqlFileSource` from being registered. All three endpoint sources (RoutineSource, CrudSource, SqlFileSource) are now independently enabled/disabled.
384+
385+
---
386+
359387
### DataProtection Disabled by Default
360388

361389
The `DataProtection:Enabled` setting now defaults to `false` (was `true`).
@@ -368,6 +396,7 @@ Users who enable Auth, Antiforgery, or encrypt/decrypt annotations should explic
368396

369397
### Internal Changes
370398

399+
- `RoutineType.SqlFile` — new enum value for SQL file endpoints (was `Other`), shown in log messages
371400
- `NpgsqlRestParameter.ConvertedName` / `ActualName``internal set` (was `private set`) for `@param` rename support
372401
- `ParameterHandler.HandleParameterRename` — new method handling all rename/retype annotation forms
373402
- `SqlFileParameterFormatter` — static singleton, `IsFormattable = false`, zero per-endpoint allocation

0 commit comments

Comments
 (0)