Merge branch 'csharp/INotifyPropertyChanged' of https://github.com/jimschubert/swagger-codegen into jimschubert-csharp/INotifyPropertyChanged

This commit is contained in:
wing328 2016-08-12 15:04:04 +08:00
commit f746a93da4
10 changed files with 57 additions and 15 deletions

View File

@ -26,6 +26,6 @@ fi
# if you've executed sbt assembly previously it will use that instead. # if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l csharp -o samples/client/petstore/csharp/SwaggerClient" ags="generate $@ -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l csharp -o samples/client/petstore/csharp/SwaggerClient"
java $JAVA_OPTS -jar $executable $ags java $JAVA_OPTS -jar $executable $ags

View File

@ -127,4 +127,7 @@ public class CodegenConstants {
public static final String GENERATE_MODEL_TESTS = "generateModelTests"; public static final String GENERATE_MODEL_TESTS = "generateModelTests";
public static final String GENERATE_MODEL_TESTS_DESC = "Specifies that model tests are to be generated."; public static final String GENERATE_MODEL_TESTS_DESC = "Specifies that model tests are to be generated.";
public static final String GENERATE_PROPERTY_CHANGED = "generatePropertyChanged";
public static final String GENERATE_PROPERTY_CHANGED_DESC = "Specifies that models support raising property changed events.";
} }

View File

