Skip to content

fix(clients): add generation banner to generated files #816

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 3 commits into from
Jul 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public void processOpts() {

super.processOpts();

// Generation notice, added on every generated files
Utils.setGenerationBanner(additionalProperties);

// Prevent all useless file to generate
apiTestTemplateFiles.clear();
modelTestTemplateFiles.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public void processOpts() {
setModelPropertyNaming("original");
setApiPackage("src");

// Generation notice, added on every generated files
Utils.setGenerationBanner(additionalProperties);

languageSpecificPrimitives.add("Record");
instantiationTypes.put("map", "Record");
// clear all supported files to avoid unwanted ones
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public void processOpts() {

super.processOpts();

// Generation notice, added on every generated files
Utils.setGenerationBanner(additionalProperties);

// Remove base template as we want to change its path
supportingFiles.removeIf(file -> file.getTemplateFile().equals("Configuration.mustache"));

Expand Down
11 changes: 11 additions & 0 deletions generators/src/main/java/com/algolia/codegen/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,15 @@ public static JsonNode readJsonFile(String filePath) throws ConfigException {
public static boolean shouldUseExplicitOneOfName(Collection<String> oneOf) {
return oneOf.stream().filter(type -> type != null && type.startsWith("List")).count() >= 2;
}

/**
* Sets a `generationBanner` variable on the mustache templates, to display the generation banner
* on generated files.
*/
public static void setGenerationBanner(Map<String, Object> additionalProperties) {
additionalProperties.put(
"generationBanner",
"This file is generated, manual changes will be lost - read more on" + " https://github.com/algolia/api-clients-automation."
);
}
}
2 changes: 2 additions & 0 deletions templates/java/api.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

package {{package}};

import {{invokerPackage}}.ApiClient;
Expand Down
2 changes: 2 additions & 0 deletions templates/java/model.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

package {{package}};

import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

import {
createMemoryCache,
createFallbackableCache,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

import type {
Host,
Requester,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

import {
DEFAULT_CONNECT_TIMEOUT_NODE,
DEFAULT_READ_TIMEOUT_NODE,
Expand Down
2 changes: 2 additions & 0 deletions templates/javascript/clients/api-single.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

{{> client/api/imports}}

export const apiClientVersion = '{{packageVersion}}';
Expand Down
2 changes: 2 additions & 0 deletions templates/javascript/clients/client/builds/browser.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

import { createXhrRequester } from '{{{npmNamespace}}}/requester-browser-xhr';
import { DEFAULT_CONNECT_TIMEOUT_BROWSER, DEFAULT_READ_TIMEOUT_BROWSER, DEFAULT_WRITE_TIMEOUT_BROWSER } from '{{{npmNamespace}}}/client-common';
{{> client/builds/imports}}
Expand Down
2 changes: 2 additions & 0 deletions templates/javascript/clients/client/builds/node.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

import { createHttpRequester } from '{{{npmNamespace}}}/requester-node-http';
import { DEFAULT_CONNECT_TIMEOUT_NODE, DEFAULT_READ_TIMEOUT_NODE, DEFAULT_WRITE_TIMEOUT_NODE } from '{{{npmNamespace}}}/client-common';
{{> client/builds/imports}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

{{#apiInfo.apis.0}}

{{#imports}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

{{#models}}
{{#model}}
export * from '{{{ classFilename }}}';
Expand Down
2 changes: 2 additions & 0 deletions templates/javascript/clients/model.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// {{{generationBanner}}}

{{#models}}{{#model}}
{{#tsImports}}
import { {{classname}} } from '{{filename}}';
Expand Down
1 change: 1 addition & 0 deletions templates/php/ApiException.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// {{{generationBanner}}}
<?php

namespace {{invokerPackage}};
Expand Down
1 change: 1 addition & 0 deletions templates/php/ConfigWithRegion.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// {{{generationBanner}}}

namespace {{invokerPackage}}\Configuration;

Expand Down
1 change: 1 addition & 0 deletions templates/php/Configuration.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// {{{generationBanner}}}

namespace {{invokerPackage}}\Configuration;

Expand Down
1 change: 1 addition & 0 deletions templates/php/ModelInterface.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// {{{generationBanner}}}

namespace {{modelPackage}};

Expand Down
1 change: 1 addition & 0 deletions templates/php/ObjectSerializer.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// {{{generationBanner}}}

namespace {{invokerPackage}};

Expand Down
1 change: 1 addition & 0 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// {{{generationBanner}}}

namespace {{apiPackage}};

Expand Down
1 change: 1 addition & 0 deletions templates/php/client_config.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// {{{generationBanner}}}

namespace {{invokerPackage}}\Configuration;

Expand Down
2 changes: 2 additions & 0 deletions templates/php/model.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
// {{{generationBanner}}}

{{#models}}
{{#model}}

Expand Down
25 changes: 16 additions & 9 deletions website/docs/contributing/add-new-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ The resulting User Agent is the concatenation of `base`, then `client` and all t

For example, if we have:

- base: `Algolia for Java (4.0.0)`
- client: `; Search (4.0.0)`
- segment:
- `; JVM (11.0.14)`
- `; experimental`
- `; test (8.0.0-beta)`
- base: `Algolia for Java (4.0.0)`
- client: `; Search (4.0.0)`
- segment:
- `; JVM (11.0.14)`
- `; experimental`
- `; test (8.0.0-beta)`

Then the resulting User Agent is (the order is arbitrary):

Expand All @@ -109,6 +109,7 @@ Algolia for Java (4.0.0); JVM (11.0.14); Search (4.0.0); experimental ; test (8.
You can take a look at the Java implementation [here](https://github.com/algolia/api-clients-automation/pull/347).

The `User-Agent` MUST match the following regular expression:

```regex
^Algolia for <LANGUAGE> \\(\\d+\\.\\d+\\.\\d+(-.*)?\\)(; [a-zA-Z. ]+ (\\(\\d+((\\.\\d+)?\\.\\d+)?(-.*)?\\))?)*(; <CLIENT> (\\(\\d+\\.\\d+\\.\\d+(-.*)?\\)))(; [a-zA-Z. ]+ (\\(\\d+((\\.\\d+)?\\.\\d+)?(-.*)?\\))?)*$
```
Expand Down Expand Up @@ -140,13 +141,19 @@ You can take a look at the implementation over all clients, [in this pull reques

Every methods of every clients provide a parameter that does not exist in the REST API, called `requestOptions`.

This parameter **must** be the last parameter of a method, and allow a user to override/merge additional `query parameters` or `headers` with the transporter ones.
This parameter MUST be the last parameter of a method, and allow a user to override/merge additional `query parameters` or `headers` with the transporter ones.

### Requesters

The requester needs to be configurable and capable to be overwriten to use a user defined requester.
The requester needs to be configurable and capable to be overwritten to use a user defined requester.
The default requester must be the classic HTTP requester, and it's possible to provide other alternatives like an [Echo Requester](https://github.com/algolia/api-clients-automation/blob/main/clients/algoliasearch-client-javascript/packages/client-common/src/createEchoRequester.ts) that just send the request back.

### Logger

> TODO: informations
> TODO: information

### Generated file banner

The banner SHOULD be added at the very top of every generated files, to avoid confusion for our users, but also redirect them to the `api-clients-automation` monorepo, where the contribution happens.

To do so, a `generationBanner` variable is available on every `mustache` templates, see [the initial implementation](https://github.com/algolia/api-clients-automation/pull/816) for more context.
Copy link
Collaborator

Choose a reason for hiding this comment

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

recursion 🤯