Description
The Microsoft.OpenApi library allows setting Description on OpenApiSchemaReference, which results in serialized output containing both $ref and description.
However, according to OpenAPI 3.0.x specification:
When a $ref is used, all other properties SHALL be ignored.
This makes $ref effectively a replacement, meaning sibling keywords like description should not appear.
Example
Code
usingSystem;usingSystem.IO;usingMicrosoft.OpenApi.Models;usingMicrosoft.OpenApi.Writers;classProgram{staticvoidMain(){vardocument=newOpenApiDocument{Info=newOpenApiInfo{Title="Ref Description Test",Version="1.0.0"},Components=newOpenApiComponents{Schemas={["Pet"]=newOpenApiSchema{Type="object",Properties={["name"]=newOpenApiSchema{Type="string"}}}}},Paths=newOpenApiPaths{["/test"]=newOpenApiPathItem{Operations={[OperationType.Get]=newOpenApiOperation{Responses={["200"]=newOpenApiResponse{Description="OK",Content={["application/json"]=newOpenApiMediaType{Schema=newOpenApiSchemaReference("Pet"){Description="Local description"}}}}}}}}}};usingvarstream=newMemoryStream();usingvarwriter=newOpenApiJsonWriter(newStreamWriter(stream));document.SerializeAsV3(writer);writer.Flush();stream.Position=0;Console.WriteLine(newStreamReader(stream).ReadToEnd());}}Output
$ref: '#/components/schemas/Pet'description: Local description
Expected behavior
One of:
- Serializer wraps in
allOf
allOf:
- $ref: '#/components/schemas/Pet'description: Local description
Description ignored when targeting OAS 3.0
Validation warning
Notes
Question
What is the intended behavior when serializing references with annotations for OAS 3.0 vs 3.1?
Description
The Microsoft.OpenApi library allows setting
DescriptiononOpenApiSchemaReference, which results in serialized output containing both$refanddescription.However, according to OpenAPI 3.0.x specification:
This makes
$refeffectively a replacement, meaning sibling keywords likedescriptionshould not appear.Example
Code
Output
Expected behavior
One of:
allOfDescription ignored when targeting OAS 3.0
Validation warning
Notes
In OAS 3.1 sibling keywords are allowed due to JSON Schema alignment
Behavior difference between 3.0 and 3.1 is currently unclear
This can lead to portability issues across tooling
This is a clone of OpenApiSchemaReference allows Description alongside $ref producing non-spec-compliant output in OAS 3.0 #2745
Question
What is the intended behavior when serializing references with annotations for OAS 3.0 vs 3.1?