@ -1,32 +1,20 @@
package io.swagger.codegen.languages; package io.swagger.codegen.languages;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenType; import io.swagger.codegen.CodegenType;
import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile; import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.CodegenProperty; import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenOperation; import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;
import io.swagger.models.properties.*;
import io.swagger.codegen.CliOption; import io.swagger.codegen.CliOption;
import io.swagger.models.Model; import io.swagger.models.Model;
import java.io.File; import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -48,6 +36,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
protected String targetFrameworkNuget = "net45"; protected String targetFrameworkNuget = "net45";
protected boolean supportsAsync = Boolean.TRUE; protected boolean supportsAsync = Boolean.TRUE;
protected boolean supportsUWP = Boolean.FALSE; protected boolean supportsUWP = Boolean.FALSE;
protected boolean generatePropertyChanged = Boolean.FALSE;
protected final Map<String, String> frameworks; protected final Map<String, String> frameworks;
@ -127,6 +116,10 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
addSwitch(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES, addSwitch(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES,
CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES_DESC, CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES_DESC,
this.optionalEmitDefaultValue); this.optionalEmitDefaultValue);
addSwitch(CodegenConstants.GENERATE_PROPERTY_CHANGED,
CodegenConstants.PACKAGE_DESCRIPTION_DESC,
this.generatePropertyChanged);
} }
@Override @Override
@ -173,6 +166,14 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
additionalProperties.put("supportsAsync", this.supportsAsync); additionalProperties.put("supportsAsync", this.supportsAsync);
} }
if(additionalProperties.containsKey(CodegenConstants.GENERATE_PROPERTY_CHANGED)) {
if(NET35.equals(targetFramework)) {
LOGGER.warn(CodegenConstants.GENERATE_PROPERTY_CHANGED + " is only supported by generated code for .NET 4+.");
} else {
setGeneratePropertyChanged(Boolean.valueOf(additionalProperties.get(CodegenConstants.GENERATE_PROPERTY_CHANGED).toString()));
}
}
additionalProperties.put("targetFrameworkNuget", this.targetFrameworkNuget); additionalProperties.put("targetFrameworkNuget", this.targetFrameworkNuget);
if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_FILE)) { if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_FILE)) {
@ -239,6 +240,10 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
supportingFiles.add(new SupportingFile("packages_test.config.mustache", testPackageFolder + File.separator, "packages.config")); supportingFiles.add(new SupportingFile("packages_test.config.mustache", testPackageFolder + File.separator, "packages.config"));
} }
if(Boolean.TRUE.equals(generatePropertyChanged)) {
supportingFiles.add(new SupportingFile("FodyWeavers.xml", "", "FodyWeavers.xml"));
}
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore")); supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
@ -446,6 +451,10 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
this.supportsUWP = supportsUWP; this.supportsUWP = supportsUWP;
} }
public void setGeneratePropertyChanged(final Boolean generatePropertyChanged){
this.generatePropertyChanged = generatePropertyChanged;
}
@Override @Override
public String toModelDocFilename(String name) { public String toModelDocFilename(String name) {
return toModelFilename(name); return toModelFilename(name);

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<PropertyChanged/>
</Weavers>

View File

@ -82,13 +82,19 @@ limitations under the License.
<HintPath Condition="Exists('..\..\packages')">..\..\packages\RestSharp.105.1.0\lib\{{targetFrameworkNuget}}\RestSharp.dll</HintPath> <HintPath Condition="Exists('..\..\packages')">..\..\packages\RestSharp.105.1.0\lib\{{targetFrameworkNuget}}\RestSharp.dll</HintPath>
<HintPath Condition="Exists('{{binRelativePath}}')">{{binRelativePath}}\RestSharp.105.1.0\lib\{{targetFrameworkNuget}}\RestSharp.dll</HintPath> <HintPath Condition="Exists('{{binRelativePath}}')">{{binRelativePath}}\RestSharp.105.1.0\lib\{{targetFrameworkNuget}}\RestSharp.dll</HintPath>
</Reference> </Reference>
{{#generatePropertyChanged}}<Reference Include="PropertyChanged">
<HintPath>..\..\packages\PropertyChanged.Fody.1.51.3\Lib\portable-net4+sl4+wp8+win8+wpa81+MonoAndroid16+MonoTouch40\PropertyChanged.dll</HintPath>
</Reference>{{/generatePropertyChanged}}
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="**\*.cs"/> <Compile Include="**\*.cs"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
{{#generatePropertyChanged}}<None Include="FodyWeavers.xml" />{{/generatePropertyChanged}}
</ItemGroup> </ItemGroup>
<Import Project="$(MsBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MsBuildToolsPath)\Microsoft.CSharp.targets" />{{#generatePropertyChanged}}
<Import Project="..\..\packages\Fody.1.29.2\build\portable-net+sl+win+wpa+wp\Fody.targets" Condition="Exists('..\..\packages\Fody.1.29.2\build\portable-net+sl+win+wpa+wp\Fody.targets')" />
{{/generatePropertyChanged}}
</Project> </Project>

View File

@ -9,6 +9,8 @@ using System.Collections.ObjectModel;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
{{#generatePropertyChanged}}using PropertyChanged;
using System.ComponentModel;{{/generatePropertyChanged}}
{{#models}} {{#models}}
{{#model}} {{#model}}

View File

@ -1,7 +1,8 @@
/// <summary> /// <summary>
/// {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}} /// {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}}
/// </summary> /// </summary>
[DataContract] [DataContract]{{#generatePropertyChanged}}
[ImplementPropertyChanged]{{/generatePropertyChanged}}
public partial class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}> public partial class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}>
{ {
{{#vars}} {{#vars}}
@ -169,4 +170,16 @@ this.{{name}} = {{name}};
return hash; return hash;
} }
} }
{{#generatePropertyChanged}}
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
// NOTE: property changed is handled via "code weaving" using Fody.
// Properties with setters are modified at compile time to notify of changes.
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}{{/generatePropertyChanged}}
} }

View File

@ -2,4 +2,6 @@
<packages> <packages>
<package id="RestSharp" version="105.1.0" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" /> <package id="RestSharp" version="105.1.0" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" /> <package id="Newtonsoft.Json" version="8.0.3" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
{{#generatePropertyChanged}}<package id="Fody" version="1.29.2" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
<package id="PropertyChanged.Fody" version="1.51.3" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />{{/generatePropertyChanged}}
</packages> </packages>

View File

@ -48,6 +48,8 @@ public class CSharpClientOptionsTest extends AbstractOptionsTest {
times = 1; times = 1;
clientCodegen.setOptionalEmitDefaultValue(true); clientCodegen.setOptionalEmitDefaultValue(true);
times = 1; times = 1;
clientCodegen.setGeneratePropertyChanged(true);
times = 1;
}}; }};
} }
} }

View File

@ -33,6 +33,7 @@ public class CSharpClientOptionsProvider implements OptionsProvider {
.put(CodegenConstants.OPTIONAL_PROJECT_GUID, PACKAGE_GUID_VALUE) .put(CodegenConstants.OPTIONAL_PROJECT_GUID, PACKAGE_GUID_VALUE)
.put(CodegenConstants.DOTNET_FRAMEWORK, "4.x") .put(CodegenConstants.DOTNET_FRAMEWORK, "4.x")
.put(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES, "true") .put(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES, "true")
.put(CodegenConstants.GENERATE_PROPERTY_CHANGED, "true")
.build(); .build();
} }