From bf0d7e4261d93f741068f5efc116bcbe8705e8d2 Mon Sep 17 00:00:00 2001 From: thim81 Date: Fri, 28 Jan 2022 10:56:36 +0100 Subject: [PATCH] Fix unwanted sorting components (#11) * Fix for unwanted sorting of components Co-authored-by: Tim <> --- asyncapi-format.js | 16 +- test/yaml-custom/output.yaml | 16 +- .../output.yaml | 2 +- .../customSortComponents.yaml | 6 + test/yaml-sort-skip-components/input.yaml | 249 ++++++++++++++++++ test/yaml-sort-skip-components/options.yaml | 4 + test/yaml-sort-skip-components/output.yaml | 248 +++++++++++++++++ 7 files changed, 526 insertions(+), 15 deletions(-) create mode 100644 test/yaml-sort-skip-components/customSortComponents.yaml create mode 100644 test/yaml-sort-skip-components/input.yaml create mode 100644 test/yaml-sort-skip-components/options.yaml create mode 100644 test/yaml-sort-skip-components/output.yaml diff --git a/asyncapi-format.js b/asyncapi-format.js index 5869af4..bc4e978 100644 --- a/asyncapi-format.js +++ b/asyncapi-format.js @@ -132,13 +132,17 @@ async function asyncapiSort(oaObj, options) { sortedObj[keyRes] = prioritySort(sortedObj[keyRes], sortSet[this.key]); } this.update(sortedObj); - } else if (this.path[0] === 'components' && this.path[3] === 'examples') { - // debugStep = 'Generic sorting - skip nested components>examples' - // Skip nested components>examples values } else { - debugStep = 'Generic sorting - properties' - // Sort list of properties - this.update(prioritySort(node, sortSet[this.key])); + if (this.path[0] === 'components' && (this.path[3] === 'examples' || + (this.parent && this.parent.key === 'components'))) { + // debugStep = 'Generic sorting - skip nested components>examples' + // Skip nested components>examples values + // Skip component objects that are not in the sortComponentsSet + } else { + // debugStep = 'Generic sorting - properties' + // Sort list of properties + this.update(prioritySort(node, sortSet[this.key])); + } } } } diff --git a/test/yaml-custom/output.yaml b/test/yaml-custom/output.yaml index 985a5d8..7bbb832 100644 --- a/test/yaml-custom/output.yaml +++ b/test/yaml-custom/output.yaml @@ -49,14 +49,6 @@ components: schema: type: string messages: - dimLight: - name: dimLight - title: Dim light - summary: Command a particular streetlight to dim the lights. - traits: - - $ref: '#/components/messageTraits/commonHeaders' - payload: - $ref: '#/components/schemas/dimLightPayload' lightMeasured: name: lightMeasured title: Light measured @@ -74,6 +66,14 @@ components: - $ref: '#/components/messageTraits/commonHeaders' payload: $ref: '#/components/schemas/turnOnOffPayload' + dimLight: + name: dimLight + title: Dim light + summary: Command a particular streetlight to dim the lights. + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + $ref: '#/components/schemas/dimLightPayload' schemas: lightMeasuredPayload: type: object diff --git a/test/yaml-filter-unused-components-period/output.yaml b/test/yaml-filter-unused-components-period/output.yaml index 68a54de..90e24ba 100644 --- a/test/yaml-filter-unused-components-period/output.yaml +++ b/test/yaml-filter-unused-components-period/output.yaml @@ -1,4 +1,4 @@ -asyncapi: '2.2.0' +asyncapi: 2.2.0 channels: smartylighting.streetlights.1.0.action.{streetlightId}.turn.on: subscribe: diff --git a/test/yaml-sort-skip-components/customSortComponents.yaml b/test/yaml-sort-skip-components/customSortComponents.yaml new file mode 100644 index 0000000..9208b89 --- /dev/null +++ b/test/yaml-sort-skip-components/customSortComponents.yaml @@ -0,0 +1,6 @@ +- schemas +#- messages +- parameters +- messageTraits +- operationTraits +- securitySchemes diff --git a/test/yaml-sort-skip-components/input.yaml b/test/yaml-sort-skip-components/input.yaml new file mode 100644 index 0000000..d775922 --- /dev/null +++ b/test/yaml-sort-skip-components/input.yaml @@ -0,0 +1,249 @@ +asyncapi: 2.2.0 +info: + title: Streetlights Kafka API + version: 1.0.0 + description: | + The Smartylighting Streetlights API allows you to remotely manage the city lights. + + ### Check out its awesome features: + + * Turn a specific streetlight on/off 🌃 + * Dim a specific streetlight 😎 + * Receive real-time information about environmental lighting conditions 📈 + license: + name: Apache 2.0 + url: https://www.apache.org/licenses/LICENSE-2.0 +servers: + production: + url: test.mosquitto.org:{port} + protocol: mqtt + description: Test broker + variables: + port: + description: Secure connection (TLS) is available through port 8883. + default: '1883' + enum: + - '1883' + - '8883' + security: + - apiKey: [] + - supportedOauthFlows: + - streetlights:on + - streetlights:off + - streetlights:dim + - openIdConnectWellKnown: [] +defaultContentType: application/json +channels: + smartylighting.streetlights.1.0.event.{streetlightId}.lighting.measured: + description: The topic on which measured values may be produced and consumed. + parameters: + streetlightId: + $ref: '#/components/parameters/streetlightId' + subscribe: + operationId: measuredStreetlight + message: + $ref: '#/components/messages/turnOnOff' + traits: + - $ref: '#/components/operationTraits/kafka' + publish: + operationId: receiveLightMeasurement + summary: Inform about environmental lighting conditions of a particular streetlight. + message: + $ref: '#/components/messages/lightMeasured' + traits: + - $ref: '#/components/operationTraits/kafka' + smartylighting.streetlights.1.0.action.{streetlightId}.turn.on: + parameters: + streetlightId: + $ref: '#/components/parameters/streetlightId' + subscribe: + operationId: turnOn + message: + $ref: '#/components/messages/turnOnOff' + traits: + - $ref: '#/components/operationTraits/kafka' + smartylighting.streetlights.1.0.action.{streetlightId}.turn.off: + parameters: + streetlightId: + $ref: '#/components/parameters/streetlightId' + subscribe: + operationId: turnOff + message: + $ref: '#/components/messages/turnOnOff' + traits: + - $ref: '#/components/operationTraits/kafka' + smartylighting.streetlights.1.0.action.{streetlightId}.dim: + parameters: + streetlightId: + $ref: '#/components/parameters/streetlightId' + subscribe: + operationId: dimLight + message: + title: Dim light + summary: Command a particular streetlight to dim the lights. + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + type: object + properties: + percentage: + type: integer + description: Percentage to which the light should be dimmed to. + minimum: 0 + maximum: 100 + sentAt: + $ref: '#/components/schemas/sentAt' + traits: + - $ref: '#/components/operationTraits/kafka' +components: + parameters: + streetlightId: + description: The ID of the streetlight. + schema: + type: string + dimId: + description: The ID of the dimmer. + schema: + type: string + abcId: + description: The ID of the abc. + schema: + type: number + messages: + lightMeasured: + name: lightMeasured + title: Light measured + summary: Inform about environmental lighting conditions of a particular streetlight. + contentType: application/json + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + $ref: '#/components/schemas/lightMeasuredPayload' + turnOnOff: + name: turnOnOff + title: Turn on/off + summary: Command a particular streetlight to turn the lights on or off. + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + $ref: '#/components/schemas/turnOnOffPayload' + dimLight: + name: dimLight + title: Dim light + summary: Command a particular streetlight to dim the lights. + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + type: object + properties: + percentage: + type: integer + description: Percentage to which the light should be dimmed to. + minimum: 0 + maximum: 100 + sentAt: + $ref: '#/components/schemas/sentAt' + schemas: + lightMeasuredPayload: + type: object + properties: + lumens: + description: Light intensity measured in lumens. + type: integer + minimum: 0 + sentAt: + $ref: '#/components/schemas/sentAt' + turnOnOffPayload: + type: object + properties: + command: + description: Whether to turn on or off the light. + type: string + enum: + - 'on' + - 'off' + sentAt: + $ref: '#/components/schemas/sentAt' + dimLightPayload: + type: object + properties: + percentage: + description: Percentage to which the light should be dimmed to. + type: integer + minimum: 0 + maximum: 100 + sentAt: + $ref: '#/components/schemas/sentAt' + sentAt: + description: Date and time when the message was sent. + type: string + format: date-time + messageTraits: + eventHeaders: + headers: + type: object + properties: + event-header: + type: integer + minimum: 0 + maximum: 100 + commonHeaders: + headers: + type: object + properties: + my-app-header: + type: integer + minimum: 0 + maximum: 100 + operationTraits: + ws: + bindings: + ws: + clientId: ws-id + kafka: + bindings: + kafka: + clientId: my-app-id + http: + bindings: + http: + clientId: http-id + securitySchemes: + supportedOauthFlows: + type: oauth2 + description: Flows to support OAuth 2.0 + flows: + implicit: + authorizationUrl: https://authserver.example/auth + scopes: + streetlights:on: Ability to switch lights on + streetlights:off: Ability to switch lights off + streetlights:dim: Ability to dim the lights + password: + tokenUrl: https://authserver.example/token + scopes: + streetlights:on: Ability to switch lights on + streetlights:off: Ability to switch lights off + streetlights:dim: Ability to dim the lights + clientCredentials: + tokenUrl: https://authserver.example/token + scopes: + streetlights:on: Ability to switch lights on + streetlights:off: Ability to switch lights off + streetlights:dim: Ability to dim the lights + authorizationCode: + authorizationUrl: https://authserver.example/auth + tokenUrl: https://authserver.example/token + refreshUrl: https://authserver.example/refresh + scopes: + streetlights:on: Ability to switch lights on + streetlights:off: Ability to switch lights off + streetlights:dim: Ability to dim the lights + openIdConnectWellKnown: + type: openIdConnect + openIdConnectUrl: https://authserver.example/.well-known + apiKey: + type: apiKey + in: user + description: Provide your API key as the user and leave the password empty. + diff --git a/test/yaml-sort-skip-components/options.yaml b/test/yaml-sort-skip-components/options.yaml new file mode 100644 index 0000000..ea2955c --- /dev/null +++ b/test/yaml-sort-skip-components/options.yaml @@ -0,0 +1,4 @@ +verbose: true +sortComponentsFile: customSortComponents.yaml +output: output.yaml + diff --git a/test/yaml-sort-skip-components/output.yaml b/test/yaml-sort-skip-components/output.yaml new file mode 100644 index 0000000..ba79b8d --- /dev/null +++ b/test/yaml-sort-skip-components/output.yaml @@ -0,0 +1,248 @@ +asyncapi: 2.2.0 +info: + title: Streetlights Kafka API + version: 1.0.0 + description: | + The Smartylighting Streetlights API allows you to remotely manage the city lights. + + ### Check out its awesome features: + + * Turn a specific streetlight on/off 🌃 + * Dim a specific streetlight 😎 + * Receive real-time information about environmental lighting conditions 📈 + license: + name: Apache 2.0 + url: https://www.apache.org/licenses/LICENSE-2.0 +servers: + production: + url: test.mosquitto.org:{port} + protocol: mqtt + description: Test broker + variables: + port: + description: Secure connection (TLS) is available through port 8883. + default: '1883' + enum: + - '1883' + - '8883' + security: + - apiKey: [] + - supportedOauthFlows: + - streetlights:on + - streetlights:off + - streetlights:dim + - openIdConnectWellKnown: [] +defaultContentType: application/json +channels: + smartylighting.streetlights.1.0.event.{streetlightId}.lighting.measured: + description: The topic on which measured values may be produced and consumed. + parameters: + streetlightId: + $ref: '#/components/parameters/streetlightId' + subscribe: + operationId: measuredStreetlight + message: + $ref: '#/components/messages/turnOnOff' + traits: + - $ref: '#/components/operationTraits/kafka' + publish: + operationId: receiveLightMeasurement + summary: Inform about environmental lighting conditions of a particular streetlight. + message: + $ref: '#/components/messages/lightMeasured' + traits: + - $ref: '#/components/operationTraits/kafka' + smartylighting.streetlights.1.0.action.{streetlightId}.turn.on: + parameters: + streetlightId: + $ref: '#/components/parameters/streetlightId' + subscribe: + operationId: turnOn + message: + $ref: '#/components/messages/turnOnOff' + traits: + - $ref: '#/components/operationTraits/kafka' + smartylighting.streetlights.1.0.action.{streetlightId}.turn.off: + parameters: + streetlightId: + $ref: '#/components/parameters/streetlightId' + subscribe: + operationId: turnOff + message: + $ref: '#/components/messages/turnOnOff' + traits: + - $ref: '#/components/operationTraits/kafka' + smartylighting.streetlights.1.0.action.{streetlightId}.dim: + parameters: + streetlightId: + $ref: '#/components/parameters/streetlightId' + subscribe: + operationId: dimLight + message: + title: Dim light + summary: Command a particular streetlight to dim the lights. + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + type: object + properties: + percentage: + type: integer + description: Percentage to which the light should be dimmed to. + minimum: 0 + maximum: 100 + sentAt: + $ref: '#/components/schemas/sentAt' + traits: + - $ref: '#/components/operationTraits/kafka' +components: + parameters: + abcId: + description: The ID of the abc. + schema: + type: number + dimId: + description: The ID of the dimmer. + schema: + type: string + streetlightId: + description: The ID of the streetlight. + schema: + type: string + messages: + lightMeasured: + name: lightMeasured + title: Light measured + summary: Inform about environmental lighting conditions of a particular streetlight. + contentType: application/json + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + $ref: '#/components/schemas/lightMeasuredPayload' + turnOnOff: + name: turnOnOff + title: Turn on/off + summary: Command a particular streetlight to turn the lights on or off. + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + $ref: '#/components/schemas/turnOnOffPayload' + dimLight: + name: dimLight + title: Dim light + summary: Command a particular streetlight to dim the lights. + traits: + - $ref: '#/components/messageTraits/commonHeaders' + payload: + type: object + properties: + percentage: + type: integer + description: Percentage to which the light should be dimmed to. + minimum: 0 + maximum: 100 + sentAt: + $ref: '#/components/schemas/sentAt' + schemas: + dimLightPayload: + type: object + properties: + percentage: + description: Percentage to which the light should be dimmed to. + type: integer + minimum: 0 + maximum: 100 + sentAt: + $ref: '#/components/schemas/sentAt' + lightMeasuredPayload: + type: object + properties: + lumens: + description: Light intensity measured in lumens. + type: integer + minimum: 0 + sentAt: + $ref: '#/components/schemas/sentAt' + sentAt: + description: Date and time when the message was sent. + type: string + format: date-time + turnOnOffPayload: + type: object + properties: + command: + description: Whether to turn on or off the light. + type: string + enum: + - 'on' + - 'off' + sentAt: + $ref: '#/components/schemas/sentAt' + messageTraits: + commonHeaders: + headers: + type: object + properties: + my-app-header: + type: integer + minimum: 0 + maximum: 100 + eventHeaders: + headers: + type: object + properties: + event-header: + type: integer + minimum: 0 + maximum: 100 + operationTraits: + http: + bindings: + http: + clientId: http-id + kafka: + bindings: + kafka: + clientId: my-app-id + ws: + bindings: + ws: + clientId: ws-id + securitySchemes: + apiKey: + type: apiKey + in: user + description: Provide your API key as the user and leave the password empty. + openIdConnectWellKnown: + type: openIdConnect + openIdConnectUrl: https://authserver.example/.well-known + supportedOauthFlows: + type: oauth2 + description: Flows to support OAuth 2.0 + flows: + implicit: + authorizationUrl: https://authserver.example/auth + scopes: + streetlights:on: Ability to switch lights on + streetlights:off: Ability to switch lights off + streetlights:dim: Ability to dim the lights + password: + tokenUrl: https://authserver.example/token + scopes: + streetlights:on: Ability to switch lights on + streetlights:off: Ability to switch lights off + streetlights:dim: Ability to dim the lights + clientCredentials: + tokenUrl: https://authserver.example/token + scopes: + streetlights:on: Ability to switch lights on + streetlights:off: Ability to switch lights off + streetlights:dim: Ability to dim the lights + authorizationCode: + authorizationUrl: https://authserver.example/auth + tokenUrl: https://authserver.example/token + refreshUrl: https://authserver.example/refresh + scopes: + streetlights:on: Ability to switch lights on + streetlights:off: Ability to switch lights off + streetlights:dim: Ability to dim the lights