Skip to content

Commit 0a037ae

Browse files
saigiridhar21wing328
authored andcommitted
[csharp] Making example code snippet compilable (#3019)
* fix(okhttp-gson): Updating example * fix(csharp): Updating example * fix(csharp): Updating pet project * fix(csharp): Updating example
1 parent 499c8ac commit 0a037ae

File tree

9 files changed

+234
-156
lines changed

9 files changed

+234
-156
lines changed

modules/openapi-generator/src/main/resources/csharp/README.mustache

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ Then, publish to a [local feed](https://docs.microsoft.com/en-us/nuget/hosting-p
102102
## Getting Started
103103

104104
```csharp
105-
using System;
106105
using System.Diagnostics;
107106
using {{packageName}}.{{apiPackage}};
108107
using {{packageName}}.Client;
@@ -112,9 +111,10 @@ namespace Example
112111
{
113112
public class {{operationId}}Example
114113
{
115-
public void main()
114+
public static void Main()
116115
{
117116
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
117+
Configuration.Default.BasePath = "{{{basePath}}}";
118118
{{#hasAuthMethods}}
119119
{{#authMethods}}
120120
{{#isBasic}}
@@ -135,7 +135,7 @@ namespace Example
135135
{{/authMethods}}
136136

137137
{{/hasAuthMethods}}
138-
var apiInstance = new {{classname}}();
138+
var apiInstance = new {{classname}}(Configuration.Default);
139139
{{#allParams}}
140140
{{#isPrimitiveType}}
141141
var {{paramName}} = {{{example}}}; // {{{dataType}}} | {{{description}}}{{^required}} (optional) {{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
@@ -153,9 +153,11 @@ namespace Example
153153
{{#returnType}}{{{.}}} result = {{/returnType}}apiInstance.{{{operationId}}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
154154
Debug.WriteLine(result);{{/returnType}}
155155
}
156-
catch (Exception e)
156+
catch (ApiException e)
157157
{
158158
Debug.Print("Exception when calling {{classname}}.{{operationId}}: " + e.Message );
159+
Debug.Print("Status Code: "+ e.ErrorCode);
160+
Debug.Print(e.StackTrace);
159161
}
160162
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
161163
}

modules/openapi-generator/src/main/resources/csharp/api_doc.mustache

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Method | HTTP request | Description
2323
### Example
2424

2525
```csharp
26-
using System;
2726
using System.Diagnostics;
2827
using {{packageName}}.{{apiPackage}};
2928
using {{packageName}}.Client;
@@ -33,8 +32,9 @@ namespace Example
3332
{
3433
public class {{operationId}}Example
3534
{
36-
public void main()
35+
public static void Main()
3736
{
37+
Configuration.Default.BasePath = "{{{basePath}}}";
3838
{{#hasAuthMethods}}
3939
{{#authMethods}}
4040
{{#isBasic}}
@@ -55,7 +55,7 @@ namespace Example
5555
{{/authMethods}}
5656

5757
{{/hasAuthMethods}}
58-
var apiInstance = new {{classname}}();
58+
var apiInstance = new {{classname}}(Configuration.Default);
5959
{{#allParams}}
6060
{{#isPrimitiveType}}
6161
var {{paramName}} = {{{example}}}; // {{{dataType}}} | {{{description}}}{{^required}} (optional) {{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
@@ -73,9 +73,11 @@ namespace Example
7373
{{#returnType}}{{returnType}} result = {{/returnType}}apiInstance.{{{operationId}}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
7474
Debug.WriteLine(result);{{/returnType}}
7575
}
76-
catch (Exception e)
76+
catch (ApiException e)
7777
{
7878
Debug.Print("Exception when calling {{classname}}.{{operationId}}: " + e.Message );
79+
Debug.Print("Status Code: "+ e.ErrorCode);
80+
Debug.Print(e.StackTrace);
7981
}
8082
}
8183
}

samples/client/petstore/csharp/OpenAPIClient/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ Then, publish to a [local feed](https://docs.microsoft.com/en-us/nuget/hosting-p
6464
## Getting Started
6565

6666
```csharp
67-
using System;
6867
using System.Diagnostics;
6968
using Org.OpenAPITools.Api;
7069
using Org.OpenAPITools.Client;
@@ -74,10 +73,11 @@ namespace Example
7473
{
7574
public class Example
7675
{
77-
public void main()
76+
public static void Main()
7877
{
7978

80-
var apiInstance = new AnotherFakeApi();
79+
Configuration.Default.BasePath = "http://petstore.swagger.io:80/v2";
80+
var apiInstance = new AnotherFakeApi(Configuration.Default);
8181
var body = new ModelClient(); // ModelClient | client model
8282
8383
try
@@ -86,9 +86,11 @@ namespace Example
8686
ModelClient result = apiInstance.Call123TestSpecialTags(body);
8787
Debug.WriteLine(result);
8888
}
89-
catch (Exception e)
89+
catch (ApiException e)
9090
{
9191
Debug.Print("Exception when calling AnotherFakeApi.Call123TestSpecialTags: " + e.Message );
92+
Debug.Print("Status Code: "+ e.ErrorCode);
93+
Debug.Print(e.StackTrace);
9294
}
9395

9496
}

samples/client/petstore/csharp/OpenAPIClient/docs/AnotherFakeApi.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ To test special tags and operation ID starting with number
1919
### Example
2020

2121
```csharp
22-
using System;
2322
using System.Diagnostics;
2423
using Org.OpenAPITools.Api;
2524
using Org.OpenAPITools.Client;
@@ -29,9 +28,10 @@ namespace Example
2928
{
3029
public class Call123TestSpecialTagsExample
3130
{
32-
public void main()
31+
public static void Main()
3332
{
34-
var apiInstance = new AnotherFakeApi();
33+
Configuration.Default.BasePath = "http://petstore.swagger.io:80/v2";
34+
var apiInstance = new AnotherFakeApi(Configuration.Default);
3535
var body = new ModelClient(); // ModelClient | client model
3636
3737
try
@@ -40,9 +40,11 @@ namespace Example
4040
ModelClient result = apiInstance.Call123TestSpecialTags(body);
4141
Debug.WriteLine(result);
4242
}
43-
catch (Exception e)
43+
catch (ApiException e)
4444
{
4545
Debug.Print("Exception when calling AnotherFakeApi.Call123TestSpecialTags: " + e.Message );
46+
Debug.Print("Status Code: "+ e.ErrorCode);
47+
Debug.Print(e.StackTrace);
4648
}
4749
}
4850
}

0 commit comments

Comments
 (0)