[Repo Assist] feat: generate CLI enum types for top-level named enum schemas#409
Conversation
OpenAPI schemas with type: string/integer and an enum keyword now produce real CLI enum ProvidedTypeDefinitions instead of plain string/int aliases. This gives callers compile-time type safety and enables exhaustive matching. - DefinitionCompiler: detect top-level named string/integer enum schemas and emit ProvidedTypeDefinition with base type System.Enum. String enums receive [JsonConverter(typeof<JsonStringEnumConverter>)] and each member gets [JsonPropertyName(originalValue)] for round-trip serialization. Integer enum members use the numeric values from the schema. - RuntimeHelpers: add buildEnumSerializer (cached per type), which handles both string enums (via JsonPropertyName lookup) and integer enums (via Convert.ChangeType). The toParam function dispatches to this path when the boxed value's type IsEnum. - Tests: 10 new Schema.TypeMappingTests covering enum type identity, member names, integer values, JSON attribute presence, and optional/required wrapping; 8 new RuntimeHelpersTests covering toParam for string enum wire values, hyphenated names, integer enums, and Option<EnumType> unwrapping. Closes #146 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot fix tests and build |
… string Agent-Logs-Url: https://github.com/fsprojects/SwaggerProvider/sessions/2b859f86-a22d-4020-9c5b-9103b1d6afe4 Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
Fixed in a895848. The CI failures were in |
|
@copilot Add new provider tests with some custom enum with more than 2 cases |
There was a problem hiding this comment.
Pull request overview
Updates the provider to generate real CLR/CLI enum types for top-level named OpenAPI enum schemas, and extends runtime parameter serialization to preserve enum wire values.
Changes:
- Add a
DefinitionCompiler.compileBySchemabranch to emitProvidedTypeDefinitionenums for top-level namedstring/integerschemas with non-emptyenum. - Add enum-aware serialization in
RuntimeHelpers.toParamwith cached per-enum serializers and a helper to emit[JsonConverter(typeof<JsonStringEnumConverter>)]. - Update and add tests to validate enum type emission, attributes, and runtime serialization behavior; adjust existing provider tests to use generated enum types.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/SwaggerProvider.Tests/Schema.TypeMappingTests.fs | Adds tests asserting named enum schemas compile to CLI enums with correct members/attributes and optional/required property mapping. |
| tests/SwaggerProvider.Tests/RuntimeHelpersTests.fs | Adds tests for toParam serialization of string/integer enums (including Option<_>). |
| tests/SwaggerProvider.ProviderTests/Swashbuckle.UpdateControllers.Tests.fs | Updates controller integration tests to pass/expect enum types instead of strings. |
| tests/SwaggerProvider.ProviderTests/Swashbuckle.ReturnControllers.Tests.fs | Updates return-type integration tests to expect enum types instead of strings. |
| src/SwaggerProvider.Runtime/RuntimeHelpers.fs | Adds cached enum serializers and emits JsonStringEnumConverter attribute data; extends toParam to handle enums. |
| src/SwaggerProvider.DesignTime/DefinitionCompiler.fs | Generates non-erased CLI enum types for top-level named enum schemas (string/integer). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…uery param serialization Agent-Logs-Url: https://github.com/fsprojects/SwaggerProvider/sessions/9b5046fc-d426-4c48-b89c-8716b4dd4ac0 Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
Done in 5318b60. Added a
Also fixed a latent bug in |
|
@copilot fix inline comments |
Agent-Logs-Url: https://github.com/fsprojects/SwaggerProvider/sessions/2c40b117-cd9d-4369-a5b6-1b47c0472d75 Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
Fixed in 672a8f6. Addressed all three inline review comments:
Also added a unit test for the int64 enum case (total: 345 tests, 0 failed). |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Agent-Logs-Url: https://github.com/fsprojects/SwaggerProvider/sessions/dacfc1ea-568a-4d06-9bb6-5ac6fe02eda0 Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
|
@copilot resolve the merge conflicts in this pull request |
Master (#410) restructured toParam to call GetType() once at the top, while the enum branch (this PR) added an 'elif ty.IsEnum' arm after the Option<T> check. The two changes are compatible: keep the single-GetType() structure from master and preserve the enum serializer dispatch from this PR. Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
Resolved in 893d284. The only conflict was in |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot address inline comments and check that we have provider tests for all kinds of enumb (int32, int64 and string) |
…m members; add enum provider tests for all three enum kinds Agent-Logs-Url: https://github.com/fsprojects/SwaggerProvider/sessions/8a5b2b75-2311-4b89-9632-18e61a74ec39 Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
…t; move compile-time checks into explicit test method Agent-Logs-Url: https://github.com/fsprojects/SwaggerProvider/sessions/8a5b2b75-2311-4b89-9632-18e61a74ec39 Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
Inline review comments addressed:
Provider tests for all three enum kinds: Added
All 348 unit tests pass; Fantomas format check clean. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Closes #146
Named OpenAPI enum schemas (with
type: stringortype: integerplusenum: [...]) now produce real CLI enumProvidedTypeDefinitiontypes instead of plainstring/intaliases. This gives callers compile-time type safety and exhaustive pattern matching on API response values.Root cause
The
DefinitionCompilerpreviously mapped all named schemas to their scalar CLR type (e.g.string). There was no path to emit a proper CLI enum even though the TypeProviders SDK supportsProvidedTypeDefinitionwithBaseType = typeof<System.Enum>.Changes
DefinitionCompiler.fscompileBySchematriggered when:fromByPathCompiler = true && tyName ≠ null)type: stringortype: integerwith a non-emptyenumarrayProvidedTypeDefinitionwithBaseType = typeof<System.Enum>andSetEnumUnderlyingType(typeof<int32>)[JsonConverter(typeof<JsonStringEnumConverter>)]to the type, and[JsonPropertyName(originalValue)]to each member (for round-trip serialization of names with hyphens or other special chars)nicePascalNameandUniqueNameGenerator.MakeUniqueto ensure valid .NET identifiersproperties) still map tostring/int; only named$ref-able schemas upgrade to enum typesRuntimeHelpers.fsbuildEnumSerializer: builds an(obj -> string)function per enum type, cached in aConcurrentDictionary[JsonPropertyName]value for each member by integer ordinalConvert.ChangeTypeto extract the underlying integertoParamgains an| o when ty.IsEnum -> ...branch that dispatches to the cached serializergetJsonStringEnumConverterAttribute(): returnsCustomAttributeDatarepresenting[JsonConverter(typeof<JsonStringEnumConverter>)]Attribute.GetCustomAttribute(ty, typeof<...>)(reflection-API form) for netstandard2.0 compatibilityTrade-offs
$refto a named enum schema will now have the enum type rather thanstring/int. This is the intended behavior but callers that relied on the raw type will need to be updated.properties) still producestring/intas before — only named, top-level schemas are upgraded.enum: [null, "active"]) are silently skipped.Test Status
✅ Build succeeded:
dotnet build tests/SwaggerProvider.Tests/SwaggerProvider.Tests.fsproj -c Release✅ All tests pass: 344 total, 0 failed, 1 skipped (pre-existing skip)
✅ Format check:
dotnet fantomas --checkpassed after formattingNew tests added:
Schema.TypeMappingTests: 10 new tests — named string enum → CLI type, member names, integer values,JsonConverter/JsonPropertyNameattributes, integer enums,Option<EnumType>wrappingRuntimeHelpersTests: 8 new tests —toParamfor string enum wire values, hyphenated names, integer enum serialization,Option<EnumType>unwrapping