Skip to content

Commit

Permalink
Tsp - Merge headers from different content types (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolauli authored Dec 25, 2024
1 parent 3ca9480 commit bcb7e88
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
32 changes: 21 additions & 11 deletions packages/typespec-powershell/src/convertor/convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,28 @@ function addResponses(psContext: SdkContext, op: HttpOperation, newOperation: Op
(<any>newResponse).schema = schema;
}
// Add headers
// we merge headers here, if the same header is defined in multiple responses, we only add it once.
// This is aligned with the behavior of typescript emitter and typespec-autorest emitter.
newResponse.protocol.http = newResponse.protocol.http ?? new Protocol();
if (response.responses[0].headers) {
for (const key in response.responses[0].headers) {
newResponse.protocol.http.headers = newResponse.protocol.http.headers || [];
const header = response.responses[0].headers[key];
const headerSchema = getSchemaForType(psContext, header.type);
const headerResponse = new HttpHeader(key, headerSchema);
headerResponse.language = new Languages();
headerResponse.language.default = new Language();
headerResponse.language.default.description = getDoc(psContext.program, header) || "";
headerResponse.language.default.name = pascalCase(deconstruct(key));
newResponse.protocol.http.headers.push(headerResponse);
const addedKeys: string[] = [];
for (const innerResponse of response.responses) {
if (innerResponse.headers) {
for (const key in innerResponse.headers) {
if (addedKeys.includes(key)) {
continue;
} else {
addedKeys.push(key);
}
newResponse.protocol.http.headers = newResponse.protocol.http.headers || [];
const header = innerResponse.headers[key];
const headerSchema = getSchemaForType(psContext, header.type);
const headerResponse = new HttpHeader(key, headerSchema);
headerResponse.language = new Languages();
headerResponse.language.default = new Language();
headerResponse.language.default.description = getDoc(psContext.program, header) || "";
headerResponse.language.default.name = pascalCase(deconstruct(key));
newResponse.protocol.http.headers.push(headerResponse);
}
}
}
newResponse.protocol.http.statusCodes = statusCode === "*" ? ["default"] : [statusCode];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ internal ProviderActionsOnboardAcceptedResponseHeaders(global::System.Collection
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).Location = (string) content.GetValueForProperty("Location",((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).Location, global::System.Convert.ToString);
}
if (content.Contains("RetryAfter"))
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).RetryAfter = (int?) content.GetValueForProperty("RetryAfter",((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).RetryAfter, (__y)=> (int) global::System.Convert.ChangeType(__y, typeof(int)));
}
AfterDeserializeDictionary(content);
}

Expand All @@ -136,6 +140,10 @@ internal ProviderActionsOnboardAcceptedResponseHeaders(global::System.Management
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).Location = (string) content.GetValueForProperty("Location",((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).Location, global::System.Convert.ToString);
}
if (content.Contains("RetryAfter"))
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).RetryAfter = (int?) content.GetValueForProperty("RetryAfter",((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).RetryAfter, (__y)=> (int) global::System.Convert.ChangeType(__y, typeof(int)));
}
AfterDeserializePSObject(content);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ public partial class ProviderActionsOnboardAcceptedResponseHeaders :
[Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Origin(Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.PropertyOrigin.Owned)]
public string Location { get => this._location; set => this._location = value; }

/// <summary>Backing field for <see cref="RetryAfter" /> property.</summary>
private int? _retryAfter;

[Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Origin(Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.PropertyOrigin.Owned)]
public int? RetryAfter { get => this._retryAfter; set => this._retryAfter = value; }

/// <param name="headers"></param>
void Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Runtime.IHeaderSerializable.ReadHeaders(global::System.Net.Http.Headers.HttpResponseHeaders headers)
{
if (headers.TryGetValues("Location", out var __locationHeader0))
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).Location = System.Linq.Enumerable.FirstOrDefault(__locationHeader0) is string __headerLocationHeader0 ? __headerLocationHeader0 : (string)null;
}
if (headers.TryGetValues("Retry-After", out var __retryAfterHeader1))
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsOnboardAcceptedResponseHeadersInternal)this).RetryAfter = System.Linq.Enumerable.FirstOrDefault(__retryAfterHeader1) is string __headerRetryAfterHeader1 ? int.TryParse( __headerRetryAfterHeader1, out int __headerRetryAfterHeader1Value ) ? __headerRetryAfterHeader1Value : default(int?) : default(int?);
}
}

