feat(go): patch iaas/AreaID after generation to fix oneOf regex issue#277
feat(go): patch iaas/AreaID after generation to fix oneOf regex issue#277cgoetz-inovex wants to merge 1 commit into
Conversation
| regex = regexp.MustCompile("^\\\\/|\\\\/$").ReplaceAllString(regex, "$1") // Remove beginning slash and ending slash | ||
| regex = regexp.MustCompile("\\\\\\\\(.)").ReplaceAllString(regex, "$1") // Remove duplicate escaping char for dots |
There was a problem hiding this comment.
These two replaces were in the previous implementation, because the openapi generator didn't pass the regex 1:1 from the api schema. Instead it modified the regex a bit and we had to revert these changes. With this new approach we can copy the regex directly from the api spec and don't need this part anymore
There was a problem hiding this comment.
nice hint thanks, currently this part also generates invalid go code. I've probably made an error when escaping \.
I'd address this when we've decided on the general approach.
| if (file.getAbsolutePath().endsWith("iaas/v2api/model_area_id.go")) { | ||
| try { | ||
| var content = Files.readAllBytes(file.toPath()); | ||
| var digest = MessageDigest.getInstance("MD5"); | ||
| var hash = digest.digest(content); | ||
| if (Arrays.equals(hash, IAAS_AREA_ID_HASH)) { | ||
| Files.writeString(file.toPath(), IAAS_AREA_ID_PATCHED, StandardOpenOption.TRUNCATE_EXISTING); | ||
| } else { | ||
| throw new IllegalStateException( | ||
| "expected iaas/v2api/model_area_id.go to hash to " + | ||
| Arrays.toString(IAAS_AREA_ID_HASH) + " but got " + Arrays.toString(hash) + | ||
| "\nedit CustomRegionGenerator.java in the sdk-generator and update IAAS_AREA_ID_HASH" + | ||
| "to accept this change" | ||
| ); | ||
| } |
There was a problem hiding this comment.
This fixes it only for the v2api, but we have the same issue in v1, v2alpha and v2beta. I think it would be nice, if we have the option to apply the same fix for multiple versions of an service.
There was a problem hiding this comment.
Introducing a method patch(Path path, byte[] hash, String content) and declaring all 3 versions with their patches would be easiest.
To express sth. like 'apply one patch to all 3 versions' would be a bit more involved. If we use a git diff approach we would rely on stable line numbers for UnmarshalJSON accross all versions, which may work.
Another approach would be a tool like https://github.com/ast-grep/ast-grep, which should be able to express sth. like 'patch the UnmarshalJSON method by inserting this snippet before the 'if string(jsonString) != "{}"' statement. But I have never used ast-grep in this fashion / can't say how easy it would be to write the pattern + fix.
We could also roll our own with https://pkg.go.dev/go/parser
STACKITSDK-226