Skip to content

Commit 7e52992

Browse files
authored
[csharp][generichost] Implement not required nullable properties (#16810)
* 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
1 parent 2f655f1 commit 7e52992

490 files changed

Lines changed: 44717 additions & 7696 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,21 +418,28 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
418418

419419
@Override
420420
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
421+
CopyLambda copyLambda = new CopyLambda();
422+
421423
return super.addMustacheLambdas()
422424
.put("camelcase_param", new CamelCaseLambda().generator(this).escapeAsParamName(true))
423425
.put("required", new RequiredParameterLambda())
424426
.put("optional", new OptionalParameterLambda().generator(this))
425427
.put("joinWithComma", new JoinWithCommaLambda())
428+
.put("joinWithAmpersand", new JoinWithCommaLambda(true, " ", " && "))
426429
.put("joinLinesWithComma", new JoinWithCommaLambda(false, "\n", ",\n"))
427430
.put("joinConditions", new JoinWithCommaLambda(true, " ", " && "))
428431
.put("trimLineBreaks", new TrimLineBreaksLambda())
429432
.put("trimTrailingWithNewLine", new TrimTrailingWhiteSpaceLambda(true))
430433
.put("trimTrailing", new TrimTrailingWhiteSpaceLambda(false))
431434
.put("first", new FirstLambda(" "))
432435
.put("firstDot", new FirstLambda("\\."))
436+
.put("indent1", new IndentedLambda(4, " ", false, true))
433437
.put("indent3", new IndentedLambda(12, " ", false, true))
434438
.put("indent4", new IndentedLambda(16, " ", false, true))
435-
.put("uniqueLinesWithNewLine", new UniqueLambda("\n", true));
439+
.put("copy", copyLambda)
440+
.put("paste", new PasteLambda(copyLambda, true, true, true, false))
441+
.put("pasteOnce", new PasteLambda(copyLambda, true, true, true, true))
442+
.put("pasteLine", new PasteLambda(copyLambda, true, true, false, false));
436443
}
437444

438445
@Override

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,13 @@ public CodegenModel fromModel(String name, Schema model) {
454454
Collections.sort(codegenModel.readWriteVars, propertyComparatorByName);
455455
Collections.sort(codegenModel.parentVars, propertyComparatorByName);
456456

457-
Comparator<CodegenProperty> comparator = propertyComparatorByNullable.thenComparing(propertyComparatorByDefaultValue);
458-
Collections.sort(codegenModel.vars, comparator);
459-
Collections.sort(codegenModel.allVars, comparator);
460-
Collections.sort(codegenModel.requiredVars, comparator);
461-
Collections.sort(codegenModel.optionalVars, comparator);
462-
Collections.sort(codegenModel.readOnlyVars, comparator);
463-
Collections.sort(codegenModel.readWriteVars, comparator);
464-
Collections.sort(codegenModel.parentVars, comparator);
457+
Collections.sort(codegenModel.vars, propertyComparatorByNotNullableRequiredNoDefault);
458+
Collections.sort(codegenModel.allVars, propertyComparatorByNotNullableRequiredNoDefault);
459+
Collections.sort(codegenModel.requiredVars, propertyComparatorByNotNullableRequiredNoDefault);
460+
Collections.sort(codegenModel.optionalVars, propertyComparatorByNotNullableRequiredNoDefault);
461+
Collections.sort(codegenModel.readOnlyVars, propertyComparatorByNotNullableRequiredNoDefault);
462+
Collections.sort(codegenModel.readWriteVars, propertyComparatorByNotNullableRequiredNoDefault);
463+
Collections.sort(codegenModel.parentVars, propertyComparatorByNotNullableRequiredNoDefault);
465464
}
466465

467466
return codegenModel;
@@ -474,24 +473,12 @@ public int compare(CodegenProperty one, CodegenProperty another) {
474473
}
475474
};
476475

