Skip to content

Commit 72b9ab6

Browse files
committed
[csharp] Clean up general enum support integration test, validate different enum usage cases.
1 parent 8be610b commit 72b9ab6

File tree

7 files changed

+430
-10
lines changed

7 files changed

+430
-10
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public Generator opts(ClientOptInput opts) {
6363
return this;
6464
}
6565

66+
/**
67+
* Programmatically disable the output of .swagger-codegen/VERSION, .swagger-codegen-ignore,
68+
* or other metadata files used by Swagger Codegen.
69+
* @param generateSwaggerMetadata true: enable outputs, false: disable outputs
70+
*/
71+
@SuppressWarnings("WeakerAccess")
6672
public void setGenerateSwaggerMetadata(Boolean generateSwaggerMetadata) {
6773
this.generateSwaggerMetadata = generateSwaggerMetadata;
6874
}
@@ -73,6 +79,7 @@ public void setGenerateSwaggerMetadata(Boolean generateSwaggerMetadata) {
7379
* @param key The system property key
7480
* @param value The system property value
7581
*/
82+
@SuppressWarnings("WeakerAccess")
7683
public void setGeneratorPropertyDefault(final String key, final String value) {
7784
this.generatorPropertyDefaults.put(key, value);
7885
}
@@ -116,6 +123,7 @@ private String getHost() {
116123

117124
private void configureGeneratorProperties() {
118125
// allows generating only models by specifying a CSV of models to generate, or empty for all
126+
// NOTE: Boolean.TRUE is required below rather than `true` because of JVM boxing constraints and type inference.
119127
generateApis = System.getProperty(CodegenConstants.APIS) != null ? Boolean.TRUE : getGeneratorPropertyDefaultSwitch(CodegenConstants.APIS, null);
120128
generateModels = System.getProperty(CodegenConstants.MODELS) != null ? Boolean.TRUE : getGeneratorPropertyDefaultSwitch(CodegenConstants.MODELS, null);
121129
generateSupportingFiles = System.getProperty(CodegenConstants.SUPPORTING_FILES) != null ? Boolean.TRUE : getGeneratorPropertyDefaultSwitch(CodegenConstants.SUPPORTING_FILES, null);

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,20 +315,18 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
315315
if (var.name.equalsIgnoreCase(cm.name)) {
316316
var.name = "_" + var.name;
317317
}
318-
319-
if(var.isEnum) {
320-
// In C#, Enums are considered primitives because they're compiled to numerical types (int by default)
321-
var.isPrimitiveType = true;
322-
323-
// HACK: Consider Nullable<Enum> as a container so we can properly handle unsupplied enum values as null.
324-
var.isContainer = var.required;
325-
}
326318
}
327319
}
328320
// process enum in models
329321
return postProcessModelsEnum(objs);
330322
}
331323

