bump-sh / cli

Bump.sh CLI - Deploy your OpenAPI & AsyncAPI documentations from your CI
https://bump.sh
MIT License
44 stars 4 forks source link

bump overlay not merging parameters #580

Open thim81 opened 2 months ago

thim81 commented 2 months ago

Steps to reproduce

Openapi.yaml

openapi: 3.0.1
info:
  title: Datasource API
  description: API to work with Datasources.
  termsOfService: 'urn:tos'''
paths:
  /timeseries:
    get:
      operationId: searchTimeSeriesDefinitions
      summary: Search the time series definitions
      parameters:
        - name: searchPattern
          in: query
          description: Pattern to search time series definitions. Cannot be used at the same time as name parameter.
          required: false
          schema:
            type: string

overlays.yaml

overlay: 1.0.0
info:
  title: Structured Overlay
  version: 1.0.0
actions:
  - target: "$"   # Root of document
    update:
      paths:
        /timeseries:
          get:
            description: Retrieve a list of all time series definitions. This endpoint supports search and pagination.
            parameters:
              - name: searchPattern
                in: query
                description: A pattern used to search time series definitions.
                schema:
                  example: temp-

execute: bump overlay openapi.yaml overlays.yaml > openapi.public.yaml

Result in:

paths:
  /timeseries:
    get:
      operationId: searchTimeSeriesDefinitions
      summary: Search the time series definitions
      description: Retrieve a list of all time series definitions. This endpoint supports search and pagination.
      parameters:
        - name: searchPattern
          in: query
          description: Pattern to search time series definitions. Cannot be used at the same time as name parameter.
          required: false
          schema:
            type: string
        - name: searchPattern
          in: query
          description: A pattern used to search time series definitions.
          schema:
            example: temp*

Expected result

paths:
  /timeseries:
    get:
      operationId: searchTimeSeriesDefinitions
      summary: Search the time series definitions
      description: Retrieve a list of all time series definitions. This endpoint supports search and pagination.
      parameters:
        - name: searchPattern
          in: query
          description: A pattern used to search time series definitions.
          required: false
          schema:
            type: string
            example: temp-
thim81 commented 2 months ago

I think, it would make sense to provide an explicit merge instruction for the "parameters".

This will handle that the parameters are merged intelligently by checking for existing parameters with the same name and path location (in: query in this case)

Something like this?

jsonpath.apply(spec, target, (chunk) => {
  if (typeof chunk === 'object' && typeof action.update === 'object') {
    if (Array.isArray(chunk) && Array.isArray(action.update)) {
      // Handle array merging carefully to avoid duplicating parameters
      if (target.includes('parameters')) {
        // Merge the parameters array by checking if parameter already exists
        const mergedParameters = [...chunk];
        action.update.forEach((newParam: any) => {
          const existingParamIndex = mergedParameters.findIndex(
            (p: any) => p.name === newParam.name && p.in === newParam.in
          );
          if (existingParamIndex > -1) {
            // Update the existing parameter
            mergedParameters[existingParamIndex] = merger(mergedParameters[existingParamIndex], newParam);
          } else {
            // Add the new parameter if it doesn't exist
            mergedParameters.push(newParam);
          }
        });
        return mergedParameters;
      } else {
        // For arrays that are not parameters, just concatenate
        return chunk.concat(action.update);
      }
    } else {
      return merger(chunk, action.update);
    }
  } else {
    return action.update;
  }
});