[csharp] Clean up general enum support integration test, validate different enum usage cases.

This commit is contained in:
James Schubert
2017-10-31 14:17:13 -04:00
parent 8be610b9a4
commit 72b9ab6cab
7 changed files with 430 additions and 10 deletions

View File

@@ -63,6 +63,12 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
return this;
}
/**
* Programmatically disable the output of .swagger-codegen/VERSION, .swagger-codegen-ignore,
* or other metadata files used by Swagger Codegen.
* @param generateSwaggerMetadata true: enable outputs, false: disable outputs
*/
@SuppressWarnings("WeakerAccess")
public void setGenerateSwaggerMetadata(Boolean generateSwaggerMetadata) {
this.generateSwaggerMetadata = generateSwaggerMetadata;
}
@@ -73,6 +79,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
* @param key The system property key
* @param value The system property value
*/
@SuppressWarnings("WeakerAccess")
public void setGeneratorPropertyDefault(final String key, final String value) {
this.generatorPropertyDefaults.put(key, value);
}
@@ -116,6 +123,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
private void configureGeneratorProperties() {
// allows generating only models by specifying a CSV of models to generate, or empty for all
// NOTE: Boolean.TRUE is required below rather than `true` because of JVM boxing constraints and type inference.
generateApis = System.getProperty(CodegenConstants.APIS) != null ? Boolean.TRUE : getGeneratorPropertyDefaultSwitch(CodegenConstants.APIS, null);
generateModels = System.getProperty(CodegenConstants.MODELS) != null ? Boolean.TRUE : getGeneratorPropertyDefaultSwitch(CodegenConstants.MODELS, null);
generateSupportingFiles = System.getProperty(CodegenConstants.SUPPORTING_FILES) != null ? Boolean.TRUE : getGeneratorPropertyDefaultSwitch(CodegenConstants.SUPPORTING_FILES, null);

View File

@@ -315,20 +315,18 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
if (var.name.equalsIgnoreCase(cm.name)) {
var.name = "_" + var.name;
}
if(var.isEnum) {
// In C#, Enums are considered primitives because they're compiled to numerical types (int by default)
var.isPrimitiveType = true;
// HACK: Consider Nullable<Enum> as a container so we can properly handle unsupplied enum values as null.
var.isContainer = var.required;
}
}
}
// process enum in models
return postProcessModelsEnum(objs);
}
/**
* Invoked by {@link DefaultGenerator} after all models have been post-processed, allowing for a last pass of codegen-specific model cleanup.
*
* @param objs Current state of codegen object model.
* @return An in-place modified state of the codegen object model.
*/
@Override
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
final Map<String, Object> processed = super.postProcessAllModels(objs);

View File

