Skip to content

Commit ea4e566

Browse files
authored
Merge branch 'master' into issue-10105
2 parents 797f9e9 + c1ab184 commit ea4e566

30 files changed

+1768
-0
lines changed

src/main/java/io/swagger/codegen/v3/generators/dotnet/CSharpClientCodegen.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.swagger.codegen.v3.CodegenProperty;
1111
import io.swagger.codegen.v3.CodegenType;
1212
import io.swagger.codegen.v3.SupportingFile;
13+
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
1314
import io.swagger.v3.oas.models.media.Schema;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
@@ -491,6 +492,19 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
491492
return objs;
492493
}
493494

495+
@Override
496+
public String getSchemaType(Schema schema) {
497+
String schemaType = super.getSchemaType(schema);
498+
499+
if (schema.get$ref() != null) {
500+
final Schema refSchema = OpenAPIUtil.getSchemaFromName(schemaType, this.openAPI);
501+
if (refSchema != null && !isObjectSchema(refSchema) && (refSchema.getEnum() == null || refSchema.getEnum().isEmpty())) {
502+
schemaType = super.getSchemaType(refSchema);
503+
}
504+
}
505+
return schemaType;
506+
}
507+
494508
@Override
495509
public CodegenType getTag() {
496510
return CodegenType.CLIENT;
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package io.swagger.codegen.v3.generators.typescript;
2+
3+
import java.io.File;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
7+
import io.swagger.codegen.v3.CliOption;
8+
import io.swagger.codegen.v3.CodegenModel;
9+
import io.swagger.codegen.v3.SupportingFile;
10+
import io.swagger.v3.oas.models.media.ArraySchema;
11+
import io.swagger.v3.oas.models.media.BinarySchema;
12+
import io.swagger.v3.oas.models.media.FileSchema;
13+
import io.swagger.v3.oas.models.media.MapSchema;
14+
import io.swagger.v3.oas.models.media.ObjectSchema;
15+
import io.swagger.v3.oas.models.media.Schema;
16+
import io.swagger.v3.parser.util.SchemaTypeUtil;
17+
18+
import org.apache.commons.lang3.StringUtils;
19+
20+
public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodegen {
21+
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
22+
23+
public static final String NPM_NAME = "npmName";
24+
public static final String NPM_VERSION = "npmVersion";
25+
public static final String NPM_REPOSITORY = "npmRepository";
26+
public static final String SNAPSHOT = "snapshot";
27+
public static final String WITH_INTERFACES = "withInterfaces";
28+
29+
protected String npmName = null;
30+
protected String npmVersion = "1.0.0";
31+
protected String npmRepository = null;
32+
33+
public TypeScriptFetchClientCodegen() {
34+
super();
35+
this.outputFolder = "generated-code" + File.separator + "typescript-fetch";
36+
37+
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
38+
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
39+
this.cliOptions.add(new CliOption(NPM_REPOSITORY,
40+
"Use this property to set an url your private npmRepo in the package.json"));
41+
this.cliOptions.add(new CliOption(SNAPSHOT,
42+
"When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm",
43+
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
44+
this.cliOptions.add(new CliOption(WITH_INTERFACES,
45+
"Setting this property to true will generate interfaces next to the default class implementations.",
46+
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
47+
}
48+
49+
@Override
50+
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
51+
if (schema instanceof MapSchema && hasSchemaProperties(schema)) {
52+
codegenModel.additionalPropertiesType = getTypeDeclaration((Schema) schema.getAdditionalProperties());
53+
addImport(codegenModel, codegenModel.additionalPropertiesType);
54+
} else if (schema instanceof MapSchema && hasTrueAdditionalProperties(schema)) {
55+
codegenModel.additionalPropertiesType = getTypeDeclaration(new ObjectSchema());
56+
}
57+
}
58+
59+
@Override
60+
public String getName() {
61+
return "typescript-fetch";
62+
}
63+
64+
@Override
65+
public String getHelp() {
66+
return "Generates a TypeScript client library using Fetch API (beta).";
67+
}
68+
69+
@Override
70+
public void processOpts() {
71+
super.processOpts();
72+
73+
if (StringUtils.isBlank(templateDir)) {
74+
embeddedTemplateDir = templateDir = getTemplateDir();
75+
}
76+
77+
supportingFiles.add(new SupportingFile("index.mustache", "", "index.ts"));
78+
supportingFiles.add(new SupportingFile("api.mustache", "", "api.ts"));
79+
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));
80+
supportingFiles.add(new SupportingFile("custom.d.mustache", "", "custom.d.ts"));
81+
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
82+
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
83+
84+
if (additionalProperties.containsKey(NPM_NAME)) {
85+
addNpmPackageGeneration();
86+
}
87+
}
88+
89+
@Override
90+
public String getDefaultTemplateDir() {
91+
return "typescript-fetch";
92+
}
93+
94+
@Override
95+
public String getTypeDeclaration(Schema propertySchema) {
96+
Schema inner;
97+
if (propertySchema instanceof ArraySchema) {
98+
ArraySchema arraySchema = (ArraySchema) propertySchema;
99+
inner = arraySchema.getItems();
100+
return this.getSchemaType(propertySchema) + "<" + this.getTypeDeclaration(inner) + ">";
101+
} else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) {
102+
inner = (Schema) propertySchema.getAdditionalProperties();
103+
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
104+
} else if (propertySchema instanceof MapSchema && hasTrueAdditionalProperties(propertySchema)) {
105+
inner = new ObjectSchema();
106+
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
107+
} else if (propertySchema instanceof FileSchema || propertySchema instanceof BinarySchema) {
108+
return "Blob";
109+
} else if (propertySchema instanceof ObjectSchema) {
110+
return "any";
111+
} else {
112+
return super.getTypeDeclaration(propertySchema);
113+
}
114+
}
115+
116+
@Override
117+
public String getSchemaType(Schema schema) {
118+
String swaggerType = super.getSchemaType(schema);
119+
if (isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
120+
return swaggerType;
121+
}
122+
applyLocalTypeMapping(swaggerType);
123+
return swaggerType;
124+
}
125+
126+
private String applyLocalTypeMapping(String type) {
127+
if (typeMapping.containsKey(type)) {
128+
type = typeMapping.get(type);
129+
}
130+
return type;
131+
}
132+
133+
private boolean isLanguagePrimitive(String type) {
134+
return languageSpecificPrimitives.contains(type);
135+
}
136+
137+
private boolean isLanguageGenericType(String type) {
138+
for (String genericType : languageGenericTypes) {
139+
if (type.startsWith(genericType + "<")) {
140+
return true;
141+
}
142+
}
143+
return false;
144+
}
145+
146+
private void addNpmPackageGeneration() {
147+
if (additionalProperties.containsKey(NPM_NAME)) {
148+
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
149+
}
150+
151+
if (additionalProperties.containsKey(NPM_VERSION)) {
152+
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString());
153+
}
154+
155+
if (additionalProperties.containsKey(SNAPSHOT)
156+
&& Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) {
157+
this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
158+
}
159+
additionalProperties.put(NPM_VERSION, npmVersion);
160+
161+
if (additionalProperties.containsKey(NPM_REPOSITORY)) {
162+
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
163+
}
164+
165+
// Files for building our lib
166+
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
167+
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
168+
supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json"));
169+
}
170+
171+
public String getNpmName() {
172+
return npmName;
173+
}
174+
175+
public void setNpmName(String npmName) {
176+
this.npmName = npmName;
177+
}
178+
179+
public String getNpmVersion() {
180+
return npmVersion;
181+
}
182+
183+
public void setNpmVersion(String npmVersion) {
184+
this.npmVersion = npmVersion;
185+
}
186+
187+
public String getNpmRepository() {
188+
return npmRepository;
189+
}
190+
191+
public void setNpmRepository(String npmRepository) {
192+
this.npmRepository = npmRepository;
193+
}
194+
195+
}

