Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[csharp][generichost] Implement not required nullable properties #16810

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7ddf7be
init
devhl-labs Oct 12, 2023
3196c35
fixed read and write
devhl-labs Oct 15, 2023
a450627
completed changes using latest-nrt sample
devhl-labs Oct 15, 2023
763ac29
fixed all samples
devhl-labs Oct 15, 2023
9a23a30
add null check on write, change on exception
devhl-labs Oct 15, 2023
0b59e51
resolved conflicts
devhl-labs Oct 15, 2023
d79cb9b
resolved conflicts
devhl-labs Oct 15, 2023
9a65524
build samples
devhl-labs Oct 15, 2023
c292416
added backing property for not required properties
devhl-labs Oct 29, 2023
dac6859
more not required and nullable hanlding improvements
devhl-labs Nov 1, 2023
40164d5
revert sample updates for a merge master
devhl-labs Nov 1, 2023
7f8d318
revert sample updates for a merge master
devhl-labs Nov 1, 2023
35fd9a7
Merge branch 'master' into 16520-notrequired-nullable-properties
devhl-labs Nov 1, 2023
37a602d
sample build is working, need to remove warnings
devhl-labs Nov 1, 2023
2383dd2
fixed warnings in .net 7 with nrt
devhl-labs Nov 1, 2023
920e3be
fixed manual tests
devhl-labs Nov 2, 2023
120ba1f
fixed all samples
devhl-labs Nov 5, 2023
3af40a2
fix npe
devhl-labs Nov 5, 2023
e703fae
removed debugging lines
devhl-labs Nov 5, 2023
2527e12
revert changes to unused file
devhl-labs Nov 5, 2023
9cbc15e
removed unused lambdas
devhl-labs Nov 5, 2023
9cf3ee3
fix a serialization bug
devhl-labs Nov 5, 2023
25d2df1
make option a hidden property
devhl-labs Nov 5, 2023
baed4ef
updated documentation
devhl-labs Nov 5, 2023
0116d4a
improved parameter ordering
devhl-labs Nov 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,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
Loading