Skip to content

Commit

Permalink
fix: Generate additional properties for date/time-oriented parameters
Browse files Browse the repository at this point in the history
This broadly follows the pattern in DataPropertyModel, but with
changes related to the differences in usage:

- We never need to allow the Raw property to be set, because it
  won't be in responses.
- We need to set an appropriate RequestParameterAttribute, but nothing
  JSON-related.

The properties may behave oddly if users set one property and then
fetch another, but that seems like an unlikely to be a common
scenario.

Fixes #701.
  • Loading branch information
jskeet committed Nov 13, 2023
1 parent 9497537 commit 46435dd
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,13 +644,71 @@ public ListRequest(Google.Apis.Services.IClientService service, string parent) :
[Google.Apis.Util.RequestParameterAttribute("filter", Google.Apis.Util.RequestParameterType.Query)]
public virtual string Filter { get; set; }

/// <summary>The end of the time window.</summary>
private object _intervalEndTime;

/// <summary>
/// String representation of <see cref="IntervalEndTimeDateTimeOffset"/>, formatted for
/// inclusion in the HTTP request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("interval.endTime", Google.Apis.Util.RequestParameterType.Query)]
public virtual object IntervalEndTime { get; set; }
public virtual string IntervalEndTimeRaw { get; private set; }

/// <summary>
/// <seealso cref="object"/> representation of <see cref="IntervalEndTimeRaw"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use IntervalEndTimeDateTimeOffset instead.")]
public virtual object IntervalEndTime
{
get => _intervalEndTime;
set
{
IntervalEndTimeRaw = Google.Apis.Util.Utilities.ConvertToString(value);
_intervalEndTime = value;
}
}

public virtual System.DateTimeOffset? IntervalEndTimeDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseGoogleDateTimeToDateTimeOffset(IntervalEndTimeRaw);
set
{
IntervalEndTimeRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToGoogleDateTime(value);
_intervalEndTime = value;
}
}

/// <summary>The start of the time window.</summary>
private object _intervalStartTime;

/// <summary>
/// String representation of <see cref="IntervalStartTimeDateTimeOffset"/>, formatted for
/// inclusion in the HTTP request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("interval.startTime", Google.Apis.Util.RequestParameterType.Query)]
public virtual object IntervalStartTime { get; set; }
public virtual string IntervalStartTimeRaw { get; private set; }

/// <summary>
/// <seealso cref="object"/> representation of <see cref="IntervalStartTimeRaw"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use IntervalStartTimeDateTimeOffset instead.")]
public virtual object IntervalStartTime
{
get => _intervalStartTime;
set
{
IntervalStartTimeRaw = Google.Apis.Util.Utilities.ConvertToString(value);
_intervalStartTime = value;
}
}

public virtual System.DateTimeOffset? IntervalStartTimeDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseGoogleDateTimeToDateTimeOffset(IntervalStartTimeRaw);
set
{
IntervalStartTimeRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToGoogleDateTime(value);
_intervalStartTime = value;
}
}

/// <summary>Optional. Page size.</summary>
[Google.Apis.Util.RequestParameterAttribute("pageSize", Google.Apis.Util.RequestParameterType.Query)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2489,15 +2489,55 @@ public InstancesRequest(Google.Apis.Services.IClientService service, string cale
/// Upper bound (exclusive) for an event's start time to filter by. Optional. The default is not to filter
/// by start time. Must be an RFC3339 timestamp with mandatory time zone offset.
/// </summary>
public virtual System.DateTimeOffset? TimeMaxDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseDateTimeToDateTimeOffset(TimeMaxRaw);
set => TimeMaxRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToDateTime(value);
}

/// <summary>
/// String representation of <see cref="TimeMaxDateTimeOffset"/>, formatted for inclusion in the HTTP
/// request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("timeMax", Google.Apis.Util.RequestParameterType.Query)]
public virtual System.Nullable<System.DateTime> TimeMax { get; set; }
public virtual string TimeMaxRaw { get; private set; }

/// <summary>
/// <seealso cref="System.DateTime"/> representation of <see cref="TimeMaxDateTimeOffset"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use TimeMaxDateTimeOffset instead.")]
public virtual System.DateTime? TimeMax
{
get => Google.Apis.Util.Utilities.GetDateTimeFromString(TimeMaxRaw);
set => TimeMaxRaw = Google.Apis.Util.Utilities.GetStringFromDateTime(value);
}

/// <summary>
/// Lower bound (inclusive) for an event's end time to filter by. Optional. The default is not to filter by
/// end time. Must be an RFC3339 timestamp with mandatory time zone offset.
/// </summary>
public virtual System.DateTimeOffset? TimeMinDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseDateTimeToDateTimeOffset(TimeMinRaw);
set => TimeMinRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToDateTime(value);
}

