Skip to content

Commit

Permalink
[csharp][generichost] Implement not required nullable properties (#16810
Browse files Browse the repository at this point in the history
)

* init

* fixed read and write

* completed changes using latest-nrt sample

* fixed all samples

* add null check on write, change on exception

* resolved conflicts

* build samples

* added backing property for not required properties

* more not required and nullable hanlding improvements

* revert sample updates for a merge master

* revert sample updates for a merge master

* sample build is working, need to remove warnings

* fixed warnings in .net 7 with nrt

* fixed manual tests

* fixed all samples

* fix npe

* removed debugging lines

* revert changes to unused file

* removed unused lambdas

* fix a serialization bug

* make option a hidden property

* updated documentation

* improved parameter ordering
  • Loading branch information
devhl-labs authored Nov 13, 2023
1 parent 2f655f1 commit 7e52992
Show file tree
Hide file tree
Showing 490 changed files with 44,717 additions and 7,696 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -418,21 +418,28 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio

@Override
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
CopyLambda copyLambda = new CopyLambda();

return super.addMustacheLambdas()
.put("camelcase_param", new CamelCaseLambda().generator(this).escapeAsParamName(true))
.put("required", new RequiredParameterLambda())
.put("optional", new OptionalParameterLambda().generator(this))
.put("joinWithComma", new JoinWithCommaLambda())
.put("joinWithAmpersand", new JoinWithCommaLambda(true, " ", " && "))
.put("joinLinesWithComma", new JoinWithCommaLambda(false, "\n", ",\n"))
.put("joinConditions", new JoinWithCommaLambda(true, " ", " && "))
.put("trimLineBreaks", new TrimLineBreaksLambda())
.put("trimTrailingWithNewLine", new TrimTrailingWhiteSpaceLambda(true))
.put("trimTrailing", new TrimTrailingWhiteSpaceLambda(false))
.put("first", new FirstLambda(" "))
.put("firstDot", new FirstLambda("\\."))
.put("indent1", new IndentedLambda(4, " ", false, true))
.put("indent3", new IndentedLambda(12, " ", false, true))
.put("indent4", new IndentedLambda(16, " ", false, true))
.put("uniqueLinesWithNewLine", new UniqueLambda("\n", true));
.put("copy", copyLambda)
.put("paste", new PasteLambda(copyLambda, true, true, true, false))
.put("pasteOnce", new PasteLambda(copyLambda, true, true, true, true))
.put("pasteLine", new PasteLambda(copyLambda, true, true, false, false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,13 @@ public CodegenModel fromModel(String name, Schema model) {
Collections.sort(codegenModel.readWriteVars, propertyComparatorByName);
Collections.sort(codegenModel.parentVars, propertyComparatorByName);

Comparator<CodegenProperty> comparator = propertyComparatorByNullable.thenComparing(propertyComparatorByDefaultValue);
Collections.sort(codegenModel.vars, comparator);
Collections.sort(codegenModel.allVars, comparator);
Collections.sort(codegenModel.requiredVars, comparator);
Collections.sort(codegenModel.optionalVars, comparator);
Collections.sort(codegenModel.readOnlyVars, comparator);
Collections.sort(codegenModel.readWriteVars, comparator);
Collections.sort(codegenModel.parentVars, comparator);
Collections.sort(codegenModel.vars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.allVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.requiredVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.optionalVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.readOnlyVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.readWriteVars, propertyComparatorByNotNullableRequiredNoDefault);
Collections.sort(codegenModel.parentVars, propertyComparatorByNotNullableRequiredNoDefault);
}

return codegenModel;
Expand All @@ -474,24 +473,12 @@ public int compare(CodegenProperty one, CodegenProperty another) {
}
};

public static Comparator<CodegenProperty> propertyComparatorByDefaultValue = new Comparator<CodegenProperty>() {
public static Comparator<CodegenProperty> propertyComparatorByNotNullableRequiredNoDefault = new Comparator<CodegenProperty>() {
@Override
public int compare(CodegenProperty one, CodegenProperty another) {
if ((one.defaultValue == null) == (another.defaultValue == null))
return 0;
else if (one.defaultValue == null)
return -1;
else
return 1;
}
};

public static Comparator<CodegenProperty> propertyComparatorByNullable = new Comparator<CodegenProperty>() {
@Override
public int compare(CodegenProperty one, CodegenProperty another) {
if (one.isNullable == another.isNullable)
if (one.isNullable == another.isNullable && one.required == another.required && (one.defaultValue == null) == (another.defaultValue == null))
return 0;
else if (Boolean.FALSE.equals(one.isNullable))
else if (!one.isNullable && one.required && one.defaultValue == null)
return -1;
else
return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openapitools.codegen.templating.mustache;

import java.io.IOException;
import java.io.Writer;

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template.Fragment;

/**
* Saves template text to be used later.
*
* Register:
* <pre>
* additionalProperties.put("copy", new CopyLambda());
* </pre>
*
* Use:
* <pre>
* {{#copy}}{{name}}{{/copy}}
* </pre>
*/
public class CopyLambda implements Mustache.Lambda {
public String savedContent;

public CopyLambda() {
}

@Override
public void execute(Fragment fragment, Writer writer) throws IOException {
savedContent = fragment.execute().stripTrailing();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openapitools.codegen.templating.mustache;

import java.io.IOException;
import java.io.Writer;

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template.Fragment;

/**
* Writes text that was previously saved.
*
* Register:
* <pre>
* additionalProperties.put("paste", new PasteLambda(copyLambda, true, true, true, false));
* </pre>
*
* Use:
* <pre>
* {{#paste}}{{/paste}}
* </pre>
*/
public class PasteLambda implements Mustache.Lambda {
private final CopyLambda copyLambda;
private final Boolean stripLeading;
private final Boolean stripTrailing;
private final Boolean endWithLineBreak;
private final Boolean clear;

public PasteLambda(CopyLambda copyLambda, Boolean stripLeading, Boolean stripTrailing, Boolean endWithLineBreak, boolean clear) {
this.copyLambda = copyLambda;
this.stripLeading = stripLeading;
this.stripTrailing = stripTrailing;
this.endWithLineBreak = endWithLineBreak;
this.clear = clear;
}

@Override
public void execute(Fragment fragment, Writer writer) throws IOException {
String content = this.copyLambda.savedContent;

if (content == null) {
return;
}

if (this.stripTrailing){
content = content.stripTrailing();
}
if (this.stripLeading) {
content = content.stripLeading();
}
if (this.endWithLineBreak && !content.endsWith("\n")){
content = content + "\n";
}
writer.write(content);

if (this.clear) {
this.copyLambda.savedContent = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#isNullable}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}}{{/isNullable}}
{{#lambda.first}}{{#isNullable}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} {{/isNullable}}{{^required}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} {{/required}}{{/lambda.first}}
Loading

0 comments on commit 7e52992

Please sign in to comment.