sortParamsByRequiredFlag未应用于模型生成

时间:2017-11-23 21:35:11

标签: c# swagger-codegen

我正在使用swagger-codegen生成c#客户端,但注意到sortParamsByRequiredFlag未应用于模型生成。

例如,这是一个示例配置文件:

{
    "packageVersion" : "1.0.0",
    "sortParamsByRequiredFlag": true,
    "optionalProjectFile" : false
}

以下是模型构造函数生成的截断代码:

/// <summary>
/// Initializes a new instance of the <see cref="V2alpha1CronJobSpec" /> class.
/// </summary>
/// <param name="ConcurrencyPolicy">Specifies how to treat concurrent executions of a Job. Defaults to Allow..</param>
/// <param name="FailedJobsHistoryLimit">The number of failed finished jobs to retain. This is a pointer to distinguish between explicit zero and not specified..</param>
/// <param name="JobTemplate">Specifies the job that will be created when executing a CronJob. (required).</param>
/// <param name="Schedule">The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron. (required).</param>
/// <param name="StartingDeadlineSeconds">Optional deadline in seconds for starting the job if it misses scheduled time for any reason.  Missed jobs executions will be counted as failed ones..</param>
/// <param name="SuccessfulJobsHistoryLimit">The number of successful finished jobs to retain. This is a pointer to distinguish between explicit zero and not specified..</param>
/// <param name="Suspend">This flag tells the controller to suspend subsequent executions, it does not apply to already started executions.  Defaults to false..</param>
public V2alpha1CronJobSpec(string ConcurrencyPolicy = default(string), int? FailedJobsHistoryLimit = default(int?), V2alpha1JobTemplateSpec JobTemplate = default(V2alpha1JobTemplateSpec), string Schedule = default(string), long? StartingDeadlineSeconds = default(long?), int? SuccessfulJobsHistoryLimit = default(int?), bool? Suspend = default(bool?))
{
    // to ensure "JobTemplate" is required (not null)
    if (JobTemplate == null)
    {
        throw new InvalidDataException("JobTemplate is a required property for V2alpha1CronJobSpec and cannot be null");
    }
    else
    {
        this.JobTemplate = JobTemplate;
    }
    // to ensure "Schedule" is required (not null)
    if (Schedule == null)
    {
        throw new InvalidDataException("Schedule is a required property for V2alpha1CronJobSpec and cannot be null");
    }
    else
    {
        this.Schedule = Schedule;
    }
    this.ConcurrencyPolicy = ConcurrencyPolicy;
    this.FailedJobsHistoryLimit = FailedJobsHistoryLimit;
    this.StartingDeadlineSeconds = StartingDeadlineSeconds;
    this.SuccessfulJobsHistoryLimit = SuccessfulJobsHistoryLimit;
    this.Suspend = Suspend;
}

从swagger规范中可以看出,JobTemplateSchedule是必需参数。但是,构造函数中的参数按字母顺序排序。

我一直在浏览swagger-codegen代码库,我认为sortParamsByRequiredFlag仅适用于API生成的方法。

这是设计的吗?我不确定我是否缺少一些我应该设置的配置?

Here是我打开的GitHub问题,但没有听到任何内容。

2 个答案:

答案 0 :(得分:0)

这不是为什么Swagger没有生成正确的代码,而是解决您在GitHub上的问题的答案。

你绝对不需要使用它:

var jobSpec = new V2alpha1CronJobSpec(null, null, new V2alpha1JobTemplateSpec(), "stringValue", null, ...);

你可以使用它(称为命名参数):

var jobSpec = new V2alpha1CronJobSpec(JobTemplate: new V2alpha1JobTemplateSpec(), Schedule: "stringValue");

答案 1 :(得分:0)

对于c#客户端,此标志在主代码库中被忽略。 您可以创建自己的扩展名并重写fromModel方法以对属性进行排序。 看看下面的代码。

@Override
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
    CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);

    if (sortParamsByRequiredFlag) 
    {
        Collections.sort(codegenModel.readWriteVars, new Comparator<CodegenProperty>() {
            @Override
            public int compare(CodegenProperty one, CodegenProperty another) {
                if (one.required == another.required) return 0;
                else if (one.required) return -1;
                else return 1;
            }
        });
        System.out.println("***Sorting based on required params in model....***");
    }
    return codegenModel;
}