7
7
8
8
import io .swagger .codegen .*;
9
9
import io .swagger .models .*;
10
+ import io .swagger .models .auth .SecuritySchemeDefinition ;
10
11
import io .swagger .util .Json ;
11
12
13
+ import org .apache .commons .lang3 .StringUtils ;
12
14
import org .slf4j .Logger ;
13
15
import org .slf4j .LoggerFactory ;
14
16
21
23
public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
22
24
23
25
private String packageGuid = "{" + randomUUID ().toString ().toUpperCase () + "}" ;
26
+ private final String DEFAULT_ASP_NET_CORE_VERSION = "2.2" ;
27
+ private String aspNetCoreVersion ;
24
28
25
29
@ SuppressWarnings ("hiding" )
26
30
protected Logger LOGGER = LoggerFactory .getLogger (AspNetCoreServerCodegen .class );
@@ -32,7 +36,7 @@ public AspNetCoreServerCodegen() {
32
36
outputFolder = "generated-code" + File .separator + this .getName ();
33
37
34
38
modelTemplateFiles .put ("model.mustache" , ".cs" );
35
- apiTemplateFiles . put ( "controller.mustache" , ".cs" ) ;
39
+ aspNetCoreVersion = DEFAULT_ASP_NET_CORE_VERSION ;
36
40
37
41
// contextually reserved words
38
42
// NOTE: C# uses camel cased reserved words, while models are title cased. We don't want lowercase comparisons.
@@ -59,6 +63,18 @@ public AspNetCoreServerCodegen() {
59
63
CodegenConstants .SOURCE_FOLDER_DESC ,
60
64
sourceFolder );
61
65
66
+ addOption (CodegenConstants .ASP_NET_CORE_VERSION ,
67
+ "aspnetcore version to use, current options are: 2.0, 2.1 and 2.2 (default)" ,
68
+ this .aspNetCoreVersion );
69
+
70
+ addOption (CodegenConstants .INTERFACE_ONLY ,
71
+ "creates interfaces controller only" ,
72
+ null );
73
+
74
+ addOption (CodegenConstants .INTERFACE_CONTROLLER ,
75
+ "creates interfaces and default implementation for controllers" ,
76
+ null );
77
+
62
78
addOption (CodegenConstants .PRESERVE_COMMENT_NEWLINES ,
63
79
"Preserve newlines in comments" ,
64
80
String .valueOf (this .preserveNewLines ));
@@ -100,18 +116,44 @@ public String getHelp() {
100
116
public void processOpts () {
101
117
super .processOpts ();
102
118
119
+ if (additionalProperties .containsKey ("aspnetCoreVersion" )) {
120
+ setAspNetCoreVersion (String .valueOf (additionalProperties .get ("aspnetCoreVersion" )));
121
+ }
122
+
103
123
if (additionalProperties .containsKey (CodegenConstants .OPTIONAL_PROJECT_GUID )) {
104
124
setPackageGuid ((String ) additionalProperties .get (CodegenConstants .OPTIONAL_PROJECT_GUID ));
105
125
}
126
+
127
+ String packageFolder = sourceFolder + File .separator + packageName ;
128
+
129
+ boolean interfaceOnly = Boolean .valueOf (String .valueOf (additionalProperties .get (CodegenConstants .INTERFACE_ONLY )));
130
+ boolean interfaceController = Boolean .valueOf (String .valueOf (additionalProperties .get (CodegenConstants .INTERFACE_CONTROLLER )));
131
+
132
+ if (this .aspNetCoreVersion .equals ("2.0" )) {
133
+ apiTemplateFiles .put ("controller.mustache" , ".cs" );
134
+ addInterfaceControllerTemplate (interfaceOnly , interfaceController );
135
+
136
+ supportingFiles .add (new SupportingFile ("Program.mustache" , packageFolder , "Program.cs" ));
137
+ supportingFiles .add (new SupportingFile ("Project.csproj.mustache" , packageFolder , this .packageName + ".csproj" ));
138
+ supportingFiles .add (new SupportingFile ("Dockerfile.mustache" , packageFolder , "Dockerfile" ));
139
+ } else {
140
+ apiTemplateFiles .put ("2.1/controller.mustache" , ".cs" );
141
+ addInterfaceControllerTemplate (interfaceOnly , interfaceController );
142
+
143
+ supportingFiles .add (new SupportingFile ("2.1/Program.mustache" , packageFolder , "Program.cs" ));
144
+ supportingFiles .add (new SupportingFile ("2.1/Project.csproj.mustache" , packageFolder , this .packageName + ".csproj" ));
145
+ supportingFiles .add (new SupportingFile ("2.1/Dockerfile.mustache" , packageFolder , "Dockerfile" ));
146
+ }
147
+
148
+ additionalProperties .put ("aspNetCoreVersion" , aspNetCoreVersion );
149
+
106
150
additionalProperties .put ("packageGuid" , packageGuid );
107
151
108
152
additionalProperties .put ("dockerTag" , this .packageName .toLowerCase ());
109
153
110
154
apiPackage = packageName + ".Controllers" ;
111
155
modelPackage = packageName + ".Models" ;
112
156
113
- String packageFolder = sourceFolder + File .separator + packageName ;
114
-
115
157
supportingFiles .add (new SupportingFile ("NuGet.Config" , "" , "NuGet.Config" ));
116
158
supportingFiles .add (new SupportingFile ("build.sh.mustache" , "" , "build.sh" ));
117
159
supportingFiles .add (new SupportingFile ("build.bat.mustache" , "" , "build.bat" ));
@@ -122,12 +164,9 @@ public void processOpts() {
122
164
supportingFiles .add (new SupportingFile ("appsettings.json" , packageFolder , "appsettings.json" ));
123
165
124
166
supportingFiles .add (new SupportingFile ("Startup.mustache" , packageFolder , "Startup.cs" ));
125
- supportingFiles .add (new SupportingFile ("Program.mustache" , packageFolder , "Program.cs" ));
126
167
supportingFiles .add (new SupportingFile ("validateModel.mustache" , packageFolder + File .separator + "Attributes" , "ValidateModelStateAttribute.cs" ));
127
168
supportingFiles .add (new SupportingFile ("web.config" , packageFolder , "web.config" ));
128
169
129
- supportingFiles .add (new SupportingFile ("Project.csproj.mustache" , packageFolder , this .packageName + ".csproj" ));
130
-
131
170
supportingFiles .add (new SupportingFile ("Properties" + File .separator + "launchSettings.json" , packageFolder + File .separator + "Properties" , "launchSettings.json" ));
132
171
133
172
supportingFiles .add (new SupportingFile ("Filters" + File .separator + "BasePathFilter.mustache" , packageFolder + File .separator + "Filters" , "BasePathFilter.cs" ));
@@ -162,6 +201,16 @@ public String apiFileFolder() {
162
201
return outputFolder + File .separator + sourceFolder + File .separator + packageName + File .separator + "Controllers" ;
163
202
}
164
203
204
+ @ Override
205
+ public String apiFilename (String templateName , String tag ) {
206
+ boolean isInterface = templateName .equalsIgnoreCase ("icontroller.mustache" );
207
+ String suffix = apiTemplateFiles ().get (templateName );
208
+ if (isInterface ) {
209
+ return apiFileFolder () + "/I" + toApiFilename (tag ) + suffix ;
210
+ }
211
+ return apiFileFolder () + '/' + toApiFilename (tag ) + suffix ;
212
+ }
213
+
165
214
@ Override
166
215
public String modelFileFolder () {
167
216
return outputFolder + File .separator + sourceFolder + File .separator + packageName + File .separator + "Models" ;
@@ -204,4 +253,57 @@ public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
204
253
// To avoid unexpected behaviors when options are passed programmatically such as { "useCollection": "" }
205
254
return super .processCompiler (compiler ).emptyStringIsFalse (true );
206
255
}
256
+
257
+ @ Override
258
+ public List <CodegenSecurity > fromSecurity (Map <String , SecuritySchemeDefinition > schemes ) {
259
+ final List <CodegenSecurity > securities = super .fromSecurity (schemes );
260
+ if (securities == null || securities .isEmpty ()) {
261
+ return securities ;
262
+ }
263
+ boolean hasBasic = false ;
264
+ boolean hasBearer = false ;
265
+ boolean hasApiKey = false ;
266
+ for (int index = 0 ; index < securities .size (); index ++) {
267
+ final CodegenSecurity codegenSecurity = securities .get (index );
268
+ if (codegenSecurity .isBasic ) {
269
+ hasBasic = true ;
270
+ }
271
+ if (codegenSecurity .isApiKey ) {
272
+ hasApiKey = true ;
273
+ }
274
+ }
275
+ final String packageFolder = sourceFolder + File .separator + packageName ;
276
+ if (hasBasic ) {
277
+ supportingFiles .add (new SupportingFile ("Security/BasicAuthenticationHandler.mustache" , packageFolder + File .separator + "Security" , "BasicAuthenticationHandler.cs" ));
278
+ }
279
+ if (hasBearer ) {
280
+ supportingFiles .add (new SupportingFile ("Security/BearerAuthenticationHandler.mustache" , packageFolder + File .separator + "Security" , "BearerAuthenticationHandler.cs" ));
281
+ }
282
+ if (hasApiKey ) {
283
+ supportingFiles .add (new SupportingFile ("Security/ApiKeyAuthenticationHandler.mustache" , packageFolder + File .separator + "Security" , "ApiKeyAuthenticationHandler.cs" ));
284
+ }
285
+ return securities ;
286
+ }
287
+
288
+ private void addInterfaceControllerTemplate (boolean interfaceOnly , boolean interfaceController ) {
289
+ if (interfaceController ) {
290
+ apiTemplateFiles .put ("icontroller.mustache" , ".cs" );
291
+ additionalProperties .put ("interfaceController" , Boolean .TRUE );
292
+ }
293
+ if (interfaceOnly ) {
294
+ apiTemplateFiles .clear ();
295
+ apiTemplateFiles .put ("icontroller.mustache" , ".cs" );
296
+ }
297
+ }
298
+
299
+ private void setAspNetCoreVersion (String optionValue ) {
300
+ if (StringUtils .isBlank (optionValue )) {
301
+ return ;
302
+ }
303
+ this .aspNetCoreVersion = optionValue ;
304
+ if (!this .aspNetCoreVersion .equals ("2.0" ) && !this .aspNetCoreVersion .equals ("2.1" ) && !this .aspNetCoreVersion .equals ("2.2" )) {
305
+ LOGGER .error ("version '" + this .aspNetCoreVersion + "' is not supported, switching to default version: '" + DEFAULT_ASP_NET_CORE_VERSION + "'" );
306
+ this .aspNetCoreVersion = DEFAULT_ASP_NET_CORE_VERSION ;
307
+ }
308
+ }
207
309
}
0 commit comments