Skip to content

Commit e12301c

Browse files
committed
Add test for sample document with 3.1 features
1 parent 5af79d4 commit e12301c

File tree

3 files changed

+247
-0
lines changed

3 files changed

+247
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
openapi: '3.1.0'
2+
jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema
3+
info:
4+
title: Sample OpenAPI 3.1 API
5+
description: A sample API demonstrating OpenAPI 3.1 features
6+
license:
7+
name: Apache 2.0
8+
url: https://www.apache.org/licenses/LICENSE-2.0.html
9+
identifier: Apache-2.0
10+
version: 2.0.0
11+
summary: Sample OpenAPI 3.1 API with the latest features
12+
servers:
13+
- url: https://api.example.com/v2
14+
description: Main production server
15+
paths:
16+
/pets:
17+
get:
18+
tags:
19+
- pets
20+
summary: List all pets
21+
operationId: listPets
22+
parameters:
23+
- name: limit
24+
in: query
25+
description: How many items to return at one time (max 100)
26+
schema:
27+
exclusiveMaximum: 100
28+
exclusiveMinimum: 1
29+
type: integer
30+
responses:
31+
'200':
32+
description: A paged array of pets
33+
content:
34+
application/json:
35+
schema:
36+
$ref: https://example.com/schemas/pet.json
37+
/sample:
38+
get:
39+
summary: Sample endpoint
40+
responses:
41+
'200':
42+
description: Sample response
43+
content:
44+
application/json:
45+
schema:
46+
$id: https://example.com/schemas/person.schema.yaml
47+
$schema: https://json-schema.org/draft/2020-12/schema
48+
$comment: A schema defining a pet object with optional references to dynamic components.
49+
$vocabulary:
50+
https://json-schema.org/draft/2020-12/vocab/core: true
51+
https://json-schema.org/draft/2020-12/vocab/applicator: true
52+
https://json-schema.org/draft/2020-12/vocab/validation: true
53+
https://json-schema.org/draft/2020-12/vocab/meta-data: false
54+
https://json-schema.org/draft/2020-12/vocab/format-annotation: false
55+
$dynamicAnchor: addressDef
56+
title: Pet
57+
required:
58+
- name
59+
type: object
60+
properties:
61+
name:
62+
$comment: The pet's full name
63+
type: string
64+
address:
65+
$comment: Reference to an address definition which can change dynamically
66+
$dynamicRef: '#addressDef'
67+
description: Schema for a pet object
68+
components:
69+
schemas:
70+
Pet:
71+
$id: https://example.com/schemas/pet.json
72+
$comment: This schema represents a pet in the system.
73+
$defs:
74+
ExtraInfo:
75+
type: string
76+
required:
77+
- id
78+
- weight
79+
type: object
80+
properties:
81+
id:
82+
type: string
83+
format: uuid
84+
weight:
85+
exclusiveMinimum: 0
86+
type: number
87+
description: Weight of the pet in kilograms
88+
attributes:
89+
patternProperties:
90+
'^attr_[A-Za-z]+$':
91+
type: string
92+
type:
93+
- 'null'
94+
- object
95+
description: Dynamic attributes for the pet
96+
security:
97+
- api_key: [ ]
98+
webhooks:
99+
newPetAlert:
100+
post:
101+
summary: Notify about a new pet being added
102+
requestBody:
103+
content:
104+
application/json:
105+
schema:
106+
type: string
107+
required: true
108+
responses:
109+
'200':
110+
description: Webhook processed successfully

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.OpenApi.Services;
1313
using Xunit;
1414
using System.Linq;
15+
using VerifyXunit;
1516

