swagger-api / swagger-api/swagger-codegen

[SPRING] Parameters defined on the `path` level are skipped during the generation when path definition is in separate file.

Open
#11,089 0 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

Description

Parameters defined on the path level (applicable for all methods for this path) are skipped during the generation when path definition is in separate file.
Language: Spring, generating controller interfaces

Swagger-codegen version

3.0.27

Swagger declaration file

openapi.yaml

openapi: 3.0.4
info:
  title: Test
  description: Test project
  version: 0.1.0
servers:
  - url: http://localhost:8080
    description: development server

components:

paths:
  /test/{testParam}:
    $ref: "path.yaml"

path.yaml

summary: Operation on configuration group
get:
  responses:
    '200':
      description: "OK"
    default:
      description: "ERROR"

parameters:
  - name: testParam
    in: path
    required: true
    schema:
      type: string

Output: (TestApi.java):

/**
 * NOTE: This class is auto generated by the swagger code generator program (3.0.27).
 * https://github.com/swagger-api/swagger-codegen
 * Do not edit the class manually.
 */
package com.example.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.CookieValue;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2021-07-08T11:54:07.880831+02:00[Europe/Warsaw]")
@Validated
public interface TestApi {

    Logger log = LoggerFactory.getLogger(TestApi.class);

    default Optional<ObjectMapper> getObjectMapper(){
        return Optional.empty();
    }

    default Optional<HttpServletRequest> getRequest(){
        return Optional.empty();
    }

    default Optional<String> getAcceptHeader() {
        return getRequest().map(r -> r.getHeader("Accept"));
    }

    @Operation(summary = "", description = "", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(responseCode = "200", description = "OK"),
        
        @ApiResponse(responseCode = "200", description = "ERROR") })
    @RequestMapping(value = "/test/{testParam}",
        method = RequestMethod.GET)
    default ResponseEntity<Void> testTestParamGet() {
        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
        } else {
            log.warn("ObjectMapper or HttpServletRequest not configured in default TestApi interface so no example is generated");
        }
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

}

Command line used for generation
java -jar swagger-codegen-cli-3.0.27.jar generate -i api/openapi.yaml -c swagger-codegen-spring-config.json -l spring -o result/

swagger-codegen-spring-config.json:

{
  "modelPackage": "com.example",
  "apiPackage": "com.example.controllers",
  "dateLibrary": "java8",
  "java8": true,
  "interfaceOnly": true,
  "useBeanValidation": true
}
Steps to reproduce
# Clone repository
git clone https://github.com/xsw2-2wsx/PossibleSwaggerCodegenBug
cd PossibleSwaggerCodegenBug

# Generate 
java -jar swagger-codegen-cli-3.0.27.jar generate -i api/openapi.yaml -c swagger-codegen-spring-config.json -l spring -o result/

# Display the result
cat result/src/main/java/com/example/controllers/TestApi.java
Related issues/PRs

#11088

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 the command against openapi.yaml and path.yaml, then inspect the generated TestApi.java to trace where the path-level testParam is lost. Done means the generated Spring controller interface includes testParam for the referenced path while preserving the existing endpoint output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend
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.