src/main/resources/META-INF/services/io.swagger.codegen.v3.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ io.swagger.codegen.v3.generators.swift.Swift3Codegen
3333
io.swagger.codegen.v3.generators.swift.Swift4Codegen
3434
io.swagger.codegen.v3.generators.swift.Swift5Codegen
3535
io.swagger.codegen.v3.generators.typescript.TypeScriptAngularClientCodegen
36+
io.swagger.codegen.v3.generators.typescript.TypeScriptFetchClientCodegen
3637
io.swagger.codegen.v3.generators.javascript.JavaScriptClientCodegen
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## {{npmName}}@{{npmVersion}}
2+
3+
This generator creates TypeScript/JavaScript client that utilizes [Fetch API](https://fetch.spec.whatwg.org/). The generated Node module can be used in the following environments:
4+
5+
Environment
6+
* Node.js
7+
* Webpack
8+
* Browserify
9+
10+
Language level
11+
* ES5 - you must have a Promises/A+ library installed
12+
* ES6
13+
14+
Module system
15+
* CommonJS
16+
* ES6 module system
17+
18+
It can be used in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html))
19+
20+
### Building
21+
22+
To build an compile the typescript sources to javascript use:
23+
```
24+
npm install
25+
npm run build
26+
```
27+
28+
### Publishing
29+
30+
First build the package then run ```npm publish```
31+
32+
### Consuming
33+
34+
navigate to the folder of your consuming project and run one of the following commands.
35+
36+
_published:_
37+
38+
```
39+
npm install {{npmName}}@{{npmVersion}} --save
40+
```
41+
42+
_unPublished (not recommended):_
43+
44+
```
45+
npm install PATH_TO_GENERATED_PACKAGE --save

0 commit comments

Comments
 (0)