/// <summary>
/// String representation of <see cref="TimeMinDateTimeOffset"/>, formatted for inclusion in the HTTP
/// request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("timeMin", Google.Apis.Util.RequestParameterType.Query)]
public virtual System.Nullable<System.DateTime> TimeMin { get; set; }
public virtual string TimeMinRaw { get; private set; }

/// <summary>
/// <seealso cref="System.DateTime"/> representation of <see cref="TimeMinDateTimeOffset"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use TimeMinDateTimeOffset instead.")]
public virtual System.DateTime? TimeMin
{
get => Google.Apis.Util.Utilities.GetDateTimeFromString(TimeMinRaw);
set => TimeMinRaw = Google.Apis.Util.Utilities.GetStringFromDateTime(value);
}

/// <summary>
/// Time zone used in the response. Optional. The default is the time zone of the calendar.
Expand Down Expand Up @@ -2767,17 +2807,57 @@ public enum OrderByEnum
/// 2011-06-03T10:00:00-07:00, 2011-06-03T10:00:00Z. Milliseconds may be provided but are ignored. If
/// timeMin is set, timeMax must be greater than timeMin.
/// </summary>
public virtual System.DateTimeOffset? TimeMaxDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseDateTimeToDateTimeOffset(TimeMaxRaw);
set => TimeMaxRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToDateTime(value);
}

/// <summary>
/// String representation of <see cref="TimeMaxDateTimeOffset"/>, formatted for inclusion in the HTTP
/// request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("timeMax", Google.Apis.Util.RequestParameterType.Query)]
public virtual System.Nullable<System.DateTime> TimeMax { get; set; }
public virtual string TimeMaxRaw { get; private set; }

/// <summary>
/// <seealso cref="System.DateTime"/> representation of <see cref="TimeMaxDateTimeOffset"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use TimeMaxDateTimeOffset instead.")]
public virtual System.DateTime? TimeMax
{
get => Google.Apis.Util.Utilities.GetDateTimeFromString(TimeMaxRaw);
set => TimeMaxRaw = Google.Apis.Util.Utilities.GetStringFromDateTime(value);
}

/// <summary>
/// Lower bound (exclusive) for an event's end time to filter by. Optional. The default is not to filter by
/// end time. Must be an RFC3339 timestamp with mandatory time zone offset, for example,
/// 2011-06-03T10:00:00-07:00, 2011-06-03T10:00:00Z. Milliseconds may be provided but are ignored. If
/// timeMax is set, timeMin must be smaller than timeMax.
/// </summary>
public virtual System.DateTimeOffset? TimeMinDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseDateTimeToDateTimeOffset(TimeMinRaw);
set => TimeMinRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToDateTime(value);
}

/// <summary>
/// String representation of <see cref="TimeMinDateTimeOffset"/>, formatted for inclusion in the HTTP
/// request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("timeMin", Google.Apis.Util.RequestParameterType.Query)]
public virtual System.Nullable<System.DateTime> TimeMin { get; set; }
public virtual string TimeMinRaw { get; private set; }

/// <summary>
/// <seealso cref="System.DateTime"/> representation of <see cref="TimeMinDateTimeOffset"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use TimeMinDateTimeOffset instead.")]
public virtual System.DateTime? TimeMin
{
get => Google.Apis.Util.Utilities.GetDateTimeFromString(TimeMinRaw);
set => TimeMinRaw = Google.Apis.Util.Utilities.GetStringFromDateTime(value);
}

/// <summary>
/// Time zone used in the response. Optional. The default is the time zone of the calendar.
Expand All @@ -2790,8 +2870,28 @@ public enum OrderByEnum
/// entries deleted since this time will always be included regardless of showDeleted. Optional. The default
/// is not to filter by last modification time.
/// </summary>
public virtual System.DateTimeOffset? UpdatedMinDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseDateTimeToDateTimeOffset(UpdatedMinRaw);
set => UpdatedMinRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToDateTime(value);
}

/// <summary>
/// String representation of <see cref="UpdatedMinDateTimeOffset"/>, formatted for inclusion in the HTTP
/// request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("updatedMin", Google.Apis.Util.RequestParameterType.Query)]
public virtual System.Nullable<System.DateTime> UpdatedMin { get; set; }
public virtual string UpdatedMinRaw { get; private set; }

/// <summary>
/// <seealso cref="System.DateTime"/> representation of <see cref="UpdatedMinDateTimeOffset"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use UpdatedMinDateTimeOffset instead.")]
public virtual System.DateTime? UpdatedMin
{
get => Google.Apis.Util.Utilities.GetDateTimeFromString(UpdatedMinRaw);
set => UpdatedMinRaw = Google.Apis.Util.Utilities.GetStringFromDateTime(value);
}

/// <summary>Gets the method name.</summary>
public override string MethodName => "list";
Expand Down Expand Up @@ -3727,17 +3827,57 @@ public enum OrderByEnum
/// 2011-06-03T10:00:00-07:00, 2011-06-03T10:00:00Z. Milliseconds may be provided but are ignored. If
/// timeMin is set, timeMax must be greater than timeMin.
/// </summary>
public virtual System.DateTimeOffset? TimeMaxDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseDateTimeToDateTimeOffset(TimeMaxRaw);
set => TimeMaxRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToDateTime(value);
}

