Skip to content

Commit 9a0890c

Browse files
author
Thibaud SOWA
committed
fix(typescript-angular): fix new angular dependency cli option usage
1 parent 0183620 commit 9a0890c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2911
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: typescript-angular
2+
outputDir: samples/client/petstore/typescript-angular-v19-with-angular-dependency-params/builds/default
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/typescript-angular
5+
additionalProperties:
6+
ngVersion: 19.0.1
7+
npmName: sample-angular-19-0-0-with-angular-dependency-params
8+
supportsES6: true
9+
tsVersion: 5.6.3
10+
zonejsVersion: 0.15.0
11+
ngPackagrVersion: 19.0.1
12+
rxjsVersion: 6.5.3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
generatorName: typescript-angular
2-
outputDir: samples/client/petstore/typescript-angular-v19-provided-in-root/builds/default
2+
outputDir: samples/client/petstore/typescript-angular-v19/builds/default
33
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
44
templateDir: modules/openapi-generator/src/main/resources/typescript-angular
55
additionalProperties:
66
ngVersion: 19.0.0
7+
npmName: sample-angular-19-0-0
78
supportsES6: true

docs/generators/typescript-angular.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5656
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |false|
5757
|useSquareBracketsInArrayNames|Setting this property to true will add brackets to array attribute names, e.g. my_values[].| |false|
5858
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
59-
|zoneJsVersion|The version of zone.js compatible with Angular (see ngVersion option).| |null|
59+
|zonejsVersion|The version of zone.js compatible with Angular (see ngVersion option).| |null|
6060

6161
## IMPORT MAPPING
6262

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static enum PROVIDED_IN_LEVEL {none, root, any, platform}
8181
public static final String TS_VERSION = "tsVersion";
8282
public static final String RXJS_VERSION = "rxjsVersion";
8383
public static final String NGPACKAGR_VERSION = "ngPackagrVersion";
84-
public static final String ZONEJS_VERSION = "zoneJsVersion";
84+
public static final String ZONEJS_VERSION = "zonejsVersion";
8585

