swagger-api / swagger-api/swagger-codegen

[DefaultCodegen] Adding namespace to model definitions

Open
#7,326 5 comments 14 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

Description

I have a specification where I have two shapes (models) with the same name but different properties:

If I was hand-coding the models, I'd solve this problem by placing them into separate namespaces:
https://gist.github.com/brendanclement/d31cb1ebea640a8d33bf8f4964fc1bae

If I try to organize models by namespace -- Swagger codegen currently (using Java as an example) strips '.' from any Schema definition key and will return two models: class Namespace1Foo & class Namespace2Foo.

I'd actually like the codegen to output

package namespace1;
class Foo {} 

--------------------

package namespace2;
class Foo {}
Swagger-codegen version

2.2.2+

Related issues/PRs

Organizing Models - https://github.com/swagger-api/swagger-codegen/issues/6926
Custom namespace for different Models - https://github.com/swagger-api/swagger-codegen/issues/3915

Suggest a fix/enhancement
  • Add an optional generator option: useModelNamespacing (default: FALSE) - which when enabled, the generator will honour the object name when generating code, e.g. outputted models will use package/namespace if specified in definition key -- e.g. io.swagger.model.Foobar instead of IoSwaggerModelFoobar . If value is FALSE then codegen will use existing (current) behaviour.
  • Make namespace (package name) a concept in CodegenModel
  • add a toNamespaceName() to the CodegenConfig interface that can be overridden per-language.
  • Adjust toModelImport to ensure that models are placed in correct location

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 reading the CodegenModel and CodegenConfig concepts described in the issue, then trace how toModelImport currently determines model locations for the Java generator. Compare the existing flattened names with the requested useModelNamespacing behavior; done means namespaced model packages and imports are generated without changing the default behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.