Skip to content

Commit

Permalink
fix(clients): add generation banner to generated files (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Jul 12, 2022
1 parent 042b5a5 commit 4b55738
Show file tree
Hide file tree
Showing 24 changed files with 67 additions and 9 deletions.
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.

0 comments on commit 4b55738

Please sign in to comment.