-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: unified NameRegistry for all generated name deduplication #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
halotukozak
wants to merge
12
commits into
master
Choose a base branch
from
feat/12-unified-name-registry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d0504f1
feat(12-01): add NameRegistry with numeric suffix collision resolution
halotukozak 1e3dbfc
refactor(12-01): extract InlineSchemaKey to standalone file
halotukozak a2d8cbf
feat(12-02): wire NameRegistry into all generators
halotukozak fb75f60
refactor(12-02): delete InlineSchemaDeduplicator, update tests to num…
halotukozak 79951e3
Merge branch 'master' into feat/12-unified-name-registry
halotukozak b7e1d9b
fix: pass NameRegistry to ModelGenerator in IntegrationTest
halotukozak 326f610
fix: resolve inline schema names consistently via InlineTypeResolver
halotukozak a21d2a2
fix: address Copilot review — recursive normalization, fail-fast reso…
halotukozak cc2a63c
fmt
halotukozak 2c93ddd
feat: improve inline schema handling and collision prevention
halotukozak bcd7aa3
Merge branch 'master' into feat/12-unified-name-registry
halotukozak 5d96415
fix(test): update error message field in CodeGeneratorTest
halotukozak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
core/src/main/kotlin/com/avsystem/justworks/core/gen/InlineSchemaDeduplicator.kt
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
core/src/main/kotlin/com/avsystem/justworks/core/gen/InlineSchemaKey.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.avsystem.justworks.core.gen | ||
|
|
||
| import com.avsystem.justworks.core.model.PropertyModel | ||
| import com.avsystem.justworks.core.model.TypeRef | ||
|
|
||
| /** | ||
| * Key for structural equality of inline schemas. | ||
| * Two inline schemas are considered equal if they have the same properties | ||
| * (name, type, required status) regardless of property order. | ||
| * Nested [TypeRef.Inline] types are normalized to ignore [TypeRef.Inline.contextHint], | ||
| * ensuring purely structural comparison. | ||
| */ | ||
| data class InlineSchemaKey(val properties: Set<PropertyKey>) { | ||
| data class PropertyKey( | ||
| val name: String, | ||
| val type: TypeRef, | ||
| val required: Boolean, | ||
| val nullable: Boolean, | ||
| val defaultValue: Any?, | ||
| ) | ||
|
|
||
| companion object { | ||
| fun from(properties: List<PropertyModel>, required: Set<String>): InlineSchemaKey { | ||
| val keys = properties.map { | ||
| PropertyKey( | ||
| name = it.name, | ||
| type = normalizeType(it.type), | ||
| required = it.name in required, | ||
| nullable = it.nullable, | ||
| defaultValue = it.defaultValue, | ||
| ) | ||
| } | ||
| return InlineSchemaKey(keys.toSet()) | ||
| } | ||
|
|
||
| private fun normalizeType(type: TypeRef): TypeRef = when (type) { | ||
| is TypeRef.Inline -> TypeRef.Inline( | ||
| properties = type.properties | ||
| .map { it.copy(type = normalizeType(it.type)) } | ||
| .sortedBy { it.name }, | ||
| requiredProperties = type.requiredProperties, | ||
| contextHint = "", | ||
| ) | ||
|
|
||
halotukozak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| is TypeRef.Array -> TypeRef.Array(normalizeType(type.items)) | ||
|
|
||
| is TypeRef.Map -> TypeRef.Map(normalizeType(type.valueType)) | ||
|
|
||
| else -> type | ||
| } | ||
| } | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
core/src/main/kotlin/com/avsystem/justworks/core/gen/InlineTypeResolver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.avsystem.justworks.core.gen | ||
|
|
||
| import com.avsystem.justworks.core.model.ApiSpec | ||
| import com.avsystem.justworks.core.model.Endpoint | ||
| import com.avsystem.justworks.core.model.PropertyModel | ||
| import com.avsystem.justworks.core.model.RequestBody | ||
| import com.avsystem.justworks.core.model.Response | ||
| import com.avsystem.justworks.core.model.SchemaModel | ||
| import com.avsystem.justworks.core.model.TypeRef | ||
|
|
||
| /** | ||
| * Rewrites all [TypeRef.Inline] references in an [ApiSpec] to [TypeRef.Reference], | ||
| * using the provided [nameMap] that maps structural keys to generated class names. | ||
| * | ||
| * This is applied once after inline schema collection, so downstream generators | ||
| * never encounter [TypeRef.Inline] and need no special handling. | ||
| */ | ||
| fun ApiSpec.resolveInlineTypes(nameMap: Map<InlineSchemaKey, String>): ApiSpec { | ||
| if (nameMap.isEmpty()) return this | ||
|
|
||
| fun TypeRef.resolve(): TypeRef = when (this) { | ||
| is TypeRef.Inline -> { | ||
| val key = InlineSchemaKey.from(properties, requiredProperties) | ||
| val className = nameMap[key] | ||
| ?: error( | ||
| "Missing inline schema mapping for key (contextHint=$contextHint). " + | ||
| "This indicates a mismatch between inline schema collection and resolution.", | ||
| ) | ||
| TypeRef.Reference(className) | ||
| } | ||
|
|
||
| is TypeRef.Array -> { | ||
| TypeRef.Array(items.resolve()) | ||
| } | ||
|
|
||
| is TypeRef.Map -> { | ||
| TypeRef.Map(valueType.resolve()) | ||
| } | ||
|
|
||
| else -> { | ||
| this | ||
| } | ||
| } | ||
|
|
||
| fun PropertyModel.resolve() = copy(type = type.resolve()) | ||
|
|
||
| fun SchemaModel.resolve() = copy( | ||
| properties = properties.map { it.resolve() }, | ||
| allOf = allOf?.map { it.resolve() }, | ||
| oneOf = oneOf?.map { it.resolve() }, | ||
| anyOf = anyOf?.map { it.resolve() }, | ||
| underlyingType = underlyingType?.resolve(), | ||
| ) | ||
|
|
||
| fun Response.resolve() = copy(schema = schema?.resolve()) | ||
|
|
||
| fun RequestBody.resolve() = copy(schema = schema.resolve()) | ||
|
|
||
| fun Endpoint.resolve() = copy( | ||
| parameters = parameters.map { it.copy(schema = it.schema.resolve()) }, | ||
| requestBody = requestBody?.resolve(), | ||
| responses = responses.mapValues { (_, v) -> v.resolve() }, | ||
| ) | ||
|
|
||
| return copy( | ||
| schemas = schemas.map { it.resolve() }, | ||
| endpoints = endpoints.map { it.resolve() }, | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.