Skip to content

fix(apidom-ns-openapi-3-0): semantic highlighting for responses #4970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: 3.0.4
info:
title: Swagger Petstore - OpenAPI 3.0
description: |-
This is a sample Pet Store Server based on the OpenAPI 3.0 specification.
termsOfService: https://swagger.io/terms/
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.12
externalDocs:
description: Find out more about Swagger
url: https://swagger.io
servers:
- url: https://petstore3.swagger.io/api/v3
paths:
/pet/findByStatus:
get:
operationId: findPetsByStatus
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
application/xml:
schema:
type: array
'400':
description: Invalid status value
default:
description: Unexpected error
content:
application/json:
schema:
type: array
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ const specHighlightNoQuotes = fs
.readFileSync(path.join(__dirname, 'fixtures', 'syntax/sample-api-noquotes.yaml'))
.toString();

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const specHighlightDefaultOperation = fs
.readFileSync(path.join(__dirname, 'fixtures', 'syntax/sample-api-default-operation.yaml'))
.toString();

const specInvalidYaml = fs
.readFileSync(path.join(__dirname, 'fixtures', 'validation', 'oas', 'invalidyaml.yaml'))
.toString();
Expand Down Expand Up @@ -795,6 +800,51 @@ describe('apidom-ls-yaml', function () {
});
});

it('test semantic highlighting for operation default response', async function () {
// valid spec
const doc: TextDocument = TextDocument.create(
'foo://bar/specHighlightDefaultOperation.json',
'json',
0,
specHighlightDefaultOperation,
);

const tokens = await languageService.computeSemanticTokens(doc);

if (tokens.data && tokens.data.length >= 5) {
const logBase = (n: number) => Math.log(n) / Math.log(2);
for (let i = 0; i < tokens.data.length; i += 5) {
// eslint-disable-next-line no-console
console.log(
`[${tokens.data[i]}, ${tokens.data[i + 1]}, ${tokens.data[i + 2]}, ${
tokens.data[i + 3]
}, ${tokens.data[i + 4]}] type: ${
languageService.getSemanticTokensLegend().tokenTypes[tokens.data[i + 3]]
}, mod: ${
languageService.getSemanticTokensLegend().tokenModifiers[logBase(tokens.data[i + 4])]
} / semTok: +line: ${tokens.data[i]}, off: ${tokens.data[i + 1]}, len: ${
tokens.data[i + 2]
}`,
);
}
}

assert.deepEqual(tokens, {
data: [
0, 0, 7, 16, 0, 0, 0, 7, 2, 0, 1, 0, 4, 4, 0, 1, 2, 5, 35, 64, 0, 7, 30, 32, 64, 1, 2, 11,
35, 64, 0, 13, 80, 32, 64, 2, 2, 14, 35, 64, 0, 16, 25, 32, 64, 1, 2, 7, 35, 64, 1, 4, 4,
35, 64, 0, 6, 10, 32, 64, 1, 4, 3, 35, 64, 0, 5, 48, 32, 64, 1, 2, 7, 1, 0, 1, 0, 12, 35,
64, 1, 2, 11, 35, 64, 0, 13, 27, 32, 64, 1, 2, 3, 35, 64, 0, 5, 18, 32, 64, 1, 0, 7, 25, 0,
1, 4, 3, 11, 0, 1, 0, 5, 18, 0, 1, 2, 17, 6, 0, 1, 4, 3, 5, 16, 1, 6, 11, 35, 64, 0, 13, 16,
32, 64, 1, 6, 9, 22, 0, 1, 8, 5, 21, 0, 1, 10, 11, 35, 64, 0, 13, 20, 32, 64, 1, 10, 7, 14,
0, 1, 12, 16, 15, 0, 1, 14, 6, 23, 0, 1, 16, 4, 35, 64, 0, 6, 5, 32, 64, 1, 12, 15, 15, 0,
1, 14, 6, 23, 0, 1, 16, 4, 35, 64, 0, 6, 5, 32, 64, 1, 8, 5, 21, 0, 1, 10, 11, 35, 64, 0,
13, 20, 32, 64, 1, 8, 7, 21, 0, 1, 10, 11, 35, 64, 0, 13, 16, 32, 64, 1, 10, 7, 14, 0, 1,
12, 16, 15, 0, 1, 14, 6, 23, 0, 1, 16, 4, 35, 64, 0, 6, 5, 32, 64,
],
});
});

