因字段以下划线开头而导致Swagger代码生成编译问题。

huangapple go评论71阅读模式
英文:

Swagger codegen compilation issues due to field starting with an underscore

问题

我有以下的openApi定义,请注意type_type字段:

openapi: 3.0.1
info:
  title: 'title'
  description: 'description'
  version: 1.0.0
paths:
  /pet:
    get:
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    Pet:
      type: object
      properties:
        type:
          type: string
        _type:
          type: string

当我尝试使用上述定义生成Java客户端时,在io.swagger.client.model.Pet中得到以下结果:

public class Pet {
...
   /**
   * 获取type
   * @return type
  **/
  @Schema(description = "")
  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public Pet _type(String _type) {
    this._type = _type;
    return this;
  }

   /**
   * 获取_type
   * @return _type
  **/
  @Schema(description = "")
  public String getType() {
    return _type;
  }

  public void setType(String _type) {
    this._type = _type;
  }
...
}

由于getTypesetType方法重复,这将无法编译通过。我该如何更新我的openApi以避免这种情况?

我不关心使用哪些getter/setter方法,但我无法更改字段名称。

可以使用 https://editor.swagger.io/ 进行复现。

更新:我从最初发布的内容中大幅简化了我的问题,该问题包含了从openApi定义生成的Java类。

英文:

I have the following openApi definition, notice the type and _type fields:

openapi: 3.0.1
info:
  title: 'title'
  description: 'description'
  version: 1.0.0
paths:
  /pet:
    get:
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    Pet:
      type: object
      properties:
        type:
          type: string
        _type:
          type: string

When I try and generate a Java client using the above, I get the following results in io.swagger.client.model.Pet

public class Pet {
...
   /**
   * Get type
   * @return type
  **/
  @Schema(description = "")
  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public Pet _type(String _type) {
    this._type = _type;
    return this;
  }

   /**
   * Get _type
   * @return _type
  **/
  @Schema(description = "")
  public String getType() {
    return _type;
  }

  public void setType(String _type) {
    this._type = _type;
  }
...
}

Which will not compile since the methods getType and setType are duplicated. How can I update my openApi to avoid this?

I don't care what getter/setter methods are used, however I am not able to change the field names.

This can be reproduced using https://editor.swagger.io/.

Update: I've simplified my question substantial from what I original posted which included the java classes the openApi definition l was generated from.

答案1

得分: 1

这个问题出现在解析属性名称时(由PropertyNamingStrategy执行)。因此,通常第一个下划线 _ 可能会被跳过。例如,使用PropertyNamingStrategy.SNAKE_CASE的情况如下:

private static String toSnakeCase(String input) {
    if (input == null) return input;
    int length = input.length();
    StringBuilder result = new StringBuilder(length * 2);
    int resultLength = 0;
    boolean wasPrevTranslated = false;
    for (int i = 0; i < length; i++) {
        char c = input.charAt(i);
        if (i > 0 || c != '_') // 跳过第一个开始的下划线
        {
            if (Character.isUpperCase(c)) {
                if (!wasPrevTranslated && resultLength > 0 && result.charAt(resultLength - 1) != '_') {
                    result.append('_');
                    resultLength++;
                }
                c = Character.toLowerCase(c);
                wasPrevTranslated = true;
            } else {
                wasPrevTranslated = false;
            }
            result.append(c);
            resultLength++;
        }
    }
    return resultLength > 0 ? result.toString() : input;
}

这个链接可能会给你一个关于属性名称解析工作原理的线索。

英文:

This problem comes when resolving property names (which is done by PropertyNamingStrategy). So, usually the first _ might be skipped. As in for example PropertyNamingStrategy.SNAKE_CASE which uses:

private static String toSnakeCase(String input) {
            if (input == null) return input;
            int length = input.length();
            StringBuilder result = new StringBuilder(length * 2);
            int resultLength = 0;
            boolean wasPrevTranslated = false;
            for (int i = 0; i &lt; length; i++) {
                char c = input.charAt(i);
                if (i &gt; 0 || c != &#39;_&#39;) // skip first starting underscore
                {
                    if (Character.isUpperCase(c)) {
                        if (!wasPrevTranslated &amp;&amp; resultLength &gt; 0 &amp;&amp; result.charAt(resultLength - 1) != &#39;_&#39;) {
                            result.append(&#39;_&#39;);
                            resultLength++;
                        }
                        c = Character.toLowerCase(c);
                        wasPrevTranslated = true;
                    } else {
                        wasPrevTranslated = false;
                    }
                    result.append(c);
                    resultLength++;
                }
            }
            return resultLength &gt; 0 ? result.toString() : input;
        }

This link might give you a clue on how property name resolution works.

huangapple
  • 本文由 发表于 2020年10月20日 03:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64433735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定