Typed request bodies refuse enum numbers and digit strings #323
Labels
No labels
agent
api-regen
agent
architect
agent
coder
agent
console-coder
agent
docs-writer
agent
mechanic
agent
protocol-coder
agent
storage-coder
area/api
area/ci
area/console
area/dhcp
area/dns
area/docs
area/packaging
area/process
area/storage
area/windows
kind
bug
kind
chore
kind
docs
kind
epic
kind
feature
kind
performance
kind
security
kind
test
priority
high
priority
low
priority
medium
resolution
duplicate
resolution
not-a-bug
resolution
wontfix
status
blocked
status
in-progress
status
needs-decision
status
needs-design
status
ready
theme
ad-samba-dns
theme
alternatives-benchmark
theme
console-toolchain
theme
later
theme
parity-devices
theme
parity-query
theme
post-release-fixes
theme
tracker-setup
theme
valkey
theme
windows-installer
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Blocks
#316 Epic: Enum fields in API request bodies accept names only
lantea-one/ninlil
Reference
lantea-one/ninlil#323
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Part of epic #316.
Why
POST /api/zonesstores"type":"99", andPOST /api/dhcp-tag-rulesstores match mode5. MVCreads enums through each type's attribute converter, and both converters accept numbers.
What changes
A new
Ninlil.Web/Serialization/RequestEnumNameJsonConverterFactory.csis registered inProgram.csAddJsonOptions. It reads declared names only (case-insensitive; comma list forflags) and writes through the type's own converter, so responses are unchanged. Shared converters
are not touched.
Acceptance
dotnet test src/dotnet/tests/Ninlil.Web.Tests --filter "FullyQualifiedName~EnumNamesOnlyRequestBodyTests|FullyQualifiedName~RequestEnumNameJsonConverterTests"passes. TheReturnsBadRequestrows fail before the change.RequestBodies_EveryEnumPropertyType_IsReadByTheNameOnlyConverterpasses.OpenApiSnapshotTestspasses unchanged (otherwise WP-4).Source
Architect design for #316: §1.2, §1.3 and WP-1.
Program.cs:744,EnumDescriptionJsonConverter.cs:26-28,DhcpModels.cs:238-246,ZoneModels.cs:85-106.Work package detail
Goal: every typed
[FromBody]model refuses digit strings, JSON numbers and undefinedvalues for enum fields. Responses are unchanged.
Files:
src/dotnet/src/Ninlil.Web/Serialization/RequestEnumNameJsonConverterFactory.cs(new): thefactory and the converter.
src/dotnet/src/Ninlil.Web/Program.cs:744: chain.AddJsonOptions(o => o.JsonSerializerOptions.Converters.Add(new RequestEnumNameJsonConverterFactory())).src/dotnet/tests/Ninlil.Web.Tests/RequestEnumNameJsonConverterTests.cs(new, pure unittests; shape like
StorageConnectionStringAssemblerTests).src/dotnet/tests/Ninlil.Web.Tests/EnumNamesOnlyRequestBodyTests.cs(new, shared host; shapelike
EnumNamesOnlyApiTests).Interfaces:
Read rules:
String.[Flags]type), must equal, ignoring case, adefined member's name or one of its declared names. Use
GetEnumDescriptionsTypeExtension.GetEnumDescriptionswith the three attribute checks ofEnumDescriptionJsonConverter.cs:61-66.JsonException("'<text>' is not a valid value for <Type>. Valid values: <ValidNames>.").T?needs no code: System.Text.Json handlesnullbefore the converter.Constraints:
EnumDescriptionJsonConverter,ToEnumStringExtensionor any type's[JsonConverter]attribute (§1.3).ENVIRONMENT.mdchange.rule.
Acceptance (Web.Tests): red first means the
ReturnsBadRequestrows fail on today's201/200.
CreateTagRule_WithADigitStringNumberOrUndefinedMatchMode_ReturnsBadRequest("1",1,"5",5, both fields).CreateTagRule_WithEachMatchModeName_IsCreated(Exact,Prefix,prefix).CreateZone_WithADigitStringForAnEnumField_ReturnsBadRequest(type"0"/"99",dnssecAlgorithm
"13"/"200", denialOfExistenceMode"1"/"99", transferTransport"0").CreateZone_WithAJsonNumberForAnEnumField_ReturnsBadRequest(pins today's 400).CreateZone_WithTheConsoleWireNames_IsCreated(Primary,ECDSAP256SHA256,Nsec3,Plain).CreateDnssecKey_WithADigitStringRoleOrAlgorithm_ReturnsBadRequest("1","9","13","200").CreateDnssecKey_WithWireAlgorithmName_IsCreated.CreateRecord_WithADigitStringTypeOrClass_ReturnsBadRequest("1").CreateClientSchedule_WithADigitStringOrMaskDaysOfWeek_ReturnsBadRequest("3",3).CreateClientSchedule_WithACommaListOfDayNames_StoresIt("Sunday, Monday", read backidentically).
AdminResetPassword_WithADigitStringMode_ReturnsBadRequest.RequestBodies_EveryEnumPropertyType_IsReadByTheNameOnlyConverter: walk the ApiExplorerbody parameter types recursively, including list elements, and assert
CanConvertfor eachenum type.
DnsAccessControlCapabilityEnumis reached only asstring[], so it does notappear.
Acceptance (unit,
RequestEnumNameJsonConverterTests):Read_ForADigitString_ThrowsJsonExceptionRead_ForAJsonNumber_ThrowsJsonExceptionRead_ForAMemberNameInAnyCase_ReturnsTheMemberRead_ForADeclaredWireName_ReturnsTheMember(RSASHA256,DSA-NSEC3-SHA1)Read_ForAFlagsCommaListOfNames_CombinesThemRead_ForAFlagsListWithADigitPart_ThrowsJsonExceptionRead_ForACommaListOnANonFlagsEnum_ThrowsJsonExceptionWrite_ForEveryValueIncludingAnUnnamedCode_MatchesTheAttributeConverter(for example(DnsResourceRecordTypeEnum)65280still writes"65280")CanConvert_ForAnEnumWithoutAStringConverterAttribute_ReturnsFalseNon-goals: merge-patch sites (WP-2), record
data(D2), query strings (D3), stored rows(D4).
Changes: REST API behaviour, yes. The OpenAPI document should be unchanged, because
Swashbuckle derives enum lists from
Write; ifOpenApiSnapshotTestsfails, WP-4 runs.Schema, no. Options, no. README, in WP-5.
Gate:
dotnet build Ninlil.slnx -p:NinlilSkipAngularBuild=true(0 warnings).dotnet test src/dotnet/tests/Ninlil.Web.Tests.dotnet csharpier check .Ghost referenced this issue2026-11-05 19:14:49 -05:00
Ghost referenced this issue2026-11-05 20:00:31 -05:00
Ghost referenced this issue2026-11-05 20:01:25 -05:00
Ghost referenced this issue2026-11-05 20:30:52 -05:00