324+
/**
325+
* Invoked by {@link DefaultGenerator} after all models have been post-processed, allowing for a last pass of codegen-specific model cleanup.
326+
*
327+
* @param objs Current state of codegen object model.
328+
* @return An in-place modified state of the codegen object model.
329+
*/
332330
@Override
333331
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
334332
final Map<String, Object> processed = super.postProcessAllModels(objs);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/// <value>{{description}}</value>
3333
{{/description}}
3434
[DataMember(Name="{{baseName}}", EmitDefaultValue={{emitDefaultValue}})]
35-
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}?{{/isContainer}} {{name}} { get; set; }
35+
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}} {{name}} { get; set; }
3636
{{/isEnum}}
3737
{{/vars}}
3838
{{#hasRequired}}
@@ -55,7 +55,7 @@
5555
{{#hasOnlyReadOnly}}
5656
[JsonConstructorAttribute]
5757
{{/hasOnlyReadOnly}}
58-
public {{classname}}({{#readWriteVars}}{{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}?{{/isContainer}}{{/isEnum}} {{name}} = {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}default({{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}?{{/isContainer}}{{/isEnum}}){{/defaultValue}}{{^-last}}, {{/-last}}{{/readWriteVars}}){{#parent}} : base({{#parentVars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/parentVars}}){{/parent}}
58+
public {{classname}}({{#readWriteVars}}{{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}}{{/isEnum}} {{name}} = {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}default({{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}}{{/isEnum}}){{/defaultValue}}{{^-last}}, {{/-last}}{{/readWriteVars}}){{#parent}} : base({{#parentVars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/parentVars}}){{/parent}}
5959
{
6060
{{#vars}}
6161
{{^isInherited}}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* My title
3+
*
4+
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
5+
*
6+
* OpenAPI spec version: 1
7+
*
8+
* Generated by: https://github.com/swagger-api/swagger-codegen.git
9+
*/
10+
11+
using System;
12+
using System.Linq;
13+
using System.IO;
14+
using System.Text;
15+
using System.Text.RegularExpressions;
16+
using System.Collections;
17+
using System.Collections.Generic;
18+
using System.Collections.ObjectModel;
19+
using System.Runtime.Serialization;
20+
using Newtonsoft.Json;
21+
using Newtonsoft.Json.Converters;
22+
using System.ComponentModel.DataAnnotations;
23+
using SwaggerDateConverter = IO.Swagger.Client.SwaggerDateConverter;
24+
25+
namespace IO.Swagger.Model
26+
{
27+
/// <summary>
28+
/// Invalid use of required on $ref enum, per Swagger 2.0 spec: Any members other than &#39;$ref&#39; in a JSON Reference object SHALL be ignored. See My_Class_With_Required_Inline_Enum for appropriate usage.
29+
/// </summary>
30+
[DataContract]
31+
public partial class MyClassWithInvalidRequiredEnumUsageOnRef : IEquatable<MyClassWithInvalidRequiredEnumUsageOnRef>, IValidatableObject
32+
{
33+
/// <summary>
34+
/// Gets or Sets Days
35+
/// </summary>
36+
[DataMember(Name="days", EmitDefaultValue=false)]
37+
public WeekDays? Days { get; set; }
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="MyClassWithInvalidRequiredEnumUsageOnRef" /> class.
40+
/// </summary>
41+
/// <param name="First">First.</param>
42+
/// <param name="Days">Days.</param>
43+
public MyClassWithInvalidRequiredEnumUsageOnRef(bool? First = default(bool?), WeekDays? Days = default(WeekDays?))
44+
{
45+
this.First = First;
46+
this.Days = Days;
47+
}
48+
49+
/// <summary>
50+
/// Gets or Sets First
51+
/// </summary>
52+
[DataMember(Name="first", EmitDefaultValue=false)]
53+
public bool? First { get; set; }
54+
55+
56+
/// <summary>
57+
/// Returns the string presentation of the object
58+
/// </summary>
59+
/// <returns>String presentation of the object</returns>
60+
public override string ToString()
61+
{
62+
var sb = new StringBuilder();
63+
sb.Append("class MyClassWithInvalidRequiredEnumUsageOnRef {\n");
64+
sb.Append(" First: ").Append(First).Append("\n");
65+
sb.Append(" Days: ").Append(Days).Append("\n");
66+
sb.Append("}\n");
67+
return sb.ToString();
68+
}
69+
70+
/// <summary>
71+
/// Returns the JSON string presentation of the object
72+
/// </summary>
73+
/// <returns>JSON string presentation of the object</returns>
74+
public string ToJson()
75+
{
76+
return JsonConvert.SerializeObject(this, Formatting.Indented);
77+
}
78+
79+
/// <summary>
80+
/// Returns true if objects are equal
81+
/// </summary>
82+
/// <param name="input">Object to be compared</param>
83+
/// <returns>Boolean</returns>
84+
public override bool Equals(object input)
85+
{
86+
return this.Equals(input as MyClassWithInvalidRequiredEnumUsageOnRef);
87+
}
88+
89+
/// <summary>
90+
/// Returns true if MyClassWithInvalidRequiredEnumUsageOnRef instances are equal
91+
/// </summary>
92+
/// <param name="input">Instance of MyClassWithInvalidRequiredEnumUsageOnRef to be compared</param>
93+
/// <returns>Boolean</returns>
94+
public bool Equals(MyClassWithInvalidRequiredEnumUsageOnRef input)
95+
{
96+
if (input == null)
97+
return false;
98+
99+
return
100+
(
101+
this.First == input.First ||
102+
(this.First != null &&
103+
this.First.Equals(input.First))
104+
) &&
105+
(
106+
this.Days == input.Days ||
107+
(this.Days != null &&
108+
this.Days.Equals(input.Days))
109+
);
110+
}
111+
112+
/// <summary>
113+
/// Gets the hash code
114+
/// </summary>
115+
/// <returns>Hash code</returns>
116+
public override int GetHashCode()
117+
{
118+
unchecked // Overflow is fine, just wrap
119+
{
120+
int hashCode = 41;
121+
if (this.First != null)
122+
hashCode = hashCode * 59 + this.First.GetHashCode();
123+
if (this.Days != null)
124+
hashCode = hashCode * 59 + this.Days.GetHashCode();
125+
return hashCode;
126+
}
127+
}
128+
129+
/// <summary>
130+
/// To validate all properties of the instance
131+
/// </summary>
132+
/// <param name="validationContext">Validation context</param>
133+
/// <returns>Validation Result</returns>
134+
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
135+
{
136+
yield break;
137+
}
138+
}
139+
140+
}

0 commit comments

Comments
 (0)