Skip to content
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

feat(generator): add custom generator to fix customRequest #315

Merged
merged 7 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/.cache_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.0.1
7
8 changes: 5 additions & 3 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ runs:

echo "::set-output name=SCRIPTS_CHANGED::$(git diff --shortstat $origin..HEAD -- scripts | wc -l)"

echo "::set-output name=GENERATORS_CHANGED::$(git diff --shortstat $origin..HEAD -- generators | wc -l)"

echo "::set-output name=JS_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript | wc -l)"
echo "::set-output name=JS_ALGOLIASEARCH_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/algoliasearch clients/algoliasearch-client-javascript/packages/client-search clients/algoliasearch-client-javascript/packages/client-analytics clients/algoliasearch-client-javascript/packages/client-personalization | wc -l)"
echo "::set-output name=JS_COMMON_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/client-common clients/algoliasearch-client-javascript/packages/requester-browser-xhr clients/algoliasearch-client-javascript/packages/requester-node-http | wc -l)"
Expand Down Expand Up @@ -95,7 +97,7 @@ runs:
id: js-matrix
shell: bash
run: |
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.JS_TEMPLATE_CHANGED > 0 || steps.diff.outputs.JS_COMMON_CHANGED > 0 }}
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.GENERATORS_CHANGED > 0 || steps.diff.outputs.JS_TEMPLATE_CHANGED > 0 || steps.diff.outputs.JS_COMMON_CHANGED > 0 }}
algoliasearch_changed=${{ steps.diff.outputs.JS_ALGOLIASEARCH_CHANGED > 0 }}
common_changed=${{ steps.diff.outputs.JS_COMMON_CHANGED > 0 }}

Expand Down Expand Up @@ -128,7 +130,7 @@ runs:
id: java-matrix
shell: bash
run: |
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.JAVA_TEMPLATE_CHANGED > 0 }}
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.GENERATORS_CHANGED > 0 || steps.diff.outputs.JAVA_TEMPLATE_CHANGED > 0 }}

matrix=$(./scripts/ci/create-client-matrix.sh java $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})

Expand All @@ -149,7 +151,7 @@ runs:
id: php-matrix
shell: bash
run: |
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.PHP_TEMPLATE_CHANGED > 0 }}
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.GENERATORS_CHANGED > 0 || steps.diff.outputs.PHP_TEMPLATE_CHANGED > 0 }}

matrix=$(./scripts/ci/create-client-matrix.sh php $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
- name: Pull Request title rules
uses: deepakputhraya/action-pr-title@v1.0.2
with:
regex: '^(docs|chore)|((?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)\((?:javascript|php|java|cts|spec|script|ci)\)): .+'
regex: '^(docs|chore)|((?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)\((?:generator|javascript|php|java|cts|spec|script|ci)\)): .+'
10 changes: 6 additions & 4 deletions config/clients.config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"java": {
"folder": "clients/algoliasearch-client-java-2",
"mainGenerator": "java-search",
"mainPackage": "java-search",
Copy link
Member

Choose a reason for hiding this comment

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

Not needed change right now IMO but tagging @eunjae-lee for visiblity

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't want the confusion with customGenerator and mainPackage, they are completely unrelated.

Copy link
Member

Choose a reason for hiding this comment

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

Yep I totally agree, just stating that it's out of the scope of this PR, but not a huge deal here

"modelFolder": "algoliasearch-core/com/algolia/model",
"customGenerator": "algolia-java",
"tests": {
Expand All @@ -11,16 +11,18 @@
},
"javascript": {
"folder": "clients/algoliasearch-client-javascript",
"mainGenerator": "javascript-search",
"mainPackage": "javascript-search",
"modelFolder": "model",
"customGenerator": "algolia-javascript",
"tests": {
"extension": ".test.ts",
"outputFolder": "src"
}
},
"php": {
"folder": "clients/algoliasearch-client-php",
"mainGenerator": "php-search",
"modelFolder": "lib/Model"
"mainPackage": "php-search",
"modelFolder": "lib/Model",
"customGenerator": "algolia-php"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import org.yaml.snakeyaml.Yaml;

import java.util.*;
import java.util.Map.Entry;
import java.io.FileInputStream;
import java.net.URL;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;

@SuppressWarnings("unchecked")
public class AlgoliaJavaGenerator extends JavaClientCodegen {
Expand Down Expand Up @@ -93,6 +94,11 @@ private void generateServer(Map<String, Object> client) {
}
}

@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers));
}

