swagger-api / swagger-api/swagger-codegen

Process Models and Operations Before Generation - Post-Processing Has Access to Both Models and Operations

Open
#8,253 1 comment 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

Hey there,

The problem I am trying to solve is that I would like to perform some logic during Models Post-Processing that is conditional upon the operations.
In DefaultGenerator.java:

private Map<String, Object> processModels(CodegenConfig config, Map<String, Model> definitions, Map<String, Model> allDefinitions)

Here you can see that when processing Models we only have access to models, but as seen below, when processing operations we have access to both Models and Operations.

private Map<String, Object> processOperations(CodegenConfig config, String tag, List<CodegenOperation> ops, List<Object> allModels)

My proposal is for the Post-Processing of both Models and Operations to happen before any files are generated. This means that the Post-Processing of Models will have access to the Operations, just like Post-Processing of the Operations now has access to the Models, as shown below:
From CodegenConfig.java

public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> ops, List<Object> allModels)

The above is great, but we also need a postProcessModelsWithOperations:

public Map<String, Object> postProcessModelsWithOperations(List<Object> allModels, Map<String, Object> ops) 

Unfortunately it looks like the codebase is highly dependent on the current order of operations, making the processing difficult to separate from the generation since the Pre and Post-Processing all take place inside of the generateModels/generateApis methods:
DefaultGenerator.java

  public List<File> generate() {
    if (this.swagger != null && this.config != null) {
      this.configureGeneratorProperties();
      this.configureSwaggerInfo();
      InlineModelResolver inlineModelResolver = new InlineModelResolver();
      inlineModelResolver.flatten(this.swagger);
      List<File> files = new ArrayList();
      List<Object> allModels = new ArrayList();
      this.generateModels(files, allModels);
      List<Object> allOperations = new ArrayList();
      this.generateApis(files, allOperations, allModels);
      Map<String, Object> bundle = this.buildSupportFileBundle(allOperations, allModels);
      this.generateSupportingFiles(files, bundle);
      this.config.processSwagger(this.swagger);
      return files;
    } else {
      throw new RuntimeException("missing swagger input or config!");
    }
  }

The code change itself should be easy, but I'm guessing it will be a breaking change for people who are for some reason depending on the Model files being generated and written to disk before we process and generate the Operations. Maybe I will open up a PR so we can see what the change will look like...

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 in DefaultGenerator.java, especially generate(), generateModels(), and generateApis(), then inspect the related hooks in CodegenConfig.java. Trace the current processing and file-generation order and identify the compatibility implications. Done means model post-processing can receive operations before files are generated without breaking the existing generation flow.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.