in src/Microsoft.OpenApi/Models/OpenApiComponents.cs [74:274]
public void SerializeAsV3(IOpenApiWriter writer)
{
if (writer == null)
{
throw Error.ArgumentNull(nameof(writer));
}
// If references have been inlined we don't need the to render the components section
// however if they have cycles, then we will need a component rendered
if (writer.GetSettings().ReferenceInline != ReferenceInlineSetting.DoNotInlineReferences)
{
var loops = writer.GetSettings().LoopDetector.Loops;
writer.WriteStartObject();
if (loops.TryGetValue(typeof(OpenApiSchema), out List<object> schemas))
{
var openApiSchemas = schemas.Cast<OpenApiSchema>().Distinct().ToList()
.ToDictionary<OpenApiSchema, string>(k => k.Reference.Id);
writer.WriteOptionalMap(
OpenApiConstants.Schemas,
Schemas,
(w, key, component) => {
component.SerializeAsV3WithoutReference(w);
});
}
writer.WriteEndObject();
return;
}
writer.WriteStartObject();
// Serialize each referenceable object as full object without reference if the reference in the object points to itself.
// If the reference exists but points to other objects, the object is serialized to just that reference.
// schemas
writer.WriteOptionalMap(
OpenApiConstants.Schemas,
Schemas,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.Schema &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// responses
writer.WriteOptionalMap(
OpenApiConstants.Responses,
Responses,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.Response &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// parameters
writer.WriteOptionalMap(
OpenApiConstants.Parameters,
Parameters,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.Parameter &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// examples
writer.WriteOptionalMap(
OpenApiConstants.Examples,
Examples,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.Example &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// requestBodies
writer.WriteOptionalMap(
OpenApiConstants.RequestBodies,
RequestBodies,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.RequestBody &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// headers
writer.WriteOptionalMap(
OpenApiConstants.Headers,
Headers,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.Header &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// securitySchemes
writer.WriteOptionalMap(
OpenApiConstants.SecuritySchemes,
SecuritySchemes,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.SecurityScheme &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// links
writer.WriteOptionalMap(
OpenApiConstants.Links,
Links,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.Link &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// callbacks
writer.WriteOptionalMap(
OpenApiConstants.Callbacks,
Callbacks,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.Callback &&
component.Reference.Id == key)
{
component.SerializeAsV3WithoutReference(w);
}
else
{
component.SerializeAsV3(w);
}
});
// extensions
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi3_0);
writer.WriteEndObject();
}