477-
public static Comparator<CodegenProperty> propertyComparatorByDefaultValue = new Comparator<CodegenProperty>() {
476+
public static Comparator<CodegenProperty> propertyComparatorByNotNullableRequiredNoDefault = new Comparator<CodegenProperty>() {
478477
@Override
479478
public int compare(CodegenProperty one, CodegenProperty another) {
480-
if ((one.defaultValue == null) == (another.defaultValue == null))
481-
return 0;
482-
else if (one.defaultValue == null)
483-
return -1;
484-
else
485-
return 1;
486-
}
487-
};
488-
489-
public static Comparator<CodegenProperty> propertyComparatorByNullable = new Comparator<CodegenProperty>() {
490-
@Override
491-
public int compare(CodegenProperty one, CodegenProperty another) {
492-
if (one.isNullable == another.isNullable)
479+
if (one.isNullable == another.isNullable && one.required == another.required && (one.defaultValue == null) == (another.defaultValue == null))
493480
return 0;
494-
else if (Boolean.FALSE.equals(one.isNullable))
481+
else if (!one.isNullable && one.required && one.defaultValue == null)
495482
return -1;
496483
else
497484
return 1;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.openapitools.codegen.templating.mustache;
18+
19+
import java.io.IOException;
20+
import java.io.Writer;
21+
22+
import com.samskivert.mustache.Mustache;
23+
import com.samskivert.mustache.Template.Fragment;
24+
25+
/**
26+
* Saves template text to be used later.
27+
*
28+
* Register:
29+
* <pre>
30+
* additionalProperties.put("copy", new CopyLambda());
31+
* </pre>
32+
*
33+
* Use:
34+
* <pre>
35+
* {{#copy}}{{name}}{{/copy}}
36+
* </pre>
37+
*/
38+
public class CopyLambda implements Mustache.Lambda {
39+
public String savedContent;
40+
41+
public CopyLambda() {
42+
}
43+
44+
@Override
45+
public void execute(Fragment fragment, Writer writer) throws IOException {
46+
savedContent = fragment.execute().stripTrailing();
47+
}
48+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.openapitools.codegen.templating.mustache;
18+
19+
import java.io.IOException;
20+
import java.io.Writer;
21+
22+
import com.samskivert.mustache.Mustache;
23+
import com.samskivert.mustache.Template.Fragment;
24+
25+
/**
26+
* Writes text that was previously saved.
27+
*
28+
* Register:
29+
* <pre>
30+
* additionalProperties.put("paste", new PasteLambda(copyLambda, true, true, true, false));
31+
* </pre>
32+
*
33+
* Use:
34+
* <pre>
35+
* {{#paste}}{{/paste}}
36+
* </pre>
37+
*/
38+
public class PasteLambda implements Mustache.Lambda {
39+
private final CopyLambda copyLambda;
40+
private final Boolean stripLeading;
41+
private final Boolean stripTrailing;
42+
private final Boolean endWithLineBreak;
43+
private final Boolean clear;
44+
45+
public PasteLambda(CopyLambda copyLambda, Boolean stripLeading, Boolean stripTrailing, Boolean endWithLineBreak, boolean clear) {
46+
this.copyLambda = copyLambda;
47+
this.stripLeading = stripLeading;
48+
this.stripTrailing = stripTrailing;
49+
this.endWithLineBreak = endWithLineBreak;
50+
this.clear = clear;
51+
}
52+
53+
@Override
54+
public void execute(Fragment fragment, Writer writer) throws IOException {
55+
String content = this.copyLambda.savedContent;
56+
57+
if (content == null) {
58+
return;
59+
}
60+
61+
if (this.stripTrailing){
62+
content = content.stripTrailing();
63+
}
64+
if (this.stripLeading) {
65+
content = content.stripLeading();
66+
}
67+
if (this.endWithLineBreak && !content.endsWith("\n")){
68+
content = content + "\n";
69+
}
70+
writer.write(content);
71+
72+
if (this.clear) {
73+
this.copyLambda.savedContent = null;
74+
}
75+
}
76+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isNullable}}{{nrt?}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}}{{/isNullable}}
1+
{{#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}}

0 commit comments

Comments
 (0)