/// <summary>
Expand All @@ -48,11 +58,24 @@ public partial interface IProviderActionsOnboardAcceptedResponseHeaders
PossibleTypes = new [] { typeof(string) })]
string Location { get; set; }

[Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Runtime.Info(
Required = false,
ReadOnly = false,
Read = true,
Create = true,
Update = true,
Description = @"",
SerializedName = @"Retry-After",
PossibleTypes = new [] { typeof(int) })]
int? RetryAfter { get; set; }

}
internal partial interface IProviderActionsOnboardAcceptedResponseHeadersInternal

{
string Location { get; set; }

int? RetryAfter { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ internal ProviderActionsTriggerEvaluationAcceptedResponseHeaders(global::System.
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).Location = (string) content.GetValueForProperty("Location",((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).Location, global::System.Convert.ToString);
}
if (content.Contains("RetryAfter"))
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).RetryAfter = (int?) content.GetValueForProperty("RetryAfter",((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).RetryAfter, (__y)=> (int) global::System.Convert.ChangeType(__y, typeof(int)));
}
AfterDeserializeDictionary(content);
}

Expand All @@ -136,6 +140,10 @@ internal ProviderActionsTriggerEvaluationAcceptedResponseHeaders(global::System.
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).Location = (string) content.GetValueForProperty("Location",((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).Location, global::System.Convert.ToString);
}
if (content.Contains("RetryAfter"))
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).RetryAfter = (int?) content.GetValueForProperty("RetryAfter",((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).RetryAfter, (__y)=> (int) global::System.Convert.ChangeType(__y, typeof(int)));
}
AfterDeserializePSObject(content);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ public partial class ProviderActionsTriggerEvaluationAcceptedResponseHeaders :
[Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Origin(Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.PropertyOrigin.Owned)]
public string Location { get => this._location; set => this._location = value; }

/// <summary>Backing field for <see cref="RetryAfter" /> property.</summary>
private int? _retryAfter;

[Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Origin(Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.PropertyOrigin.Owned)]
public int? RetryAfter { get => this._retryAfter; set => this._retryAfter = value; }

/// <param name="headers"></param>
void Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Runtime.IHeaderSerializable.ReadHeaders(global::System.Net.Http.Headers.HttpResponseHeaders headers)
{
if (headers.TryGetValues("Location", out var __locationHeader0))
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).Location = System.Linq.Enumerable.FirstOrDefault(__locationHeader0) is string __headerLocationHeader0 ? __headerLocationHeader0 : (string)null;
}
if (headers.TryGetValues("Retry-After", out var __retryAfterHeader1))
{
((Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Models.IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal)this).RetryAfter = System.Linq.Enumerable.FirstOrDefault(__retryAfterHeader1) is string __headerRetryAfterHeader1 ? int.TryParse( __headerRetryAfterHeader1, out int __headerRetryAfterHeader1Value ) ? __headerRetryAfterHeader1Value : default(int?) : default(int?);
}
}

/// <summary>
Expand All @@ -48,11 +58,24 @@ public partial interface IProviderActionsTriggerEvaluationAcceptedResponseHeader
PossibleTypes = new [] { typeof(string) })]
string Location { get; set; }

[Microsoft.Azure.PowerShell.Cmdlets.AppComplianceAutomation.Runtime.Info(
Required = false,
ReadOnly = false,
Read = true,
Create = true,
Update = true,
Description = @"",
SerializedName = @"Retry-After",
PossibleTypes = new [] { typeof(int) })]
int? RetryAfter { get; set; }

}
internal partial interface IProviderActionsTriggerEvaluationAcceptedResponseHeadersInternal

{
string Location { get; set; }

int? RetryAfter { get; set; }

}
}

0 comments on commit bcb7e88

Please sign in to comment.