/**
* Provides an opportunity to inspect and modify operation data before the code
* is generated.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.algolia.codegen;

import java.util.List;

import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.languages.TypeScriptNodeClientCodegen;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;

public class AlgoliaJavascriptGenerator extends TypeScriptNodeClientCodegen {
@Override
public String getName() {
return "algolia-javascript";
}

@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.algolia.codegen;

import java.util.List;

import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.languages.PhpClientCodegen;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;

public class AlgoliaPhpGenerator extends PhpClientCodegen {
@Override
public String getName() {
return "algolia-php";
}

@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers));
}
}
19 changes: 19 additions & 0 deletions generators/src/main/java/com/algolia/codegen/Utils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package com.algolia.codegen;

import java.util.Set;

import com.google.common.collect.Sets;

import org.openapitools.codegen.CodegenOperation;

public class Utils {
public static final Set<String> CUSTOM_METHOD = Sets.newHashSet("del", "get", "post", "put");

public static String capitalize(String str) {
return str.substring(0, 1).toUpperCase() + str.substring(1);
}

/**
* Will add the boolean `vendorExtensions.x-is-custom-request` to operations if
* they should not escape '/' in the path variable
*/
public static CodegenOperation specifyCustomRequest(CodegenOperation ope) {
if (CUSTOM_METHOD.contains(ope.nickname)) {
ope.vendorExtensions.put("x-is-custom-request", true);
}
return ope;
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
com.algolia.codegen.AlgoliaJavaGenerator
com.algolia.codegen.AlgoliaJavascriptGenerator
com.algolia.codegen.AlgoliaPhpGenerator
com.algolia.codegen.cts.AlgoliaCtsGenerator
32 changes: 16 additions & 16 deletions openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "5.4.0",
"generators": {
"javascript-search": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand All @@ -26,7 +26,7 @@
}
},
"javascript-recommend": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand All @@ -48,7 +48,7 @@
}
},
"javascript-personalization": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand All @@ -73,7 +73,7 @@
}
},
"javascript-analytics": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand All @@ -99,7 +99,7 @@
}
},
"javascript-insights": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand All @@ -125,7 +125,7 @@
}
},
"javascript-abtesting": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand All @@ -151,7 +151,7 @@
}
},
"javascript-query-suggestions": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand All @@ -176,7 +176,7 @@
}
},
"javascript-sources": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand All @@ -201,7 +201,7 @@
}
},
"javascript-predict": {
"generatorName": "typescript-node",
"generatorName": "algolia-javascript",
"templateDir": "#{cwd}/templates/javascript/",
"config": "#{cwd}/openapitools.json",
"apiPackage": "src",
Expand Down Expand Up @@ -246,7 +246,7 @@
}
},
"php-search": {
"generatorName": "php",
"generatorName": "algolia-php",
"templateDir": "#{cwd}/templates/php/",
"config": "#{cwd}/openapitools.json",
"output": "#{cwd}/clients/algoliasearch-client-php",
Expand All @@ -265,7 +265,7 @@
}
},
"php-recommend": {
"generatorName": "php",
"generatorName": "algolia-php",
"templateDir": "#{cwd}/templates/php/",
"config": "#{cwd}/openapitools.json",
"output": "#{cwd}/clients/algoliasearch-client-php",
Expand All @@ -284,7 +284,7 @@
}
},
"php-personalization": {
"generatorName": "php",
"generatorName": "algolia-php",
"templateDir": "#{cwd}/templates/php/",
"config": "#{cwd}/openapitools.json",
"output": "#{cwd}/clients/algoliasearch-client-php",
Expand All @@ -307,7 +307,7 @@
}
},
"php-analytics": {
"generatorName": "php",
"generatorName": "algolia-php",
"templateDir": "#{cwd}/templates/php/",
"config": "#{cwd}/openapitools.json",
"output": "#{cwd}/clients/algoliasearch-client-php",
Expand All @@ -331,7 +331,7 @@
}
},
"php-insights": {
"generatorName": "php",
"generatorName": "algolia-php",
"templateDir": "#{cwd}/templates/php/",
"config": "#{cwd}/openapitools.json",
"output": "#{cwd}/clients/algoliasearch-client-php",
Expand All @@ -355,7 +355,7 @@
}
},
"php-abtesting": {
"generatorName": "php",
"generatorName": "algolia-php",
"templateDir": "#{cwd}/templates/php/",
"config": "#{cwd}/openapitools.json",
"output": "#{cwd}/clients/algoliasearch-client-php",
Expand All @@ -379,7 +379,7 @@
}
},
"php-query-suggestions": {
"generatorName": "php",
"generatorName": "algolia-php",
"templateDir": "#{cwd}/templates/php/",
"config": "#{cwd}/openapitools.json",
"output": "#{cwd}/clients/algoliasearch-client-php",
Expand Down
8 changes: 4 additions & 4 deletions scripts/release/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export const RELEASED_TAG = config.releasedTag;
export const MAIN_BRANCH = config.mainBranch;
export const OWNER = config.owner;
export const REPO = config.repo;
export const MAIN_GENERATOR = Object.keys(clientsConfig).reduce(
(mainGenerator: { [lang: string]: string }, lang: string) => {
export const MAIN_PACKAGE = Object.keys(clientsConfig).reduce(
(mainPackage: { [lang: string]: string }, lang: string) => {
return {
...mainGenerator,
[lang]: clientsConfig[lang].mainGenerator,
...mainPackage,
[lang]: clientsConfig[lang].mainPackage,
};
},
{}
Expand Down
12 changes: 3 additions & 9 deletions scripts/release/create-release-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import semver from 'semver';
import { LANGUAGES, ROOT_ENV_PATH, run, getPackageVersion } from '../common';
import type { Language } from '../types';

import {
RELEASED_TAG,
MAIN_BRANCH,
OWNER,
REPO,
MAIN_GENERATOR,
} from './common';
import { RELEASED_TAG, MAIN_BRANCH, OWNER, REPO, MAIN_PACKAGE } from './common';
import TEXT from './text';
import type {
Versions,
Expand All @@ -24,11 +18,11 @@ import type {
dotenv.config({ path: ROOT_ENV_PATH });

export function readVersions(): VersionsWithoutReleaseType {
return Object.keys(MAIN_GENERATOR).reduce((acc, lang) => {
return Object.keys(MAIN_PACKAGE).reduce((acc, lang) => {
return {
...acc,
[lang]: {
current: getPackageVersion(MAIN_GENERATOR[lang]),
current: getPackageVersion(MAIN_PACKAGE[lang]),
},
};
}, {});
Expand Down