openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][Java] @Tag description should merge multiple lines into single line and trim the newline char at the end

Open tigerinus opened this issue 2 years ago • 3 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [ ] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
openapi-generator version

6.0.0

OpenAPI declaration file content or url

https://newpathfly.ticketcombine.com/openapi.yaml

pay attention to following lines:

tags:
  - name: Shopping
    description: |
      Search for virtual interlining offerings of cheapest flights.
Generation Details
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.0.0</version>
                <executions>
                    <execution>
                        <id>1</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.newpathfly.api</apiPackage>
                            <modelPackage>com.newpathfly.model</modelPackage>
                            <configOptions>
                                <dateLibrary>java8</dateLibrary>
                                <interfaceOnly>true</interfaceOnly>
                                <openApiNullable>false</openApiNullable>
                                <serializableModel>true</serializableModel>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce
  1. git clone https://github.com/newpathfly/newpathfly-public-api
  2. update openapi-generator-maven-plugin to 6.0.0
  3. run mvn clean package
  4. look at the ShoppingApi.java as example in the generated-sources under target folder

Actual:

There is a newline at the end of the tag description:

@Tag(name = "shopping", description = "Search for virtual interlining offerings of cheapest flights.
")

Expect:

There should not be any newline at the end of the tag description:

@Tag(name = "shopping", description = "Search for virtual interlining offerings of cheapest flights.")
Related issues/PRs
Suggest a fix

tigerinus avatar May 26 '22 15:05 tigerinus

Hello, just to notice that I'm facing the same issue.

Beef-Tech avatar May 31 '22 12:05 Beef-Tech

Workaround would be using block chomping indicator strip - to strip new line at the end. You could also use block scalar style folded > to replace new lines with spaces.

By declaring your Tag as following:

tags:
  - name: Shopping
    description: >-
      Search for virtual interlining offerings of cheapest flights.

parenko avatar Jun 10 '22 10:06 parenko

Hey there,

this also happens if you use \n within a json definition:

"tags": [
    {
      "name": "groups",
      "description": "**Current Group Roles**: \n\nROLE1 - User may do stuff 1 \n\nROLE2 - User may do other stuff \n\nROLE3 - User may even more stuff \n\n"
    }
  ]

This will generate broken Tag annotations with line breaks within the description of the tag.

@Tag(name = "Groups", description = "**Current Group Roles**: 

ROLE1 - User may do stuff 1

ROLE2 - User may do other stuff 

ROLE3 - User may even more stuff

")

This happens in 6.0.1

Sadly in this case the Yaml workarounds do not work.

GandalfIX avatar Aug 03 '22 21:08 GandalfIX

This issue still seems to be relevant in 6.2.1 version, or am I missing something?

Gereks123 avatar Nov 09 '22 11:11 Gereks123

Any update on this issue? I can not use the work around as I do not maintain the open api spec we are trying to consume.

taylorcressy avatar Jan 20 '23 18:01 taylorcressy

It would be great to introduce a new Lambda which changes all new lines with \n Something like:

public class ReplaceNewLineLambda implements Mustache.Lambda {

  @Override
  public void execute(Template.Fragment fragment, Writer writer) throws IOException {
    String text = fragment.execute();
    writer.write(text.replaceAll("\n", "\\\\n"));
  }

}

and usage is:

@Tag(name = "{{{tagName}}}", description = {{#tagDescription}}"{{#lambda.replaceNewLine}}{{{.}}}{{/lambda.replaceNewLine}}"{{/tagDescription}}{{^tagDescription}}"the {{{tagName}}} API"{{/tagDescription}})

Saljack avatar Mar 10 '23 08:03 Saljack

Hi, some Mustache.Lambda already exists in SpringCodegen.java such as lambdaEscapeDoubleQuote and lambdaRemoveLineBreak.
(modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java:683)

In api.mustache, maybe just replacing
{{#tagDescription}}"{{{.}}}"{{/tagDescription}}
with
{{#tagDescription}}"{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{.}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}"{{/tagDescription}}
would fix the issue ?
(modules/openapi-generator/src/main/resources/JavaSpring/api.mustache:87)

lea-raya avatar Mar 28 '23 10:03 lea-raya