swagger-api / swagger-api/swagger-codegen

@Pattern is not generated for scalar types

Open
#10,989 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Hi.
Given the following definitions:

swagger: '2.0'
info:
  title: MyDomainObject
  version: "1.0"
definitions:
  MyDomainObject:
      type: object
      required:
        - currency
      properties:
        currency:
          $ref: '#/definitions/CurrencyIso'
        riskCurrency:
          $ref: '#/definitions/CurrencyIso'

  CurrencyIso:
    description: |
      The ISO currency definition.
    type: string
    pattern: ^[A-Z]{3}|[0-9]{3}$
    example: CHF
paths: {}

when the class is generated, no @Pattern annotation is inserted on the getter of the fields referencing the CurrencyIso.
I'm actually using the swagger-codegen-maven-plugin with version 2.3.1.
It seems that version 2.4.x fixes this issue, but then the patterns are not escaped when generated, leading to not compilable code in certain cases (e.g., if it contains \d+). There is already this issue about it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing generation with the supplied Swagger 2.0 definition through the swagger-codegen-maven-plugin on version 2.3.1, then compare the 2.4.x behavior. Review related issue #9509 for the escaping context. Done means scalar references receive @Pattern and regexes such as \d+ produce compilable generated code.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.