openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.15k stars 321 forks source link

Indents Yaml recipe produces invalid outcome with > syntax #3531

Closed AlejandroBertolo closed 9 months ago

AlejandroBertolo commented 1 year ago

Hello. First of all, thanks for your work in this useful tool.

We have run into a rare formatting issue with the YAML Indents stock recipe. It seems to be related to the use of the > char. This specific case was with an openapi.yml file.

What version of OpenRewrite are you using?

How are you running OpenRewrite?

Through maven command in multimodule projects. Java17 and maven 3.8.4.

What is the smallest, simplest way to reproduce the problem?

The following yaml fragment is valid. But after applying Indents it no longer validates.

openapi: 3.0.3
info:
  title: Search Plugin
  description: API specification for all endpoints in the Search Plugin
  version: 3.0.0
servers:
  - url: https://api.staging.xxyyzz.co
    description: Staging NQs and RTs Environment
  - url: https://api.xxyyzz.co
    description: Production NQs and RTs Environment
paths:
  /search/v1/query/xxxx/xxyyzzb:
    get:
      tags:
        - Search service
      summary: xxyyzzb/suggestions endpoint
      operationId: getxxyyzzb
      description: >
        Suggestions with top queries in the Search Plugin.
      parameters:
        - in: header
          name: xxx-apiKey
          schema:
            type: string
          required: true
        - name: query
          in: query
          description: >
            Query to retrieve top trends from
              - Example: shirt
          required: false
          schema:
            type: string
        - name: lang
          in: query
          description: >
            Language
              - Example: en_GB
          required: false
          schema:
            type: string

Non-validating outcome

openapi: 3.0.3
info:
  title: Search Plugin
  description: API specification for all endpoints in the Search Plugin
  version: 3.0.0
servers:
  - url: https://api.staging.xxyyzz.co
    description: Staging NQs and RTs Environment
  - url: https://api.xxyyzz.co
    description: Production NQs and RTs Environment
paths:
  /search/v1/query/xxxx/xxyyzzb:
    get:
      tags:
        - Search service
      summary: xxyyzzb/suggestions endpoint
      operationId: getxxyyzzb
      description: >
        Suggestions with top queries in the Search Plugin.
      parameters:
      - in: header
        name: xxx-apiKey
        schema:
          type: string
        required: true
      - name: query
        in: query
        description: >
            Query to retrieve top trends from
              - Example: shirt
          required: false
        schema:
          type: string
      - name: lang
        in: query
        description: >
            Language
              - Example: en_GB
          required: false
        schema:
          type: string

diff

21,28c21,28
<         - in: header
<           name: xxx-apiKey
<           schema:
<             type: string
<           required: true
<         - name: query
<           in: query
<           description: >
---
>       - in: header
>         name: xxx-apiKey
>         schema:
>           type: string
>         required: true
>       - name: query
>         in: query
>         description: >
32,36c32,36
<           schema:
<             type: string
<         - name: lang
<           in: query
<           description: >
---
>         schema:
>           type: string
>       - name: lang
>         in: query
>         description: >
40,41c40,41
<           schema:
<             type: string
---
>         schema:
>           type: string

Thanks in advance.

timtebeek commented 1 year ago

Hi @AlejandroBertolo ; thanks for the kind words and detailed report! Sorry to hear about your issue.

I've been able to replicate the issue with the following test in https://github.com/openrewrite/rewrite/blob/fab92f59e4b98786fffdcd0814ff39cd47bd5605/rewrite-yaml/src/test/java/org/openrewrite/yaml/format/IndentsTest.java#L28

    @Test
    @Issue("https://github.com/openrewrite/rewrite/issues/3531")
    void multilineString() {
        rewriteRun(
          yaml("""
            foo:
              bar: >
                A multiline string.
              baz:
                quz: Another string.
            """
          )
        );
    }

Not yet sure what exactly causes the issue, or how to resolve, but it's a starting point if you're looking to help explore the issue and potential fix.