Skip to content

[C#][aspnet] use default in model constructors, supports enums#4573

Merged
wing328 merged 3 commits intoswagger-api:masterfrom
jimschubert:aspnetcore/4145
Jan 18, 2017
Merged

[C#][aspnet] use default in model constructors, supports enums#4573
wing328 merged 3 commits intoswagger-api:masterfrom
jimschubert:aspnetcore/4145

Conversation

@jimschubert
Copy link
Copy Markdown
Contributor

@jimschubert jimschubert commented Jan 16, 2017

PR checklist

  • Read the contribution guildelines.
  • Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

This addresses a couple of issues. As originally reported in #3608 and added to the C# client generator in #4145, this updates model constructors to use default() rather than attempting to initialize parameters to nulls. I didn't modify the NancyFX generator here, because it relies on object initialization and doesn't perform the same types of constructor validations or DataAnnotations/JSON validation.

The petstore.yaml has inner enums, so to get the sample code working against this yaml I've also added support for enums and inner enums, and regenerated the sample.

Please note if you're having issues with newer versions of dotnet CLI, there's a known issue (https://github.com/dotnet/corefx/issues/9171) with OS X/macOS support which requires linking openssl manually:

$ ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
$ ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/

To test:

cd samples/server/petstore/aspnetcore
sh build.sh

NOTE:

If you receive the following error on build, you'll need to perform the openssl symlinks mentioned above:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'Crypto' threw an exception. ---> System.TypeInitializationException: The type initializer for 'CryptoInitializer' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'System.Security.Cryptography.Native': The specified module could not be found.
 (Exception from HRESULT: 0x8007007E)
   at Interop.CryptoInitializer.EnsureOpenSslInitialized()
   at Interop.CryptoInitializer..cctor()
   --- End of inner exception stack trace ---
   at Interop.Crypto..cctor()
   --- End of inner exception stack trace ---
   at Interop.Crypto.GetRandomBytes(Byte* buf, Int32 num)
   at System.IO.Path.GetCryptoRandomBytes(Byte* bytes, Int32 byteCount)
   at System.IO.Path.GetRandomFileName()
   at Microsoft.DotNet.InternalAbstractions.TemporaryDirectory..ctor()
   at Microsoft.Extensions.EnvironmentAbstractions.DirectoryWrapper.CreateTemporaryDirectory()
   at Microsoft.DotNet.Configurer.NuGetPackagesArchiver..ctor()
   at Microsoft.DotNet.Cli.Program.ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel)
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
   at Microsoft.DotNet.Cli.Program.Main(String[] args)
build.sh: line 8: 45627 Abort trap: 6           dotnet restore src/IO.Swagger/

See original issue swagger-api#3608

This adds same model constructor logic to aspnetcore as what was added
to csharp generator by PR swagger-api#4145.

This doesn't include NancyFX because model construction relies more on
object initialization in that generator.
This follows up to swagger-api#4145, and modifies model constructors to use
default(x) instead of initializing to nulls. default(x) works in all
cases using intuitive default values it is intended to support.

Example:

  csharp> public enum Color { RED = -1, BLUE = 0, GREEN }
  csharp> var color = default(Color)
  csharp> color
  BLUE

In the above example, The default of BLUE=0 is expected. For nullable
enums, this would be null as a default.

The aspnetcore generated code is also updated to support enums and
nested enums to account for changed to the petstore.yaml used to
generate the sample.
@wing328 wing328 added this to the v2.2.2 milestone Jan 17, 2017
@wing328 wing328 merged commit da6b57f into swagger-api:master Jan 18, 2017
@wing328
Copy link
Copy Markdown
Contributor

wing328 commented Jan 18, 2017

@jimschubert thanks for the PR and the solution to address the SSL issue.

@jimschubert jimschubert deleted the aspnetcore/4145 branch January 18, 2017 09:32
@wing328 wing328 changed the title [aspnetcore] use default in model constructors, supports enums [C#][aspnet] use default in model constructors, supports enums Feb 20, 2017
davidgri pushed a commit to davidgri/swagger-codegen that referenced this pull request May 11, 2017
…er-api#4573)

* [aspnetcore] Use default rather than null in ctor

See original issue swagger-api#3608

This adds same model constructor logic to aspnetcore as what was added
to csharp generator by PR swagger-api#4145.

This doesn't include NancyFX because model construction relies more on
object initialization in that generator.

* [aspnetcore] ctor defaults + enum support

This follows up to swagger-api#4145, and modifies model constructors to use
default(x) instead of initializing to nulls. default(x) works in all
cases using intuitive default values it is intended to support.

Example:

  csharp> public enum Color { RED = -1, BLUE = 0, GREEN }
  csharp> var color = default(Color)
  csharp> color
  BLUE

In the above example, The default of BLUE=0 is expected. For nullable
enums, this would be null as a default.

The aspnetcore generated code is also updated to support enums and
nested enums to account for changed to the petstore.yaml used to
generate the sample.

* [aspnetcore] Regenerate sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants