diff --git a/packages/apidom-ls/test/fixtures/syntax/sample-api-default-operation.yaml b/packages/apidom-ls/test/fixtures/syntax/sample-api-default-operation.yaml new file mode 100644 index 0000000000..7888cb81c6 --- /dev/null +++ b/packages/apidom-ls/test/fixtures/syntax/sample-api-default-operation.yaml @@ -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 diff --git a/packages/apidom-ls/test/openapi-yaml.ts b/packages/apidom-ls/test/openapi-yaml.spec.ts similarity index 94% rename from packages/apidom-ls/test/openapi-yaml.ts rename to packages/apidom-ls/test/openapi-yaml.spec.ts index a4f68efaae..8e002e050d 100644 --- a/packages/apidom-ls/test/openapi-yaml.ts +++ b/packages/apidom-ls/test/openapi-yaml.spec.ts @@ -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(); @@ -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( diff --git a/packages/apidom-ns-openapi-3-0/src/refractor/specification.ts b/packages/apidom-ns-openapi-3-0/src/refractor/specification.ts index c847cb7585..a3f4c870ab 100644 --- a/packages/apidom-ns-openapi-3-0/src/refractor/specification.ts +++ b/packages/apidom-ns-openapi-3-0/src/refractor/specification.ts @@ -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'; @@ -299,9 +298,7 @@ const specification = { }, Responses: { $visitor: ResponsesVisitor, - fixedFields: { - default: ResponsesDefaultVisitor, - }, + fixedFields: {}, }, Response: { $visitor: ResponseVisitor, diff --git a/packages/apidom-ns-openapi-3-0/src/refractor/visitors/open-api-3-0/responses/index.ts b/packages/apidom-ns-openapi-3-0/src/refractor/visitors/open-api-3-0/responses/index.ts index eba37ebe42..e3df3a227d 100644 --- a/packages/apidom-ns-openapi-3-0/src/refractor/visitors/open-api-3-0/responses/index.ts +++ b/packages/apidom-ns-openapi-3-0/src/refractor/visitors/open-api-3-0/responses/index.ts @@ -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) { diff --git a/packages/apidom-ns-openapi-3-0/test/refractor/__snapshots__/index.mjs.snap b/packages/apidom-ns-openapi-3-0/test/refractor/__snapshots__/index.mjs.snap index 1a3ceaf530..d3fb951926 100644 --- a/packages/apidom-ns-openapi-3-0/test/refractor/__snapshots__/index.mjs.snap +++ b/packages/apidom-ns-openapi-3-0/test/refractor/__snapshots__/index.mjs.snap @@ -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" } ] } @@ -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" - } - } - } - ] - } - } } ] } @@ -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" } ] } diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-0/test/__snapshots__/adapter.mjs.snap b/packages/apidom-parser-adapter-openapi-yaml-3-0/test/__snapshots__/adapter.mjs.snap index 2a93fe8430..4e10931491 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-0/test/__snapshots__/adapter.mjs.snap +++ b/packages/apidom-parser-adapter-openapi-yaml-3-0/test/__snapshots__/adapter.mjs.snap @@ -230,6 +230,12 @@ exports[`adapter should parse 1`] = ` (MemberElement (StringElement) (ResponsesElement + (MemberElement + (StringElement) + (ReferenceElement + (MemberElement + (StringElement) + (StringElement)))) (MemberElement (StringElement) (ResponseElement @@ -274,13 +280,7 @@ exports[`adapter should parse 1`] = ` (ReferenceElement (MemberElement (StringElement) - (StringElement)))))))) - (MemberElement - (StringElement) - (ReferenceElement - (MemberElement - (StringElement) - (StringElement)))))) + (StringElement)))))))))) (MemberElement (StringElement) (ObjectElement