@@ -32,7 +32,7 @@
/// <value>{{description}}</value>
{{/description}}
[DataMember(Name="{{baseName}}", EmitDefaultValue={{emitDefaultValue}})]
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}?{{/isContainer}} {{name}} { get; set; }
public {{#complexType}}{{{complexType}}}{{/complexType}}{{^complexType}}{{{datatypeWithEnum}}}{{/complexType}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}} {{name}} { get; set; }
{{/isEnum}}
{{/vars}}
{{#hasRequired}}
@@ -55,7 +55,7 @@
{{#hasOnlyReadOnly}}
[JsonConstructorAttribute]
{{/hasOnlyReadOnly}}
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}}
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}}
{
{{#vars}}
{{^isInherited}}

View File

@@ -0,0 +1,140 @@
/*
* My title
*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 1
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.ComponentModel.DataAnnotations;
using SwaggerDateConverter = IO.Swagger.Client.SwaggerDateConverter;
namespace IO.Swagger.Model
{
/// <summary>
/// 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.
/// </summary>
[DataContract]
public partial class MyClassWithInvalidRequiredEnumUsageOnRef : IEquatable<MyClassWithInvalidRequiredEnumUsageOnRef>, IValidatableObject
{
/// <summary>
/// Gets or Sets Days
/// </summary>
[DataMember(Name="days", EmitDefaultValue=false)]
public WeekDays? Days { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="MyClassWithInvalidRequiredEnumUsageOnRef" /> class.
/// </summary>
/// <param name="First">First.</param>
/// <param name="Days">Days.</param>
public MyClassWithInvalidRequiredEnumUsageOnRef(bool? First = default(bool?), WeekDays? Days = default(WeekDays?))
{
this.First = First;
this.Days = Days;
}
/// <summary>
/// Gets or Sets First
/// </summary>
[DataMember(Name="first", EmitDefaultValue=false)]
public bool? First { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class MyClassWithInvalidRequiredEnumUsageOnRef {\n");
sb.Append(" First: ").Append(First).Append("\n");
sb.Append(" Days: ").Append(Days).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
/// <summary>
/// Returns true if objects are equal
/// </summary>
/// <param name="input">Object to be compared</param>
/// <returns>Boolean</returns>
public override bool Equals(object input)
{
return this.Equals(input as MyClassWithInvalidRequiredEnumUsageOnRef);
}
/// <summary>
/// Returns true if MyClassWithInvalidRequiredEnumUsageOnRef instances are equal
/// </summary>
/// <param name="input">Instance of MyClassWithInvalidRequiredEnumUsageOnRef to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(MyClassWithInvalidRequiredEnumUsageOnRef input)
{
if (input == null)
return false;
return
(
this.First == input.First ||
(this.First != null &&
this.First.Equals(input.First))
) &&
(
this.Days == input.Days ||
(this.Days != null &&
this.Days.Equals(input.Days))
);
}
/// <summary>
/// Gets the hash code
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.First != null)
hashCode = hashCode * 59 + this.First.GetHashCode();
if (this.Days != null)
hashCode = hashCode * 59 + this.Days.GetHashCode();
return hashCode;
}
}
/// <summary>
/// To validate all properties of the instance
/// </summary>
/// <param name="validationContext">Validation context</param>
/// <returns>Validation Result</returns>
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
yield break;
}
}
}

View File

@@ -0,0 +1,219 @@
/*
* My title
*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 1
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.ComponentModel.DataAnnotations;
using SwaggerDateConverter = IO.Swagger.Client.SwaggerDateConverter;
namespace IO.Swagger.Model
{
/// <summary>
/// MyClassWithRequiredInlineEnum
/// </summary>
[DataContract]
public partial class MyClassWithRequiredInlineEnum : IEquatable<MyClassWithRequiredInlineEnum>, IValidatableObject
{
/// <summary>
/// Gets or Sets Days
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum DaysEnum
{
/// <summary>
/// Enum Sun for "sun"
/// </summary>
[EnumMember(Value = "sun")]
Sun,
/// <summary>
/// Enum Mon for "mon"
/// </summary>
[EnumMember(Value = "mon")]
Mon,
/// <summary>
/// Enum Tue for "tue"
/// </summary>
[EnumMember(Value = "tue")]
Tue,
/// <summary>
/// Enum Wed for "wed"
/// </summary>
[EnumMember(Value = "wed")]
Wed,
/// <summary>
/// Enum Thu for "thu"
/// </summary>
[EnumMember(Value = "thu")]
Thu,
/// <summary>
/// Enum Fri for "fri"
/// </summary>
[EnumMember(Value = "fri")]
Fri,
/// <summary>
/// Enum Sat for "sat"
/// </summary>
[EnumMember(Value = "sat")]
Sat
}
/// <summary>
/// Gets or Sets Days
/// </summary>
[DataMember(Name="days", EmitDefaultValue=false)]
public DaysEnum Days { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="MyClassWithRequiredInlineEnum" /> class.
/// </summary>
[JsonConstructorAttribute]
protected MyClassWithRequiredInlineEnum() { }
/// <summary>
/// Initializes a new instance of the <see cref="MyClassWithRequiredInlineEnum" /> class.
/// </summary>
/// <param name="Quarantine">Quarantine.</param>
/// <param name="Grayware">Grayware.</param>
/// <param name="Days">Days (required).</param>
public MyClassWithRequiredInlineEnum(bool? Quarantine = default(bool?), bool? Grayware = default(bool?), DaysEnum Days = default(DaysEnum))
{
// to ensure "Days" is required (not null)
if (Days == null)
{
throw new InvalidDataException("Days is a required property for MyClassWithRequiredInlineEnum and cannot be null");
}
else
{
this.Days = Days;
}
this.Quarantine = Quarantine;
this.Grayware = Grayware;
}
/// <summary>
/// Gets or Sets Quarantine
/// </summary>
[DataMember(Name="quarantine", EmitDefaultValue=false)]
public bool? Quarantine { get; set; }
/// <summary>
/// Gets or Sets Grayware
/// </summary>
[DataMember(Name="grayware", EmitDefaultValue=false)]
public bool? Grayware { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class MyClassWithRequiredInlineEnum {\n");
sb.Append(" Quarantine: ").Append(Quarantine).Append("\n");
sb.Append(" Grayware: ").Append(Grayware).Append("\n");
sb.Append(" Days: ").Append(Days).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
/// <summary>
/// Returns true if objects are equal
/// </summary>
/// <param name="input">Object to be compared</param>
/// <returns>Boolean</returns>
public override bool Equals(object input)
{
return this.Equals(input as MyClassWithRequiredInlineEnum);
}
/// <summary>
/// Returns true if MyClassWithRequiredInlineEnum instances are equal
/// </summary>
/// <param name="input">Instance of MyClassWithRequiredInlineEnum to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(MyClassWithRequiredInlineEnum input)
{
if (input == null)
return false;
return
(
this.Quarantine == input.Quarantine ||
(this.Quarantine != null &&
this.Quarantine.Equals(input.Quarantine))
) &&
(
this.Grayware == input.Grayware ||
(this.Grayware != null &&
this.Grayware.Equals(input.Grayware))
) &&
(
this.Days == input.Days ||
(this.Days != null &&
this.Days.Equals(input.Days))
);
}
/// <summary>
/// Gets the hash code
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Quarantine != null)
hashCode = hashCode * 59 + this.Quarantine.GetHashCode();
if (this.Grayware != null)
hashCode = hashCode * 59 + this.Grayware.GetHashCode();
if (this.Days != null)
hashCode = hashCode * 59 + this.Days.GetHashCode();
return hashCode;
}
}
/// <summary>
/// To validate all properties of the instance
/// </summary>
/// <param name="validationContext">Validation context</param>
/// <returns>Validation Result</returns>
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
yield break;
}
}
}

View File

@@ -58,6 +58,23 @@
}
}
},
"My_Class_With_Invalid_Required_Enum_Usage_On_Ref": {
"description": "Invalid use of required on $ref enum, per Swagger 2.0 spec: Any members other than '$ref' in a JSON Reference object SHALL be ignored. See My_Class_With_Required_Inline_Enum for appropriate usage.",
"properties": {
"first": {
"type": "boolean"
},
"days": {
"$ref": "#/definitions/WeekDays",
"required": true
},
"second": {
"type": "int"
}
}
},
"My_Class_With_Optional_Inline_Enum": {
"properties": {
"quarantine": {
@@ -79,6 +96,30 @@
]
}
}
},
"My_Class_With_Required_Inline_Enum": {
"required": [ "days" ],
"properties": {
"quarantine": {
"type": "boolean"
},
"grayware": {
"type": "boolean"
},
"days": {
"type": "string",
"enum": [
"sun",
"mon",
"tue",
"wed",
"thu",
"fri",
"sat"
]
}
}
}
}
}

View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
declare opts="-DdebugModels -Dproject -Dmodels -DmodelTests=false -DmodelDocs=false $JAVA_OPTS"
declare curdir=$(cd $(dirname "${BASH_SOURCE}") && pwd)
# NOTE: This is sensitive to the location of this script.
declare clijar=${SWAGGER_CODEGEN_CLI_JAR:-$(cd $curdir && cd ../../../../../../../swagger-codegen-cli/target/ && echo $PWD)/swagger-codegen-cli.jar}
exec \java ${opts} -jar ${clijar} generate \
-i enum-support-spec.json -l csharp \
--additional-properties targetFramework=v4.5 \
-o enum-support-expected;