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] Duration format property support #20519

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generators/aspnet-fastendpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Object</li>
<li>String</li>
<li>System.IO.Stream</li>
<li>TimeSpan</li>
<li>TimeSpan?</li>
<li>bool</li>
<li>bool?</li>
<li>byte[]</li>
Expand Down
2 changes: 2 additions & 0 deletions docs/generators/aspnetcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Object</li>
<li>String</li>
<li>System.IO.Stream</li>
<li>TimeSpan</li>
<li>TimeSpan?</li>
<li>bool</li>
<li>bool?</li>
<li>byte[]</li>
Expand Down
2 changes: 2 additions & 0 deletions docs/generators/csharp-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Object</li>
<li>String</li>
<li>System.IO.Stream</li>
<li>TimeSpan</li>
<li>TimeSpan?</li>
<li>bool</li>
<li>bool?</li>
<li>byte[]</li>
Expand Down
2 changes: 2 additions & 0 deletions docs/generators/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Object</li>
<li>String</li>
<li>System.IO.Stream</li>
<li>TimeSpan</li>
<li>TimeSpan?</li>
<li>bool</li>
<li>bool?</li>
<li>byte[]</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public String arrayModelType;
public boolean isAlias; // Is this effectively an alias of another simple type
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble, isDate, isDateTime,
isDecimal, isShort, isUnboundedInteger, isPrimitiveType, isBoolean, isFreeFormObject;
isDuration, isDecimal, isShort, isUnboundedInteger, isPrimitiveType, isBoolean, isFreeFormObject;
private boolean additionalPropertiesIsAnyType;
public List<CodegenProperty> vars = new ArrayList<>(); // all properties (without parent's properties)
@Getter @Setter
Expand Down Expand Up @@ -544,6 +544,16 @@ public void setIsDateTime(boolean isDateTime) {
this.isDateTime = isDateTime;
}

@Override
public boolean getIsDuration() {
return isDuration;
}

@Override
public void setIsDuration(boolean isDuration) {
this.isDuration = isDuration;
}

