-
Notifications
You must be signed in to change notification settings - Fork 21
feat(java): generic support for hits #829
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a847f5c
feat(java): generic support for hits
millotp bf31c83
fix(ci): only remove relevant generated tests
millotp 9ad41df
remove logic
millotp 598cd3a
Merge branch 'main' into feat/generic-java
millotp ceaed41
add the type parameter
millotp 77d8333
test cts
millotp 77d307d
format
millotp 2f054dd
remove log
millotp fafdabe
Merge branch 'main' into feat/generic-java
millotp 19c2e55
first review
millotp 8251e51
Merge branch 'main' into feat/generic-java
millotp 73c38ec
rollback
millotp 6003c0b
format
millotp f95d80d
rename true generic
millotp a7eb202
format
millotp 989b677
Merge branch 'main' into feat/generic-java
millotp b3cd596
review
millotp 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
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
158 changes: 158 additions & 0 deletions
158
generators/src/main/java/com/algolia/codegen/GenericPropagator.java
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,158 @@ | ||
package com.algolia.codegen; | ||
millotp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import com.algolia.codegen.exceptions.*; | ||
import java.util.*; | ||
import org.openapitools.codegen.*; | ||
import org.openapitools.codegen.model.*; | ||
|
||
public class GenericPropagator { | ||
|
||
private static Set<String> primitiveModels = new HashSet<>(Arrays.asList("object", "array", "string", "boolean", "integer")); | ||
|
||
// Only static use of this class | ||
private GenericPropagator() {} | ||
|
||
private static void setVendorExtension(IJsonSchemaValidationProperties property, String key, Object value) { | ||
if (property instanceof CodegenModel) { | ||
((CodegenModel) property).vendorExtensions.put(key, value); | ||
} else if (property instanceof CodegenProperty) { | ||
((CodegenProperty) property).vendorExtensions.put(key, value); | ||
} | ||
} | ||
|
||
/** | ||
* Add the property x-propagated-generic to a model or property, meaning it should be replaced | ||
* with T directly | ||
*/ | ||
private static void setPropagatedGeneric(IJsonSchemaValidationProperties property) { | ||
setVendorExtension(property, "x-propagated-generic", true); | ||
} | ||
|
||
/** | ||
* Add the property x-has-child-generic to a model or property, meaning one of its members is | ||
* generic and it should propagate the T | ||
*/ | ||
private static void setHasChildGeneric(IJsonSchemaValidationProperties property) { | ||
setVendorExtension(property, "x-has-child-generic", true); | ||
} | ||
Comment on lines
+23
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't even need them but I can understand you want to keep it to make it clearer |
||
|
||
/** | ||
* @return true if the vendor extensions of the property contains either x-propagated-generic or | ||
* x-has-child-generic | ||
*/ | ||
private static boolean hasGeneric(IJsonSchemaValidationProperties property) { | ||
if (property instanceof CodegenModel) { | ||
return ( | ||
(boolean) ((CodegenModel) property).vendorExtensions.getOrDefault("x-propagated-generic", false) || | ||
(boolean) ((CodegenModel) property).vendorExtensions.getOrDefault("x-has-child-generic", false) | ||
); | ||
} else if (property instanceof CodegenProperty) { | ||
return ( | ||
(boolean) ((CodegenProperty) property).vendorExtensions.getOrDefault("x-propagated-generic", false) || | ||
(boolean) ((CodegenProperty) property).vendorExtensions.getOrDefault("x-has-child-generic", false) | ||
); | ||
} | ||
return false; | ||
} | ||
|
||
private static CodegenModel propertyToModel(Map<String, CodegenModel> models, CodegenProperty prop) { | ||
// openapi generator returns some weird error when looking for primitive type, | ||
// so we filter them by hand | ||
if (prop == null || primitiveModels.contains(prop.openApiType) || !models.containsKey(prop.openApiType)) { | ||
return null; | ||
} | ||
return models.get(prop.openApiType); | ||
} | ||
|
||
private static boolean markPropagatedGeneric(IJsonSchemaValidationProperties model) { | ||
CodegenProperty items = model.getItems(); | ||
millotp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// if items itself isn't generic, we recurse on its items and properties until we reach the | ||
// end or find a generic property | ||
if (items != null && ((boolean) items.vendorExtensions.getOrDefault("x-is-generic", false) || markPropagatedGeneric(items))) { | ||
setPropagatedGeneric(model); | ||
return true; | ||
} | ||
for (CodegenProperty var : model.getVars()) { | ||
// same thing for the var, if it's not a generic, we recurse on it until we find one | ||
if ((boolean) var.vendorExtensions.getOrDefault("x-is-generic", false) || markPropagatedGeneric(var)) { | ||
setPropagatedGeneric(model); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean propagateGenericRecursive(Map<String, CodegenModel> models, IJsonSchemaValidationProperties property) { | ||
CodegenProperty items = property.getItems(); | ||
// if items itself isn't generic, we recurse on its items and properties (and it's | ||
// equivalent model if we find one) until we reach the end or find a generic property. | ||
// We need to check the model too because the tree isn't complete sometime, depending on the ref | ||
// in the spec, so we get the model with the same name and recurse. | ||
if (items != null && ((hasGeneric(items) || propagateGenericRecursive(models, items) || hasGeneric(propertyToModel(models, items))))) { | ||
setHasChildGeneric(property); | ||
return true; | ||
} | ||
for (CodegenProperty var : property.getVars()) { | ||
// same thing for the var | ||
if (hasGeneric(var) || propagateGenericRecursive(models, var) || hasGeneric(propertyToModel(models, var))) { | ||
setHasChildGeneric(property); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static Map<String, CodegenModel> convertToMap(Map<String, ModelsMap> models) { | ||
Map<String, CodegenModel> modelsMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); | ||
for (ModelsMap modelMap : models.values()) { | ||
// modelContainers always have 1 and only 1 model in our specs | ||
CodegenModel model = modelMap.getModels().get(0).getModel(); | ||
modelsMap.put(model.name, model); | ||
} | ||
return modelsMap; | ||
} | ||
|
||
private static Map<String, CodegenModel> convertToMap(List<ModelMap> models) { | ||
Map<String, CodegenModel> modelsMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); | ||
for (ModelMap modelMap : models) { | ||
CodegenModel model = modelMap.getModel(); | ||
modelsMap.put(model.name, model); | ||
} | ||
return modelsMap; | ||
} | ||
|
||
/** | ||
* Models and their members will be marked with either x-propagated-generic or x-has-child-generic | ||
*/ | ||
public static void propagateGenericsToModels(Map<String, ModelsMap> modelsMap) { | ||
// We propagate generics in two phases: | ||
// 1. We mark the direct parent of the generic model to replace it with T | ||
// 2. We tell each parent with generic properties to pass that generic type all the way down | ||
|
||
Map<String, CodegenModel> models = convertToMap(modelsMap); | ||
|
||
for (CodegenModel model : models.values()) { | ||
markPropagatedGeneric(model); | ||
} | ||
|
||
for (CodegenModel model : models.values()) { | ||
propagateGenericRecursive(models, model); | ||
} | ||
} | ||
|
||
/** Mark operations with a generic return type with x-is-generic */ | ||
public static void propagateGenericsToOperations(OperationsMap operations, List<ModelMap> allModels) { | ||
Map<String, CodegenModel> models = convertToMap(allModels); | ||
for (CodegenOperation ope : operations.getOperations().getOperation()) { | ||
CodegenModel returnType = models.get(ope.returnType); | ||
if (returnType != null && hasGeneric(returnType)) { | ||
ope.vendorExtensions.put("x-is-generic", true); | ||
// we use {{#optionalParams.0}} to check for optionalParams, so we loose the | ||
// vendorExtensions at the operation level | ||
if (ope.optionalParams.size() > 0) { | ||
ope.optionalParams.get(0).vendorExtensions.put("x-is-generic", true); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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.
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.