Skip to content

Commit e40464d

Browse files
committed
Suppress format warnings for response headers
1 parent e88c9cb commit e40464d

5 files changed

Lines changed: 118 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
101101

102102
### Changed
103103

104+
- Fixed unsupported type-format warnings for response header schemas, which are always emitted as strings. [#4227](https://github.com/microsoft/kiota/issues/4227)
104105
- C#, Java, Go, PHP, Dart, TypeScript, Python and Ruby client: default value initialization in model classes for DateTime/Date/Time/UUID properties did not compile [#7404](https://github.com/microsoft/kiota/issues/7404)
105106
- All languages: default value initialization in model classes for numeric/boolean properties was missing [#7404](https://github.com/microsoft/kiota/issues/7404)
106107
- Fixed a bug where required query parameters from one HTTP operation were leaking into the path-item-level URL template, making them appear required for sibling operations on the same path. [#7292](https://github.com/microsoft/kiota/issues/7292)

src/Kiota.Builder/Validation/InconsistentTypeFormatPair.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public class InconsistentTypeFormatPair : ValidationRule<IOpenApiSchema>
4848
];
4949
public InconsistentTypeFormatPair() : base(nameof(InconsistentTypeFormatPair), static (context, schema) =>
5050
{
51-
if (schema is null || !schema.Type.HasValue || string.IsNullOrEmpty(schema.Format) || KnownAndNotSupportedFormats.knownAndUnsupportedFormats.Contains(schema.Format) || escapedTypes.Contains(schema.Type.Value))
51+
if (KnownAndNotSupportedFormats.IsHeaderSchema(context.PathString) ||
52+
schema is null || !schema.Type.HasValue || string.IsNullOrEmpty(schema.Format) || KnownAndNotSupportedFormats.knownAndUnsupportedFormats.Contains(schema.Format) || escapedTypes.Contains(schema.Type.Value))
5253
return;
5354
var sanitizedType = schema.Type.Value & ~JsonSchemaType.Null;
5455
if (!validPairs.TryGetValue(sanitizedType, out var validFormats) || !validFormats.Contains(schema.Format))

src/Kiota.Builder/Validation/KnownAndNotSupportedFormats.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,28 @@ public class KnownAndNotSupportedFormats : ValidationRule<IOpenApiSchema>
2525
};
2626
public KnownAndNotSupportedFormats() : base(nameof(KnownAndNotSupportedFormats), static (context, schema) =>
2727
{
28-
if (!string.IsNullOrEmpty(schema.Format) && knownAndUnsupportedFormats.Contains(schema.Format))
28+
if (!IsHeaderSchema(context.PathString) &&
29+
!string.IsNullOrEmpty(schema.Format) && knownAndUnsupportedFormats.Contains(schema.Format))
2930
context.CreateWarning(nameof(KnownAndNotSupportedFormats), $"The format {schema.Format} is not supported by Kiota and the string type will be used.");
3031
})
3132
{
3233
}
34+
35+
internal static bool IsHeaderSchema(string? path)
36+
{
37+
if (string.IsNullOrEmpty(path))
38+
return false;
39+
var pathSegments = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
40+
for (var i = 0; i < pathSegments.Length - 1; i++)
41+
{
42+
if (pathSegments[i].Equals("components", StringComparison.OrdinalIgnoreCase) &&
43+
pathSegments[i + 1].Equals("headers", StringComparison.OrdinalIgnoreCase))
44+
return true;
45+
if (pathSegments[i].Equals("responses", StringComparison.OrdinalIgnoreCase) &&
46+
i + 2 < pathSegments.Length &&
47+
pathSegments[i + 2].Equals("headers", StringComparison.OrdinalIgnoreCase))
48+
return true;
49+
}
50+
return false;
51+
}
3352
}

tests/Kiota.Builder.Tests/Validation/InconsistentTypeFormatPairTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ public async Task DoesntAddWarningOnNullable()
9898
type: string
9999
format: binary
100100
nullable: true
101+
""";
102+
var diagnostic = await GetDiagnosticFromDocumentAsync(documentTxt);
103+
Assert.Empty(diagnostic.Warnings);
104+
}
105+
[Fact]
106+
public async Task DoesntAddAWarningForResponseHeaders()
107+
{
108+
var documentTxt =
109+
"""
110+
openapi: 3.0.1
111+
info:
112+
title: Sample API
113+
version: 1.0.0
114+
paths:
115+
/items:
116+
post:
117+
responses:
118+
'201':
119+
description: Created
120+
headers:
121+
Location:
122+
schema:
123+
type: string
124+
format: int32
101125
""";
102126
var diagnostic = await GetDiagnosticFromDocumentAsync(documentTxt);
103127
Assert.Empty(diagnostic.Warnings);

tests/Kiota.Builder.Tests/Validation/KnownAndNotSupportedFormatsTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,77 @@ public async Task DoesntFailWhenNoFormat()
7575
var diagnostic = await GetDiagnosticFromDocumentAsync(documentTxt);
7676
Assert.Empty(diagnostic.Warnings);
7777
}
78+
[Fact]
79+
public async Task DoesntAddAWarningForResponseHeaders()
80+
{
81+
var documentTxt =
82+
"""
83+
openapi: 3.0.1
84+
info:
85+
title: Sample API
86+
version: 1.0.0
87+
paths:
88+
/items:
89+
post:
90+
responses:
91+
'201':
92+
description: Created
93+
headers:
94+
Location:
95+
schema:
96+
type: string
97+
format: uri
98+
""";
99+
var diagnostic = await GetDiagnosticFromDocumentAsync(documentTxt);
100+
Assert.Empty(diagnostic.Warnings);
101+
}
102+
[Fact]
103+
public async Task DoesntAddAWarningForComponentHeaders()
104+
{
105+
var documentTxt =
106+
"""
107+
openapi: 3.0.1
108+
info:
109+
title: Sample API
110+
version: 1.0.0
111+
paths: {}
112+
components:
113+
headers:
114+
Location:
115+
schema:
116+
type: string
117+
format: uri
118+
""";
119+
var diagnostic = await GetDiagnosticFromDocumentAsync(documentTxt);
120+
Assert.Empty(diagnostic.Warnings);
121+
}
122+
[Fact]
123+
public async Task AddsAWarningForSchemaPropertyNamedHeaders()
124+
{
125+
var documentTxt =
126+
"""
127+
openapi: 3.0.1
128+
info:
129+
title: Sample API
130+
version: 1.0.0
131+
paths:
132+
/items:
133+
get:
134+
responses:
135+
'200':
136+
description: Success
137+
content:
138+
application/json:
139+
schema:
140+
type: object
141+
properties:
142+
headers:
143+
type: string
144+
format: uri
145+
""";
146+
var diagnostic = await GetDiagnosticFromDocumentAsync(documentTxt);
147+
Assert.Single(diagnostic.Warnings);
148+
}
78149
private static async Task<OpenApiDiagnostic> GetDiagnosticFromDocumentAsync(string document)
79150
{
80151
var rule = new KnownAndNotSupportedFormats();

0 commit comments

Comments
 (0)