Skip to content
Merged
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
12 changes: 8 additions & 4 deletions src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
{
return null; // Body Parameters shouldn't be converted to Parameters
}
entry.value.Reference = new OpenApiReference()
// If the component isn't a reference to another component, then point it to itself.
if (entry.value.Reference == null)
{
Type = referenceType,
Id = entry.key
};
entry.value.Reference = new OpenApiReference()
{
Type = referenceType,
Id = entry.key
};
}
return entry;
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\src\Microsoft.OpenApi.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<None Remove="V2Tests\Samples\ComponentRootReference.json" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="OpenApiReaderTests\Samples\unsupported.v1.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
Expand All @@ -25,6 +28,9 @@
<EmbeddedResource Include="V2Tests\Samples\basic.v3.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V2Tests\Samples\ComponentRootReference.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V2Tests\Samples\minimal.v2.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,5 +419,24 @@ public void ShouldAssignSchemaToAllResponses()
xml.Schema.ShouldBeEquivalentTo(targetSchema);
}
}


[Fact]
public void ShouldAllowComponentsThatJustContainAReference()
{
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "ComponentRootReference.json")))
{
OpenApiStreamReader reader = new OpenApiStreamReader();
OpenApiDocument doc = reader.Read(stream, out OpenApiDiagnostic diags);
OpenApiSchema schema1 = doc.Components.Schemas["AllPets"];
Assert.False(schema1.UnresolvedReference);
OpenApiSchema schema2 = (OpenApiSchema)doc.ResolveReference(schema1.Reference);
if (schema2.UnresolvedReference && schema1.Reference.Id == schema2.Reference.Id)
{
// detected a cycle - this code gets triggered
Assert.True(false, "A cycle should not be detected");
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"name": "MIT"
}
},
"servers": [
{
"url": "http://petstore.swagger.io/v1"
}
],
"paths": {
"/pets": {
"get": {
"summary": "List all pets",
"operationId": "listPets",
"tags": [
"pets"
],
"parameters": [
{
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "A paged array of pets",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AllPets"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"post": {
"summary": "Create a pet",
"operationId": "createPets",
"tags": [
"pets"
],
"responses": {
"201": {
"description": "Null response"
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
},
"/pets/{petId}": {
"get": {
"summary": "Info for a specific pet",
"operationId": "showPetById",
"tags": [
"pets"
],
"parameters": [
{
"name": "petId",
"in": "path",
"required": true,
"description": "The id of the pet to retrieve",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Expected response to a valid request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pets"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Pets": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
},
"AllPets": {
"$ref": "#/components/schemas/Pets"
},
"Error": {
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
}