it('test hover', async function () {
// valid spec
const doc: TextDocument = TextDocument.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import ResponseHeadersVisitor from './visitors/open-api-3-0/response/HeadersVisi
import ResponseContentVisitor from './visitors/open-api-3-0/response/ContentVisitor.ts';
import ResponseLinksVisitor from './visitors/open-api-3-0/response/LinksVisitor.ts';
import ResponsesVisitor from './visitors/open-api-3-0/responses/index.ts';
import ResponsesDefaultVisitor from './visitors/open-api-3-0/responses/DefaultVisitor.ts';
import OperationVisitor from './visitors/open-api-3-0/operation/index.ts';
import OperationTagsVisitor from './visitors/open-api-3-0/operation/TagsVisitor.ts';
import OperationParametersVisitor from './visitors/open-api-3-0/operation/ParametersVisitor.ts';
Expand Down Expand Up @@ -299,9 +298,7 @@ const specification = {
},
Responses: {
$visitor: ResponsesVisitor,
fixedFields: {
default: ResponsesDefaultVisitor,
},
fixedFields: {},
},
Response: {
$visitor: ResponseVisitor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class ResponsesVisitor extends Mixin(MixedFieldsVisitor, FallbackVisitor) {
? ['document', 'objects', 'Reference']
: ['document', 'objects', 'Response'];
this.fieldPatternPredicate = (value) =>
new RegExp(`^(1XX|2XX|3XX|4XX|5XX|${range(100, 600).join('|')})$`).test(String(value));
new RegExp(`^(1XX|2XX|3XX|4XX|5XX|default|${range(100, 600).join('|')})$`).test(
String(value),
);
}

ObjectElement(objectElement: ObjectElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,86 @@ exports[`refractor given generic ApiDOM object in OpenApi 3.0.4 shape should ref
"content": [
{
"element": "string",
"content": "fixed-field"
"content": "patterned-field"
}
]
}
},
"content": {
"key": {
"element": "string",
"content": "201"
},
"value": {
"element": "reference",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "openapi-reference"
},
{
"element": "string",
"content": "reference-element"
}
]
},
"referenced-element": {
"element": "string",
"content": "response"
}
},
"content": [
{
"element": "member",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "fixed-field"
}
]
}
},
"content": {
"key": {
"element": "string",
"content": "$ref"
},
"value": {
"element": "string",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "reference-value"
}
]
}
},
"content": "#/components/responses/201"
}
}
}
]
}
}
},
{
"element": "member",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "patterned-field"
}
]
}
Expand Down Expand Up @@ -2649,85 +2728,6 @@ exports[`refractor given generic ApiDOM object in OpenApi 3.0.4 shape should ref
]
}
}
},
{
"element": "member",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "patterned-field"
}
]
}
},
"content": {
"key": {
"element": "string",
"content": "201"
},
"value": {
"element": "reference",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "openapi-reference"
},
{
"element": "string",
"content": "reference-element"
}
]
},
"referenced-element": {
"element": "string",
"content": "response"
}
},
"content": [
{
"element": "member",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "fixed-field"
}
]
}
},
"content": {
"key": {
"element": "string",
"content": "$ref"
},
"value": {
"element": "string",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "reference-value"
}
]
}
},
"content": "#/components/responses/201"
}
}
}
]
}
}
}
]
}
Expand Down Expand Up @@ -3697,7 +3697,7 @@ exports[`refractor given generic ApiDOM object in OpenApi 3.0.4 shape should ref
"content": [
{
"element": "string",
"content": "fixed-field"
"content": "patterned-field"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ exports[`adapter should parse 1`] = `
(MemberElement
(StringElement)
(ResponsesElement
(MemberElement
(StringElement)
(ReferenceElement
(MemberElement
(StringElement)
(StringElement))))
(MemberElement
(StringElement)
(ResponseElement
Expand Down Expand Up @@ -274,13 +280,7 @@ exports[`adapter should parse 1`] = `
(ReferenceElement
(MemberElement
(StringElement)
(StringElement))))))))
(MemberElement
(StringElement)
(ReferenceElement
(MemberElement
(StringElement)
(StringElement))))))
(StringElement))))))))))
(MemberElement
(StringElement)
(ObjectElement
Expand Down
Loading