/// <summary>
/// String representation of <see cref="TimeMaxDateTimeOffset"/>, formatted for inclusion in the HTTP
/// request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("timeMax", Google.Apis.Util.RequestParameterType.Query)]
public virtual System.Nullable<System.DateTime> TimeMax { get; set; }
public virtual string TimeMaxRaw { get; private set; }

/// <summary>
/// <seealso cref="System.DateTime"/> representation of <see cref="TimeMaxDateTimeOffset"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use TimeMaxDateTimeOffset instead.")]
public virtual System.DateTime? TimeMax
{
get => Google.Apis.Util.Utilities.GetDateTimeFromString(TimeMaxRaw);
set => TimeMaxRaw = Google.Apis.Util.Utilities.GetStringFromDateTime(value);
}

/// <summary>
/// Lower bound (exclusive) for an event's end time to filter by. Optional. The default is not to filter by
/// end time. Must be an RFC3339 timestamp with mandatory time zone offset, for example,
/// 2011-06-03T10:00:00-07:00, 2011-06-03T10:00:00Z. Milliseconds may be provided but are ignored. If
/// timeMax is set, timeMin must be smaller than timeMax.
/// </summary>
public virtual System.DateTimeOffset? TimeMinDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseDateTimeToDateTimeOffset(TimeMinRaw);
set => TimeMinRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToDateTime(value);
}

/// <summary>
/// String representation of <see cref="TimeMinDateTimeOffset"/>, formatted for inclusion in the HTTP
/// request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("timeMin", Google.Apis.Util.RequestParameterType.Query)]
public virtual System.Nullable<System.DateTime> TimeMin { get; set; }
public virtual string TimeMinRaw { get; private set; }

/// <summary>
/// <seealso cref="System.DateTime"/> representation of <see cref="TimeMinDateTimeOffset"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use TimeMinDateTimeOffset instead.")]
public virtual System.DateTime? TimeMin
{
get => Google.Apis.Util.Utilities.GetDateTimeFromString(TimeMinRaw);
set => TimeMinRaw = Google.Apis.Util.Utilities.GetStringFromDateTime(value);
}

/// <summary>
/// Time zone used in the response. Optional. The default is the time zone of the calendar.
Expand All @@ -3750,8 +3890,28 @@ public enum OrderByEnum
/// entries deleted since this time will always be included regardless of showDeleted. Optional. The default
/// is not to filter by last modification time.
/// </summary>
public virtual System.DateTimeOffset? UpdatedMinDateTimeOffset
{
get => Google.Apis.Util.DiscoveryFormat.ParseDateTimeToDateTimeOffset(UpdatedMinRaw);
set => UpdatedMinRaw = Google.Apis.Util.DiscoveryFormat.FormatDateTimeOffsetToDateTime(value);
}

/// <summary>
/// String representation of <see cref="UpdatedMinDateTimeOffset"/>, formatted for inclusion in the HTTP
/// request.
/// </summary>
[Google.Apis.Util.RequestParameterAttribute("updatedMin", Google.Apis.Util.RequestParameterType.Query)]
public virtual System.Nullable<System.DateTime> UpdatedMin { get; set; }
public virtual string UpdatedMinRaw { get; private set; }

/// <summary>
/// <seealso cref="System.DateTime"/> representation of <see cref="UpdatedMinDateTimeOffset"/>.
/// </summary>
[System.ObsoleteAttribute("This property is obsolete and may behave unexpectedly; please use UpdatedMinDateTimeOffset instead.")]
public virtual System.DateTime? UpdatedMin
{
get => Google.Apis.Util.Utilities.GetDateTimeFromString(UpdatedMinRaw);
set => UpdatedMinRaw = Google.Apis.Util.Utilities.GetStringFromDateTime(value);
}

/// <summary>Gets or sets the body of this request.</summary>
Google.Apis.Calendar.v3.Data.Channel Body { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Google.Api.Generator.Rest/Models/DataPropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private IEnumerable<MemberDeclarationSyntax> GenerateDateTimeProperties(SourceFi
{
throw new ArgumentException("Unable to handle complex date-time properties");
}
// DateTime values generate two properties: one raw as a string, and one DateTime version.
// DateTime values generate three properties: one raw as a string, one DateTimeOffset version, and one (obsolete) DateTime version.
var rawProperty = AutoProperty(Modifier.Public | Modifier.Virtual, ctx.Type<string>(), PropertyName + "Raw", hasSetter: true)
.WithAttribute(ctx.Type<JsonPropertyAttribute>())(Name);
if (_schema.Description is object)
Expand Down
Loading

0 comments on commit 46435dd

Please sign in to comment.