Skip to content

fix: generate discriminated union subtypes for oneOf schemas#20

Merged
halotukozak merged 9 commits intomasterfrom
fix/discriminated-union-subtypes
Mar 23, 2026
Merged

fix: generate discriminated union subtypes for oneOf schemas#20
halotukozak merged 9 commits intomasterfrom
fix/discriminated-union-subtypes

Conversation

@halotukozak
Copy link
Member

Summary

  • Fix SpecParser.toApiSpec() to include synthetic schemas from detectAndUnwrapOneOfWrappers — subtypes like DataModelSource, ExtenderDevice etc. were missing from output
  • Add sanitizeSchemaName() in NameUtils.kt to handle boolean discriminator values (true/false) that produce invalid Kotlin class names — now prefixed with parent schema name (e.g., DeviceStatusTrue)
  • Add comprehensive tests: parser-level polymorphic tests, name sanitization unit tests, generator-level e2e tests for multi-variant oneOf and boolean discriminators

Test plan

  • NameUtilsTest — validates identifier checking and name sanitization
  • SpecParserPolymorphicTest — verifies synthetic schemas appear in parsed output
  • ModelGeneratorPolymorphicTest — e2e tests for generated polymorphic hierarchies
  • Manual: run against cem-mobile-app ump-api.json and ump-cem-api.json specs

🤖 Generated with Claude Code

halotukozak and others added 4 commits March 20, 2026 14:30
…hema inclusion

- NameUtilsTest: isValidKotlinIdentifier and sanitizeSchemaName tests
- SpecParserPolymorphicTest: boolean discriminator and synthetic schema tests
- boolean-discriminator-spec.yaml test fixture

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ization

- Add isValidKotlinIdentifier and sanitizeSchemaName to NameUtils.kt
- Fix SpecParser.toApiSpec() to include synthetic schemas from wrapper unwrapping
- Apply name sanitization in detectAndUnwrapOneOfWrappers for boolean discriminator values
- Discriminator mapping preserves original values as keys while schema names are sanitized
- Tests for NameUtils, SpecParser polymorphic parsing, and boolean discriminator fixture

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…lti-variant oneOf

- Test sanitized boolean discriminator names produce valid data classes with correct @SerialName
- Test all 6 oneOf variants generate data classes and appear in SerializersModule
- Test SerializersModule includes sanitized variant names

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 20, 2026 13:44
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link

Overall Project 93.48% -0.07% 🍏
Files changed 98.08% 🍏

File Coverage
NameUtils.kt 98.49% 🍏
SpecParser.kt 88.91% -0.25% 🍏

@github-actions
Copy link

Overall Project 93.48% -0.07% 🍏
Files changed 98.08% 🍏

File Coverage
NameUtils.kt 98.49% 🍏
SpecParser.kt 88.91% -0.25% 🍏

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes polymorphic (oneOf/discriminator) subtype generation by ensuring parser-emitted ApiSpec.schemas includes synthetic component schemas created during wrapper unwrapping, and adds schema-name sanitization for discriminator-derived names that would otherwise be invalid Kotlin class names (notably boolean values).

Changes:

  • Include synthetic schemas registered during detectAndUnwrapOneOfWrappers in OpenAPI.toApiSpec() output.
  • Add isValidKotlinIdentifier() + sanitizeSchemaName() utilities and apply them when registering synthetic variant schemas.
  • Add parser/name-utils/generator regression tests plus a boolean-discriminator fixture spec.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
core/src/main/kotlin/com/avsystem/justworks/core/parser/SpecParser.kt Collects synthetic schemas into ApiSpec.schemas; sanitizes wrapper-derived synthetic schema names.
core/src/main/kotlin/com/avsystem/justworks/core/gen/NameUtils.kt Adds Kotlin identifier validation and schema name sanitization helper.
core/src/test/resources/boolean-discriminator-spec.yaml Adds fixture covering wrapper-style boolean discriminator keys.
core/src/test/kotlin/com/avsystem/justworks/core/parser/SpecParserPolymorphicTest.kt Adds regression tests asserting synthetic schemas and sanitized names appear in parsed output.
core/src/test/kotlin/com/avsystem/justworks/core/gen/NameUtilsTest.kt Adds unit tests for identifier validation and sanitization behavior.
core/src/test/kotlin/com/avsystem/justworks/core/gen/ModelGeneratorPolymorphicTest.kt Adds e2e generator tests for boolean discriminator variants and multi-variant oneOf.
.planning/STATE.md Adds project planning/state tracking document.
.planning/ROADMAP.md Adds roadmap/planning document.
.planning/phases/07-discriminated-union-subtype-generation/07-01-SUMMARY.md Adds Phase 07 summary documentation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…ping

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link

Overall Project 93.35% -0.07% 🍏
Files changed 95.65% 🍏

File Coverage
NameUtils.kt 97.83% 🍏
SpecParser.kt 88.88% -0.25% 🍏

@github-actions
Copy link

Overall Project 93.35% -0.07% 🍏
Files changed 95.65% 🍏

File Coverage
SpecParser.kt 88.88% -0.25% 🍏

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@janwawszczak janwawszczak self-requested a review March 23, 2026 13:06
…sive function in SpecParser

- Improves clarity and ensures consistent schema processing
- Updates related test cases in SpecParserPolymorphicTest
@halotukozak halotukozak merged commit 9dd51a8 into master Mar 23, 2026
1 check failed
@halotukozak halotukozak deleted the fix/discriminated-union-subtypes branch March 23, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants