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.
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.