Skip to content

feat(go): patch iaas/AreaID after generation to fix oneOf regex issue#277

Draft
cgoetz-inovex wants to merge 1 commit into
mainfrom
feat/STACKITSDK-226-post-process-patch-file
Draft

feat(go): patch iaas/AreaID after generation to fix oneOf regex issue#277
cgoetz-inovex wants to merge 1 commit into
mainfrom
feat/STACKITSDK-226-post-process-patch-file

Conversation

@cgoetz-inovex

Copy link
Copy Markdown
Contributor

STACKITSDK-226

Comment on lines +139 to +140
regex = regexp.MustCompile("^\\\\/|\\\\/$").ReplaceAllString(regex, "$1") // Remove beginning slash and ending slash
regex = regexp.MustCompile("\\\\\\\\(.)").ReplaceAllString(regex, "$1") // Remove duplicate escaping char for dots

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines +248 to +262
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"
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

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.

2 participants