1

I am trying to modify a YAML file to remove all of the operationId keys. And the output of file is being all jumbled to the point that I can't actually see any pattern related to the location of the files.

def remove_operationId_tag():
    with open("my_yaml.yml", "r") as stream:
        data = nested_delete(yaml.safe_load(stream), 'operationId')
    with open('new_yaml.yml', 'w') as outfile:
        yaml.dump(data, outfile, default_flow_style=False)

if __name__ == '__main__':
    remove_operationId_tag()

The file that gets parsed in looks like

paths:
  "/users/{id}":
    parameters:
      - name: id
        in: path
        required: true
        description: the user identifier, as userId
        schema:
          type: string
          additionalProperties: false
    get:
      responses:
        "200":
          description: the user being returned
          content:
            application/json:
              schema:
                type: object
                additionalProperties: false
                properties:
                  uuid:
                    type: string
                    format: uuid
          links:
            address:
              operationId: getUserAddress
              parameters:
                userId: $request.path.id
  "/users/{userid}/address":
    parameters:
      - name: userid
        in: path
        required: true
        description: the user identifier, as userId
        schema:
          type: string
          additionalProperties: false
    get:
      operationId: getUserAddress
      responses:
        "200":
          description: the user's address

and the desired output would be

paths:
  "/users/{id}":
    parameters:
      - name: id
        in: path
        required: true
        description: the user identifier, as userId
        schema:
          type: string
          additionalProperties: false
    get:
      responses:
        "200":
          description: the user being returned
          content:
            application/json:
              schema:
                type: object
                additionalProperties: false
                properties:
                  uuid:
                    type: string
                    format: uuid
          links:
            address:
              parameters:
                userId: $request.path.id
  "/users/{userid}/address":
    parameters:
      - name: userid
        in: path
        required: true
        description: the user identifier, as userId
        schema:
          type: string
          additionalProperties: false
    get:
      responses:
        "200":
          description: the user's address

However, the output I am getting is

paths:
  /users/{id}:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                additionalProperties: false
                properties:
                  uuid:
                    format: uuid
                    type: string
                type: object
          description: the user being returned
          links:
            address:
              parameters:
                userId: $request.path.id
    parameters:
    - description: the user identifier, as userId
      in: path
      name: id
      required: true
      schema:
        additionalProperties: false
        type: string
  /users/{userid}/address:
    get:
      responses:
        '200':
          description: the user's address
    parameters:
    - description: the user identifier, as userId
      in: path
      name: userid
      required: true
      schema:
        additionalProperties: false
        type: string

How could I fix this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
jack gell
  • 175
  • 1
  • 8
  • 1
    Does this answer your question? [dumping a dictionary to a YAML file while preserving order](https://stackoverflow.com/questions/31605131/dumping-a-dictionary-to-a-yaml-file-while-preserving-order) – mkrieger1 Mar 04 '22 at 13:16

0 Answers0