-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[typescript] Add parameter to enrich maps with propertyNames and required properties #19513
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
base: master
Are you sure you want to change the base?
Conversation
268d734 to
e801c0d
Compare
macjohnny
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your contribution!
| @@ -1 +1 @@ | |||
| 7.11.0-SNAPSHOT | |||
| 7.9.0-SNAPSHOT | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please rebase on the latest master an re-generate the samples?
| codegen.additionalProperties().put("enrichedMaps", Boolean.TRUE); | ||
| codegen.processOpts(); | ||
| parentSchema.propertyNames(new StringSchema()); | ||
| Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "{ [key: string]: string; x: string; y: string; }"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about adding newlines between the properties in the generated code?
|
cc @joscha |
| .orElse(""); | ||
|
|
||
| if (!enrichedMaps) { | ||
| return "{ [key: string]: " + innerDataType + suffixOrEmpty + "; }"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use Record here?
| // Property names are assumed to be optional, so if we have any we need to make sure that the type of the index | ||
| // signature includes undefined | ||
| String undefinedSuffix = !propertyNames.isEmpty() && !suffixOrEmpty.contains("undefined") ? " | undefined" : ""; | ||
| StringBuilder sb = new StringBuilder() | ||
| .append("{ [key: string]: ") | ||
| .append(innerDataType) | ||
| .append(suffixOrEmpty) | ||
| .append(undefinedSuffix) | ||
| .append(";"); | ||
|
|
||
| requiredProperties.forEach(appendProperty(sb, innerDataType + suffixOrEmpty, true)); | ||
| propertyNames.forEach(appendProperty(sb, innerDataType + suffixOrEmpty, false)); | ||
|
|
||
| return sb.append(" }").toString(); | ||
| } | ||
|
|
||
| private Consumer<String> appendProperty(StringBuilder sb, String type, boolean required) { | ||
| return (String propertyName) -> sb.append(" ") | ||
| .append(propertyName) | ||
| .append(required ? "" : "?") | ||
| .append(": ") | ||
| .append(type) | ||
| .append(";"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
juts thinking out loud here - is there a cleaner way to describe this? Like for example it's own sub-template? The string concat is quite extensive here - isn't this better expressed in a template?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to extract it into it's own sub-template. I should have some time end of january, start of february to hopefully finish the changes
|
@Sqh3rd are you still working on this feature? |
will take it up again a bit later today and try to finish it by the end of the week :) |
|
So after taking way longer than I thought it would, I'm not sure about the original approach of using a parameter to define this behavior. In order to make the changes work neatly with the templates, it's necessary to add propertyNames in IJsonSchemaValidationProperties, additionally I added the nameOnlyVars list to be able to get them in the template. (I'd really appreciate some feedback about that, not sure if the solution fits well into the codebase) Having a parameter that either sets or clears the corresponding vars seems very intrusive, so I'm not sure whether that makes sense to do. I think that just adding this behavior is not breaking backwards compatibility, as the properties will be optional, so I think it's fine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 issues found across 7 files (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenResponse.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenResponse.java:94">
P2: New field propertyNames is not included in equals/hashCode, so responses differing only in propertyNames are treated as equal.</violation>
</file>
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java:64">
P2: New field `propertyNames` is not propagated: copy(), equals(), and hashCode() ignore it, so cloned/equal parameters drop property-name constraints.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenResponse.java
Show resolved
Hide resolved
modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 issue found across 2 files (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java:299">
P2: propertyNames added to equals/hashCode is not copied in copy(), so cloned parameters drop map key schema metadata</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenParameter.java
Show resolved
Hide resolved
|
@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha Tagging again, cause the PR is quite old. Could somebody from the technical committee please take a look once more at @Sqh3rd's work? |
Add additional parameter for the abstract typescript codegen to consider defined propertyNames and required properties, when generating maps/dictionaries.
Implements #19433
Mention TS-People:
(@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka)
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming 7.6.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)