swagger-api / swagger-api/swagger-codegen

Java client generates models with `String` fields instead of `URL`

Open
#4,963 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Workaround available
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I've generated a Java client ( OkHttp ) from my team's Swagger spec. Everything looks good with the exception that models which have fields defined as url in the spec are generated as Strings.

Ideally we'd like these to be generated as URLs so we get the benefit of type-safety and fail before making a call to our service.

Please point out if this is a non-issue and/or if I'm doing something wrong.

Thanks

Swagger-codegen version

Brew managed: swagger-codegen: stable 2.2.2 (bottled), HEAD

Swagger declaration file content or url
```yaml
  hook:
    type: string
    format: url
    description: The webhook endpoint to which notifications are pushed
```

Full sample spec provided here.

Command line used for generation

swagger-codegen generate -i spec-linked-above -l java -o codegen-test/ --api-package com.myco.apackage.name

Steps to reproduce
  1. Execute the Command line provided directly above
  2. Look at the Subscription model, it has method signatures in the format: public void setHook(String hook)
Related issues

Not that I am aware of

Suggest a Fix

Other data types are implemented as expected (eg: public Integer getIndex()). I hope/expect URL to be treated similarly.

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 with the Java generator entry point used by swagger-codegen generate -i ... -l java, then trace how the Swagger type: string, format: url property is mapped into the Subscription model. Reproduce the issue with the linked sample spec and inspect the generated setHook(String hook) signature. Done means URL fields are generated with the intended Java type and the relevant generation behavior is verified.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.