1617
namespace Microsoft.OpenApi.Readers.Tests.V31Tests
1718
{
@@ -530,5 +531,19 @@ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks()
530531
// Assert
531532
requestBodySchema.Properties.Count.Should().Be(2); // reference has been resolved
532533
}
534+
535+
[Fact]
536+
public async Task ParseDocumentWith31PropertiesWorks()
537+
{
538+
var path = Path.Combine(SampleFolderPath, "documentWith31Properties.yaml");
539+
var doc = OpenApiDocument.Load(path).OpenApiDocument;
540+
var outputStringWriter = new StringWriter();
541+
doc.SerializeAsV31(new OpenApiYamlWriter(outputStringWriter));
542+
outputStringWriter.Flush();
543+
var actual = outputStringWriter.GetStringBuilder().ToString();
544+
545+
// Assert
546+
await Verifier.Verify(actual);
547+
}
533548
}
534549
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Sample OpenAPI 3.1 API
4+
description: A sample API demonstrating OpenAPI 3.1 features
5+
version: "2.0.0"
6+
summary: Sample OpenAPI 3.1 API with the latest features # OpenAPI 3.1 feature
7+
license:
8+
name: Apache 2.0
9+
identifier: Apache-2.0 # SPDX license identifier, a new 3.1 feature to define an API's SPDX license expression
10+
url: https://www.apache.org/licenses/LICENSE-2.0.html
11+
12+
# JSON Schema 2020-12 feature
13+
jsonSchemaDialect: "https://json-schema.org/draft/2020-12/schema"
14+
15+
servers:
16+
- url: https://api.example.com/v2
17+
description: Main production server
18+
19+
# Example Webhooks (OpenAPI 3.1 feature)
20+
webhooks:
21+
newPetAlert:
22+
post:
23+
summary: Notify about a new pet being added
24+
requestBody:
25+
required: true
26+
content:
27+
application/json:
28+
schema:
29+
type: string
30+
responses:
31+
'200':
32+
description: Webhook processed successfully
33+
paths:
34+
/pets:
35+
get:
36+
summary: List all pets
37+
operationId: listPets
38+
tags:
39+
- pets
40+
parameters:
41+
- name: limit
42+
in: query
43+
description: How many items to return at one time (max 100)
44+
required: false
45+
schema:
46+
type: integer
47+
#exclusiveMinimum and exclusiveMaximum now represent distinct values
48+
exclusiveMinimum: 1
49+
exclusiveMaximum: 100
50+
responses:
51+
'200':
52+
description: A paged array of pets
53+
content:
54+
application/json:
55+
schema:
56+
# 3.1 feature where we can reference schemas using their identifier
57+
$ref: 'https://example.com/schemas/pet.json'
58+
/sample:
59+
get:
60+
summary: Sample endpoint
61+
responses:
62+
'200':
63+
description: Sample response
64+
content:
65+
application/json:
66+
schema:
67+
#JSON schema keywords
68+
$schema: "https://json-schema.org/draft/2020-12/schema"
69+
$id: "https://example.com/schemas/person.schema.yaml"
70+
$comment: "A schema defining a pet object with optional references to dynamic components."
71+
$vocabulary:
72+
"https://json-schema.org/draft/2020-12/vocab/core": true
73+
"https://json-schema.org/draft/2020-12/vocab/applicator": true
74+
"https://json-schema.org/draft/2020-12/vocab/validation": true
75+
"https://json-schema.org/draft/2020-12/vocab/meta-data": false
76+
"https://json-schema.org/draft/2020-12/vocab/format-annotation": false
77+
78+
title: "Pet"
79+
description: "Schema for a pet object"
80+
type: "object"
81+
properties:
82+
name:
83+
type: "string"
84+
$comment: "The pet's full name"
85+
address:
86+
$dynamicRef: "#addressDef"
87+
$comment: "Reference to an address definition which can change dynamically"
88+
required:
89+
- name
90+
$dynamicAnchor: "addressDef"
91+
components:
92+
schemas:
93+
Pet:
94+
$id: 'https://example.com/schemas/pet.json'
95+
type: object
96+
required:
97+
- id
98+
- weight
99+
properties:
100+
id:
101+
type: string
102+
format: uuid
103+
weight:
104+
type: number
105+
exclusiveMinimum: 0
106+
description: Weight of the pet in kilograms
107+
# Pattern properties and Type array feature from JSON Schema
108+
attributes:
109+
type:
110+
- "object"
111+
- "null"
112+
description: Dynamic attributes for the pet
113+
patternProperties:
114+
"^attr_[A-Za-z]+$":
115+
type: string
116+
$comment: "This schema represents a pet in the system." # JSON Schema 2020-12 feature
117+
$defs: # JSON Schema 2020-12 feature
118+
ExtraInfo:
119+
type: string
120+
121+
security:
122+
- api_key: []

0 commit comments

Comments
 (0)