8686
protected String ngVersion = "19.0.0";
8787
@Getter @Setter
@@ -321,21 +321,21 @@ private void addNpmPackageGeneration(SemVer ngVersion) {
321321
.map(Map.Entry::getValue)
322322
.orElseThrow(() -> new IllegalArgumentException("Invalid ngVersion. Only Angular v9+ is supported."));
323323

324-
additionalProperties.put(TS_VERSION, additionalProperties.containsKey(TS_VERSION)
325-
? additionalProperties.containsKey(TS_VERSION)
326-
: angularDependencies.getTsVersion());
324+
if (!additionalProperties.containsKey(TS_VERSION)) {
325+
additionalProperties.put(TS_VERSION, angularDependencies.getTsVersion());
326+
}
327327

328-
additionalProperties.put(RXJS_VERSION, additionalProperties.containsKey(RXJS_VERSION)
329-
? additionalProperties.containsKey(RXJS_VERSION)
330-
: angularDependencies.getRxjsVersion());
328+
if (!additionalProperties.containsKey(RXJS_VERSION)) {
329+
additionalProperties.put(RXJS_VERSION, angularDependencies.getRxjsVersion());
330+
}
331331

332-
additionalProperties.put(NGPACKAGR_VERSION, additionalProperties.containsKey(NGPACKAGR_VERSION)
333-
? additionalProperties.containsKey(NGPACKAGR_VERSION)
334-
: angularDependencies.getNgPackagrVersion());
332+
if (!additionalProperties.containsKey(NGPACKAGR_VERSION)) {
333+
additionalProperties.put(NGPACKAGR_VERSION, angularDependencies.getNgPackagrVersion());
334+
}
335335

336-
additionalProperties.put("zonejsVersion", additionalProperties.containsKey(ZONEJS_VERSION)
337-
? additionalProperties.containsKey(ZONEJS_VERSION)
338-
: angularDependencies.getZonejsVersion());
336+
if (!additionalProperties.containsKey(ZONEJS_VERSION)) {
337+
additionalProperties.put(ZONEJS_VERSION, angularDependencies.getZonejsVersion());
338+
}
339339

340340
if (angularDependencies.getTsickleVersion() != null) {
341341
additionalProperties.put("tsickleVersion", angularDependencies.getTsickleVersion());

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class TypeScriptAngularClientOptionsProvider implements TypeScriptSharedClientOptionsProvider {
2626
public static final String STRING_ENUMS_VALUE = "false";
2727
private static final String NPM_REPOSITORY = "https://registry.npmjs.org";
28-
public static final String NG_VERSION = "12";
28+
public static final String NG_VERSION = "19";
2929
public static final String FILE_NAMING_VALUE = "camelCase";
3030
public static final String API_MODULE_PREFIX = "";
3131
public static final String CONFIGURATION_PREFIX = "";
@@ -37,7 +37,7 @@ public class TypeScriptAngularClientOptionsProvider implements TypeScriptSharedC
3737
public static final String MODEL_FILE_SUFFIX = "";
3838
public static final String TS_VERSION = "";
3939
public static final String RXJS_VERSION = "";
40-
public static final String NGPACKAGR_VERSION = " ";
40+
public static final String NGPACKAGR_VERSION = "";
4141
public static final String ZONEJS_VERSION = "";
4242

4343
@Override

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java

+46
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.HashMap;
2222
import java.util.Map;
2323

24+
import static org.assertj.core.api.Assertions.assertThat;
25+
2426

2527
@Test(groups = {TypeScriptGroups.TYPESCRIPT, TypeScriptGroups.TYPESCRIPT_ANGULAR})
2628
public class TypeScriptAngularClientCodegenTest {
@@ -344,4 +346,48 @@ public void testModelNameMappings() throws Exception {
344346
"files?: Array<SystemFile>;" // ensure it's an array of SystemFile (not Any)
345347
);
346348
}
349+
350+
@Test
351+
public void testAngularDependenciesFromCliOptions() {
352+
// GIVEN
353+
OpenAPI openAPI = TestUtils.createOpenAPI();
354+
355+
TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();
356+
codegen.additionalProperties().put("npmName", "@openapi/typescript-angular-petstore");
357+
codegen.additionalProperties().put("tsVersion", "12");
358+
codegen.additionalProperties().put("rxjsVersion", "23");
359+
codegen.additionalProperties().put("ngPackagrVersion", "34");
360+
codegen.additionalProperties().put("zonejsVersion", "45");
361+
362+
// WHEN
363+
codegen.processOpts();
364+
codegen.preprocessOpenAPI(openAPI);
365+
366+
// THEN
367+
assertThat(codegen.additionalProperties()).containsEntry("tsVersion", "12");
368+
assertThat(codegen.additionalProperties()).containsEntry("rxjsVersion", "23");
369+
assertThat(codegen.additionalProperties()).containsEntry("ngPackagrVersion", "34");
370+
assertThat(codegen.additionalProperties()).containsEntry("zonejsVersion", "45");
371+
}
372+
373+
@Test
374+
public void testAngularDependenciesFromConfigFile() {
375+
// GIVEN
376+
OpenAPI openAPI = TestUtils.createOpenAPI();
377+
378+
TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();
379+
codegen.additionalProperties().put("npmName", "@openapi/typescript-angular-petstore");
380+
// We fix ngVersion to do not update this test on every new angular release.
381+
codegen.additionalProperties().put("ngVersion", "19.0.0");
382+
383+
// WHEN
384+
codegen.processOpts();
385+
codegen.preprocessOpenAPI(openAPI);
386+
387+
// THEN
388+
assertThat(codegen.additionalProperties()).containsEntry("tsVersion", ">=5.5.0 <5.7.0");
389+
assertThat(codegen.additionalProperties()).containsEntry("rxjsVersion", "7.4.0");
390+
assertThat(codegen.additionalProperties()).containsEntry("ngPackagrVersion", "19.0.0");
391+
assertThat(codegen.additionalProperties()).containsEntry("zonejsVersion", "0.15.0");
392+
}
347393
}

samples/client/petstore/typescript-angular-v19-provided-in-root/builds/default/.openapi-generator/FILES samples/client/petstore/typescript-angular-v19-with-angular-dependency-params/builds/default/.openapi-generator/FILES

+3
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ model/order.ts
1616
model/pet.ts
1717
model/tag.ts
1818
model/user.ts
19+
ng-package.json
20+
package.json
1921
param.ts
22+
tsconfig.json
2023
variables.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
2+
3+
This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
4+
5+
The version of the OpenAPI document: 1.0.0
6+
7+
## Building
8+
9+
To install the required dependencies and to build the typescript sources run:
10+
11+
```console
12+
npm install
13+
npm run build
14+
```
15+
16+
## Publishing
17+
18+
First build the package then run `npm publish dist` (don't forget to specify the `dist` folder!)
19+
20+
## Consuming
21+
22+
Navigate to the folder of your consuming project and run one of next commands.
23+
24+
_published:_
25+
26+
```console
27+
npm install [email protected] --save
28+
```
29+
30+
_without publishing (not recommended):_
31+
32+
```console
33+
npm install PATH_TO_GENERATED_PACKAGE/dist.tgz --save
34+
```
35+
36+
_It's important to take the tgz file, otherwise you'll get trouble with links on windows_
37+
38+
_using `npm link`:_
39+
40+
In PATH_TO_GENERATED_PACKAGE/dist:
41+
42+
```console
43+
npm link
44+
```
45+
46+
In your project:
47+
48+
```console
49+
npm link sample-angular-19-0-0-with-angular-dependency-params
50+
```
51+
52+
__Note for Windows users:__ The Angular CLI has troubles to use linked npm packages.
53+
Please refer to this issue <https://github.com/angular/angular-cli/issues/8284> for a solution / workaround.
54+
Published packages are not effected by this issue.
55+
56+
### General usage
57+
58+
In your Angular project:
59+
60+
```typescript
61+
// without configuring providers
62+
import { ApiModule } from 'sample-angular-19-0-0-with-angular-dependency-params';
63+
import { HttpClientModule } from '@angular/common/http';
64+
65+
@NgModule({
66+
imports: [
67+
ApiModule,
68+
// make sure to import the HttpClientModule in the AppModule only,
69+
// see https://github.com/angular/angular/issues/20575
70+
HttpClientModule
71+
],
72+
declarations: [ AppComponent ],
73+
providers: [],
74+
bootstrap: [ AppComponent ]
75+
})
76+
export class AppModule {}
77+
```
78+
79+
```typescript
80+
// configuring providers
81+
import { ApiModule, Configuration, ConfigurationParameters } from 'sample-angular-19-0-0-with-angular-dependency-params';
82+
83+
export function apiConfigFactory (): Configuration {
84+
const params: ConfigurationParameters = {
85+
// set configuration parameters here.
86+
}
87+
return new Configuration(params);
88+
}
89+
90+
@NgModule({
91+
imports: [ ApiModule.forRoot(apiConfigFactory) ],
92+
declarations: [ AppComponent ],
93+
providers: [],
94+
bootstrap: [ AppComponent ]
95+
})
96+
export class AppModule {}
97+
```
98+
99+
```typescript
100+
// configuring providers with an authentication service that manages your access tokens
101+
import { ApiModule, Configuration } from 'sample-angular-19-0-0-with-angular-dependency-params';
102+
103+
@NgModule({
104+
imports: [ ApiModule ],
105+
declarations: [ AppComponent ],
106+
providers: [
107+
{
108+
provide: Configuration,
109+
useFactory: (authService: AuthService) => new Configuration(
110+
{
111+
basePath: environment.apiUrl,
112+
accessToken: authService.getAccessToken.bind(authService)
113+
}
114+
),
115+
deps: [AuthService],
116+
multi: false
117+
}
118+
],
119+
bootstrap: [ AppComponent ]
120+
})
121+
export class AppModule {}
122+
```
123+
124+
```typescript
125+
import { DefaultApi } from 'sample-angular-19-0-0-with-angular-dependency-params';
126+
127+
export class AppComponent {
128+
constructor(private apiGateway: DefaultApi) { }
129+
}
130+
```
131+
132+
Note: The ApiModule is restricted to being instantiated once app wide.
133+
This is to ensure that all services are treated as singletons.
134+
135+
### Using multiple OpenAPI files / APIs / ApiModules
136+
137+
In order to use multiple `ApiModules` generated from different OpenAPI files,
138+
you can create an alias name when importing the modules
139+
in order to avoid naming conflicts:
140+
141+
```typescript
142+
import { ApiModule } from 'my-api-path';
143+
import { ApiModule as OtherApiModule } from 'my-other-api-path';
144+
import { HttpClientModule } from '@angular/common/http';
145+
146+
@NgModule({
147+
imports: [
148+
ApiModule,
149+
OtherApiModule,
150+
// make sure to import the HttpClientModule in the AppModule only,
151+
// see https://github.com/angular/angular/issues/20575
152+
HttpClientModule
153+
]
154+
})
155+
export class AppModule {
156+
157+
}
158+
```
159+
160+
### Set service base path
161+
162+
If different than the generated base path, during app bootstrap, you can provide the base path to your service.
163+
164+
```typescript
165+
import { BASE_PATH } from 'sample-angular-19-0-0-with-angular-dependency-params';
166+
167+
bootstrap(AppComponent, [
168+
{ provide: BASE_PATH, useValue: 'https://your-web-service.com' },
169+
]);
170+
```
171+
172+
or
173+
174+
```typescript
175+
import { BASE_PATH } from 'sample-angular-19-0-0-with-angular-dependency-params';
176+
177+
@NgModule({
178+
imports: [],
179+
declarations: [ AppComponent ],
180+
providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ],
181+
bootstrap: [ AppComponent ]
182+
})
183+
export class AppModule {}
184+
```
185+
186+
### Using @angular/cli
187+
188+
First extend your `src/environments/*.ts` files by adding the corresponding base path:
189+
190+
```typescript
191+
export const environment = {
192+
production: false,
193+
API_BASE_PATH: 'http://127.0.0.1:8080'
194+
};
195+
```
196+
197+
In the src/app/app.module.ts:
198+
199+
```typescript
200+
import { BASE_PATH } from 'sample-angular-19-0-0-with-angular-dependency-params';
201+
import { environment } from '../environments/environment';
202+
203+
@NgModule({
204+
declarations: [
205+
AppComponent
206+
],
207+
imports: [ ],
208+
providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }],
209+
bootstrap: [ AppComponent ]
210+
})
211+
export class AppModule { }
212+
```
213+
214+
### Customizing path parameter encoding
215+
216+
Without further customization, only [path-parameters][parameter-locations-url] of [style][style-values-url] 'simple'
217+
and Dates for format 'date-time' are encoded correctly.
218+
219+
Other styles (e.g. "matrix") are not that easy to encode
220+
and thus are best delegated to other libraries (e.g.: [@honoluluhenk/http-param-expander]).
221+
222+
To implement your own parameter encoding (or call another library),
223+
pass an arrow-function or method-reference to the `encodeParam` property of the Configuration-object
224+
(see [General Usage](#general-usage) above).
225+
226+
Example value for use in your Configuration-Provider:
227+
228+
```typescript
229+
new Configuration({
230+
encodeParam: (param: Param) => myFancyParamEncoder(param),
231+
})
232+
```
233+
234+
[parameter-locations-url]: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameter-locations
235+
[style-values-url]: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#style-values
236+
[@honoluluhenk/http-param-expander]: https://www.npmjs.com/package/@honoluluhenk/http-param-expander

0 commit comments

Comments
 (0)