Skip to content

Commit 02e87fc

Browse files
jimschubertviclovsky
authored andcommitted
[csharp] Fix ToJson to work with composition and polymorphism (swagger-api#7399)
* [csharp] Support composition on toJson Previous implementation assumed specification only supports polymorphic associations (via discrimator), although the code didn't seem to be setup correctly for that in the first place. That is, the parent object must define the discriminator (see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#models-with-polymorphism-support), so NOT HAS parent AND HAS discriminator doesn't make sense. From a C# perspective, base classes should have the method marked virtual and derived classes should override the method. This supports both composition and polymorphic definitions. * [csharp] Regenerate integration test files * [csharp] Regenerate samples * [csharp] Regenerate security sample
1 parent ff418fb commit 02e87fc

File tree

166 files changed

+177
-172
lines changed

Some content is hidden

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

166 files changed

+177
-172
lines changed

modules/swagger-codegen/src/main/resources/csharp/modelGeneric.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ this.{{name}} = {{name}};
133133
/// Returns the JSON string presentation of the object
134134
/// </summary>
135135
/// <returns>JSON string presentation of the object</returns>
136-
public {{#parent}}{{^isArrayModel}}override {{/isArrayModel}}{{/parent}}{{^parent}}{{#discriminator}}virtual {{/discriminator}}{{/parent}}string ToJson()
136+
public {{#parent}}{{^isArrayModel}}override {{/isArrayModel}}{{/parent}}{{^parent}}virtual {{/parent}}string ToJson()
137137
{
138138
return JsonConvert.SerializeObject(this, Formatting.Indented);
139139
}

modules/swagger-codegen/src/test/resources/integrationtests/csharp/general/enum-support-expected/src/IO.Swagger/Model/MyClassWithInvalidRequiredEnumUsageOnRef.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public override string ToString()
7171
/// Returns the JSON string presentation of the object
7272
/// </summary>
7373
/// <returns>JSON string presentation of the object</returns>
74-
public string ToJson()
74+
public virtual string ToJson()
7575
{
7676
return JsonConvert.SerializeObject(this, Formatting.Indented);
7777
}

modules/swagger-codegen/src/test/resources/integrationtests/csharp/general/enum-support-expected/src/IO.Swagger/Model/MyClassWithOptionalEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public override string ToString()
8080
/// Returns the JSON string presentation of the object
8181
/// </summary>
8282
/// <returns>JSON string presentation of the object</returns>
83-
public string ToJson()
83+
public virtual string ToJson()
8484
{
8585
return JsonConvert.SerializeObject(this, Formatting.Indented);
8686
}

modules/swagger-codegen/src/test/resources/integrationtests/csharp/general/enum-support-expected/src/IO.Swagger/Model/MyClassWithOptionalInlineEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public override string ToString()
130130
/// Returns the JSON string presentation of the object
131131
/// </summary>
132132
/// <returns>JSON string presentation of the object</returns>
133-
public string ToJson()
133+
public virtual string ToJson()
134134
{
135135
return JsonConvert.SerializeObject(this, Formatting.Indented);
136136
}

modules/swagger-codegen/src/test/resources/integrationtests/csharp/general/enum-support-expected/src/IO.Swagger/Model/MyClassWithRequiredInlineEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public override string ToString()
143143
/// Returns the JSON string presentation of the object
144144
/// </summary>
145145
/// <returns>JSON string presentation of the object</returns>
146-
public string ToJson()
146+
public virtual string ToJson()
147147
{
148148
return JsonConvert.SerializeObject(this, Formatting.Indented);
149149
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3.0-SNAPSHOT
1+
2.3.1-SNAPSHOT

samples/client/petstore-security-test/csharp/SwaggerClient/README.md

Lines changed: 2 additions & 0 deletions

samples/client/petstore-security-test/csharp/SwaggerClient/build.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if not exist ".\nuget.exe" powershell -Command "(new-object System.Net.WebClient
1111
if not exist ".\bin" mkdir bin
1212

1313
copy packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll bin\Newtonsoft.Json.dll
14+
copy packages\JsonSubTypes.1.2.0\lib\net45\JsonSubTypes.dll bin\JsonSubTypes.dll
1415
copy packages\RestSharp.105.1.0\lib\net45\RestSharp.dll bin\RestSharp.dll
15-
%CSCPATH%\csc /reference:bin\Newtonsoft.Json.dll;bin\RestSharp.dll;System.ComponentModel.DataAnnotations.dll /target:library /out:bin\IO.Swagger.dll /recurse:src\IO.Swagger\*.cs /doc:bin\IO.Swagger.xml
16+
%CSCPATH%\csc /reference:bin\Newtonsoft.Json.dll;bin\JsonSubTypes.dll;bin\RestSharp.dll;System.ComponentModel.DataAnnotations.dll /target:library /out:bin\IO.Swagger.dll /recurse:src\IO.Swagger\*.cs /doc:bin\IO.Swagger.xml
1617

samples/client/petstore-security-test/csharp/SwaggerClient/build.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ esac
3333

3434
echo "[INFO] Target framework: ${frameworkVersion}"
3535

36-
if [ ! type nuget &>/dev/null ]; then
36+
if ! type nuget &>/dev/null; then
3737
echo "[INFO] Download nuget and packages"
3838
wget -nc https://dist.nuget.org/win-x86-commandline/latest/nuget.exe;
39-
nuget_cmd="mono nuget"
39+
nuget_cmd="mono nuget.exe"
4040
fi
4141

4242
mozroots --import --sync
@@ -46,9 +46,10 @@ echo "[INFO] Copy DLLs to the 'bin' folder"
4646
mkdir -p bin;
4747
cp packages/Newtonsoft.Json.10.0.3/lib/net45/Newtonsoft.Json.dll bin/Newtonsoft.Json.dll;
4848
cp packages/RestSharp.105.1.0/lib/net45/RestSharp.dll bin/RestSharp.dll;
49+
cp packages/JsonSubTypes.1.2.0/lib/net45/JsonSubTypes.dll bin/JsonSubTypes.dll
4950

5051
echo "[INFO] Run 'mcs' to build bin/IO.Swagger.dll"
51-
mcs -langversion:${langversion} -sdk:${sdk} -r:bin/Newtonsoft.Json.dll,\
52+
mcs -langversion:${langversion} -sdk:${sdk} -r:bin/Newtonsoft.Json.dll,bin/JsonSubTypes.dll,\
5253
bin/RestSharp.dll,\
5354
System.ComponentModel.DataAnnotations.dll,\
5455
System.Runtime.Serialization.dll \

samples/client/petstore-security-test/csharp/SwaggerClient/src/IO.Swagger.Test/IO.Swagger.Test.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ Contact: [email protected] *_/ ' \" =end - - \\r\\n \\n \\r
5353
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
5454
</Reference>
5555
<Reference Include="JsonSubTypes">
56-
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
57-
<HintPath Condition="Exists('..\packages')">..\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
58-
<HintPath Condition="Exists('..\..\packages')">..\..\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
59-
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
56+
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\JsonSubTypes.1.2.0\lib\net45\JsonSubTypes.dll</HintPath>
57+
<HintPath Condition="Exists('..\packages')">..\packages\JsonSubTypes.1.2.0\lib\net45\JsonSubTypes.dll</HintPath>
58+
<HintPath Condition="Exists('..\..\packages')">..\..\packages\JsonSubTypes.1.2.0\lib\net45\JsonSubTypes.dll</HintPath>
59+
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\JsonSubTypes.1.2.0\lib\net45\JsonSubTypes.dll</HintPath>
6060
</Reference>
6161
<Reference Include="RestSharp">
6262
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\RestSharp.105.1.0\lib\net45\RestSharp.dll</HintPath>

0 commit comments

Comments
 (0)