英文:
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;
}
...
}
由于getType
和setType
方法重复,这将无法编译通过。我该如何更新我的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 < length; i++) {
char c = input.charAt(i);
if (i > 0 || c != '_') // skip first starting underscore
{
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 link might give you a clue on how property name resolution works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论