@Override
public boolean getIsMap() {
return isMap;
Expand Down Expand Up @@ -878,6 +888,7 @@ public boolean equals(Object o) {
isDouble == that.isDouble &&
isDate == that.isDate &&
isDateTime == that.isDateTime &&
isDuration == that.isDuration &&
hasVars == that.hasVars &&
emptyVars == that.emptyVars &&
hasMoreModels == that.hasMoreModels &&
Expand Down Expand Up @@ -979,7 +990,7 @@ public int hashCode() {
getDescription(), getClassVarName(), getModelJson(), getDataType(), getXmlPrefix(), getXmlNamespace(),
getXmlName(), getClassFilename(), getUnescapedDescription(), getDiscriminator(), getDefaultValue(),
getArrayModelType(), isAlias, isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble,
isDate, isDateTime, isNull, hasValidation, isShort, isUnboundedInteger, isBoolean,
isDate, isDateTime, isDuration, isNull, hasValidation, isShort, isUnboundedInteger, isBoolean,
getVars(), getAllVars(), getNonNullableVars(), getRequiredVars(), getOptionalVars(), getReadOnlyVars(), getReadWriteVars(),
getParentVars(), getAllowableValues(), getMandatory(), getAllMandatory(), getImports(), hasVars,
isEmptyVars(), hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArray,
Expand Down Expand Up @@ -1035,6 +1046,7 @@ public String toString() {
sb.append(", isDouble=").append(isDouble);
sb.append(", isDate=").append(isDate);
sb.append(", isDateTime=").append(isDateTime);
sb.append(", isDuration=").append(isDuration);
sb.append(", vars=").append(vars);
sb.append(", allVars=").append(allVars);
sb.append(", nonNullableVars=").append(nonNullableVars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public Map<String, Example> examples;
public String jsonSchema;
public boolean isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary,
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword, isFreeFormObject, isAnyType, isShort, isUnboundedInteger;
isBoolean, isDate, isDateTime, isDuration, isUuid, isUri, isEmail, isPassword, isFreeFormObject, isAnyType, isShort, isUnboundedInteger;
public boolean isArray, isMap;
/** datatype is the generic inner parameter of a std::optional for C++, or Optional (Java) */
public boolean isOptional;
Expand Down Expand Up @@ -259,6 +259,7 @@ public CodegenParameter copy() {
output.isBoolean = this.isBoolean;
output.isDate = this.isDate;
output.isDateTime = this.isDateTime;
output.isDuration = this.isDuration;
output.isUuid = this.isUuid;
output.isUri = this.isUri;
output.isEmail = this.isEmail;
Expand Down Expand Up @@ -286,7 +287,7 @@ public int hashCode() {
unescapedDescription, baseType, containerType, containerTypeMapped, defaultValue,
enumDefaultValue, enumName, style, isDeepObject, isMatrix, isAllowEmptyValue, example, examples,
jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal,
isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword,
isByteArray, isBinary, isBoolean, isDate, isDateTime, isDuration, isUuid, isUri, isEmail, isPassword,
isFreeFormObject, isAnyType, isArray, isMap, isOptional, isFile, isEnum, isEnumRef, _enum, allowableValues,
items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation,
getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(),
Expand Down Expand Up @@ -329,6 +330,7 @@ public boolean equals(Object o) {
isBoolean == that.isBoolean &&
isDate == that.isDate &&
isDateTime == that.isDateTime &&
isDuration == that.isDuration &&
isUuid == that.isUuid &&
isUri == that.isUri &&
isEmail == that.isEmail &&
Expand Down Expand Up @@ -463,6 +465,7 @@ public String toString() {
sb.append(", isBoolean=").append(isBoolean);
sb.append(", isDate=").append(isDate);
sb.append(", isDateTime=").append(isDateTime);
sb.append(", isDuration=").append(isDuration);
sb.append(", isUuid=").append(isUuid);
sb.append(", isUri=").append(isUri);
sb.append(", isEmail=").append(isEmail);
Expand Down Expand Up @@ -754,6 +757,16 @@ public void setIsDateTime(boolean isDateTime) {
this.isDateTime = isDateTime;
}

@Override
public boolean getIsDuration() {
return isDuration;
}

@Override
public void setIsDuration(boolean isDuration) {
this.isDuration = isDuration;
}

@Override
public boolean getIsMap() {
return isMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
public boolean isBoolean;
public boolean isDate; // full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21
public boolean isDateTime; // the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z
public boolean isDuration;
public boolean isUuid;
public boolean isUri;
public boolean isEmail;
Expand Down Expand Up @@ -488,6 +489,16 @@ public void setIsDateTime(boolean isDateTime) {
this.isDateTime = isDateTime;
}

@Override
public boolean getIsDuration() {
return isDuration;
}

@Override
public void setIsDuration(boolean isDuration) {
this.isDuration = isDuration;
}

@Override
public boolean getIsMap() {
return isMap;
Expand Down Expand Up @@ -1014,6 +1025,7 @@ public String toString() {
sb.append(", isBoolean=").append(isBoolean);
sb.append(", isDate=").append(isDate);
sb.append(", isDateTime=").append(isDateTime);
sb.append(", isDuration=").append(isDuration);
sb.append(", isUuid=").append(isUuid);
sb.append(", isUri=").append(isUri);
sb.append(", isEmail=").append(isEmail);
Expand Down Expand Up @@ -1111,6 +1123,7 @@ public boolean equals(Object o) {
isBoolean == that.isBoolean &&
isDate == that.isDate &&
isDateTime == that.isDateTime &&
isDuration == that.isDuration &&
isUuid == that.isUuid &&
isUri == that.isUri &&
isEmail == that.isEmail &&
Expand Down Expand Up @@ -1210,7 +1223,7 @@ public int hashCode() {
exclusiveMinimum, exclusiveMaximum, required, deprecated,
hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric,
isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isFile,
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword, isFreeFormObject,
isBoolean, isDate, isDateTime, isDuration, isUuid, isUri, isEmail, isPassword, isFreeFormObject,
isArray, isMap, isOptional, isEnum, isInnerEnum, isEnumRef, isAnyType, isReadOnly, isWriteOnly, isNullable, isShort,
isUnboundedInteger, isSelfReference, isCircularReference, isDiscriminator, isNew, isOverridden, _enum,
allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public boolean isBoolean;
public boolean isDate;
public boolean isDateTime;
public boolean isDuration;
public boolean isUuid;
public boolean isEmail;
public boolean isPassword;
Expand Down Expand Up @@ -109,7 +110,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public int hashCode() {
return Objects.hash(headers, code, message, examples, dataType, baseType, containerType, containerTypeMapped, hasHeaders,
isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBoolean, isDate,
isDateTime, isUuid, isEmail, isPassword, isModel, isFreeFormObject, isAnyType, isDefault, simpleType, primitiveType,
isDateTime, isDuration, isUuid, isEmail, isPassword, isModel, isFreeFormObject, isAnyType, isDefault, simpleType, primitiveType,
isMap, isOptional, isArray, isBinary, isFile, schema, jsonSchema, vendorExtensions, items, additionalProperties,
vars, requiredVars, isNull, isVoid, hasValidation, isShort, isUnboundedInteger,
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
Expand Down Expand Up @@ -139,6 +140,7 @@ public boolean equals(Object o) {
isBoolean == that.isBoolean &&
isDate == that.isDate &&
isDateTime == that.isDateTime &&
isDuration == that.isDuration &&
isUuid == that.isUuid &&
isEmail == that.isEmail &&
isPassword == that.isPassword &&
Expand Down Expand Up @@ -485,6 +487,16 @@ public void setIsDateTime(boolean isDateTime) {
this.isDateTime = isDateTime;
}

@Override
public boolean getIsDuration() {
return isDuration;
}

@Override
public void setIsDuration(boolean isDuration) {
this.isDuration = isDuration;
}

@Override
public boolean getIsMap() {
return isMap;
Expand Down Expand Up @@ -586,6 +598,7 @@ public String toString() {
sb.append(", isBoolean=").append(isBoolean);
sb.append(", isDate=").append(isDate);
sb.append(", isDateTime=").append(isDateTime);
sb.append(", isDuration=").append(isDuration);
sb.append(", isUuid=").append(isUuid);
sb.append(", isEmail=").append(isEmail);
sb.append(", isPassword=").append(isPassword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public interface IJsonSchemaValidationProperties {

void setIsDateTime(boolean isDateTime);

boolean getIsDuration();

void setIsDuration(boolean isDuration);

// true when the schema type is object
boolean getIsMap();

Expand Down Expand Up @@ -312,6 +316,8 @@ default void setTypeProperties(Schema p, OpenAPI openAPI) {
;
} else if (ModelUtils.isDateTimeSchema(p)) {
;
} else if (ModelUtils.isDurationSchema(p)) {
;
} else if (ModelUtils.isDecimalSchema(p)) { // type: string, format: number
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public AbstractCSharpCodegen() {
"DateTimeOffset",
"DateOnly?",
"DateOnly",
"TimeSpan?",
"TimeSpan",
"Boolean",
"Double",
"Decimal",
Expand Down Expand Up @@ -1427,6 +1429,10 @@ public String toDefaultValue(Schema p) {
if (p.getDefault() != null) {
return "\"" + p.getDefault().toString() + "\"";
}
} else if (ModelUtils.isDurationSchema(p)) {
if (p.getDefault() != null) {
return "\"" + p.getDefault().toString() + "\"";
}
} else if (ModelUtils.isNumberSchema(p)) {
if (p.getDefault() != null) {
if (ModelUtils.isFloatSchema(p)) { // float
Expand Down Expand Up @@ -1986,7 +1992,7 @@ protected Set<String> getNullableTypes() {
}

protected Set<String> getValueTypes() {
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double", "DateTime", "DateOnly", "DateTimeOffset", "Guid"));
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double", "DateTime", "DateOnly", "DateTimeOffset", "TimeSpan", "Guid"));
balassit marked this conversation as resolved.
Show resolved Hide resolved
}

protected void setTypeMapping() {
Expand All @@ -2006,6 +2012,7 @@ protected void setTypeMapping() {
typeMapping.put("BigDecimal", "decimal");
typeMapping.put("DateTime", this.useDateTimeOffsetFlag ? "DateTimeOffset" : "DateTime");
typeMapping.put("date", this.useDateOnly() ? "DateOnly" : "DateTime");
typeMapping.put("duration", "TimeSpan");
typeMapping.put("file", "System.IO.Stream");
typeMapping.put("array", "List");
typeMapping.put("list", "List");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private void addCentralizedPackageManagementOption(){
@Override
protected Set<String> getNullableTypes() {
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double",
"DateTime", "DateOnly", "DateTimeOffset", "Guid"));
"DateTime", "DateOnly", "DateTimeOffset", "TimeSpan", "Guid"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public CSharpFunctionsServerCodegen() {
@Override
protected Set<String> getNullableTypes() {
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double",
"DateTime", "DateTimeOffset", "Guid"));
"DateTime", "DateTimeOffset", "TimeSpan", "Guid"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public CSharpReducedClientCodegen() {
@Override
protected Set<String> getNullableTypes() {
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double",
"DateTime", "DateTimeOffset", "Guid"));
"DateTime", "DateTimeOffset", "TimeSpan", "Guid"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,12 @@ public static boolean isDateTimeSchema(Schema schema) {
&& SchemaTypeUtil.DATE_TIME_FORMAT.equals(schema.getFormat()));
}

public static boolean isDurationSchema(Schema schema) {
// format: duration (todo: swagger parser does not support duration)
return (SchemaTypeUtil.STRING_TYPE.equals(getType(schema))
&& "duration".equals(schema.getFormat()));
}

public static boolean isPasswordSchema(Schema schema) {
return (schema instanceof PasswordSchema) ||
// double
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@
if (utf8JsonReader.TokenType != JsonTokenType.Null)
{{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}} = {{>OptionProperty}}JsonSerializer.Deserialize<DateTime{{#isNullable}}?{{/isNullable}}>(ref utf8JsonReader, jsonSerializerOptions));
{{/isDateTime}}
{{#isDuration}}
if (utf8JsonReader.TokenType != JsonTokenType.Null)
{{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}} = {{>OptionProperty}}JsonSerializer.Deserialize<Timespan{{#isNullable}}?{{/isNullable}}>(ref utf8JsonReader, jsonSerializerOptions));
{{/isDuration}}
{{#isEnum}}
{{^isMap}}
{{#isNumeric}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,10 @@ components:
type: string
format: date-time
example: '2007-12-03T10:15:30+01:00'
duration:
type: string
format: duration
example: '1.03:14:56.1667'
uuid:
type: string
format: uuid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,7 @@ components:
- number
- byte
- date
- duration
- password
properties:
integer:
Expand Down Expand Up @@ -1718,6 +1719,10 @@ components:
type: string
format: date-time
example: '2007-12-03T10:15:30+01:00'
duration:
type: string
format: duration
example: '1.03:14:56.1667'
uuid:
type: string
format: uuid
Expand Down
Loading
Loading