swagger-api / swagger-api/swagger-codegen
[codegen][JAVA] BUG: No regex escaping for Ref fiends
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
When generating from swagger 2.0 spec @Pattern annotation contains regex which is not escaped if $ref is used for field definition.
Tested for swagger-codegen 2.4.0
Steps to reproduce
Swagger spec:
swagger: '2.0'
info:
version: '1.0'
title: Test
paths:
/command:
get:
summary: Test command
operationId: test
produces:
- application/json
responses:
200:
schema:
$ref: '#/definitions/Root'
definitions:
Root:
type: object
properties:
memberId:
$ref: '#/definitions/MemberId'
anotherId:
type: string
pattern: ^\d{10}$
MemberId:
type: string
pattern: ^\d{10}$
Which results in code like this:
@Validated
public class Root {
@JsonProperty("memberId")
private String memberId = null;
@JsonProperty("anotherId")
private String anotherId = null;
public Root memberId(String memberId) {
this.memberId = memberId;
return this;
}
@ApiModelProperty(value = "")
@Pattern(regexp="^\d{10}$")
public String getMemberId() {
return memberId;
}
public void setMemberId(String memberId) {
this.memberId = memberId;
}
public Root anotherId(String anotherId) {
this.anotherId = anotherId;
return this;
}
@ApiModelProperty(value = "")
@Pattern(regexp="^\\d{10}$")
public String getAnotherId() {
return anotherId;
}
public void setAnotherId(String anotherId) {
this.anotherId = anotherId;
}
}
We can see @Pattern(regexp="^\d{10}$") for memberId field and @Pattern(regexp="^\\d{10}$") for anotherId. Lack of escaping in memberId produces compilation error due to wrong string literal format.
Related issues/PRs
Validation Contraints of $ref (JSON Reference) components are not generated
Suggest a fix/enhancement
Looks like string escaping is not applied for pattern in $ref fields.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the Swagger 2.0 specification and compare the generated Java model's @Pattern annotations for the referenced and inline fields. Trace how the referenced pattern reaches the generated annotation, then verify that both annotations compile with equivalent escaped regex strings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100