swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.91k stars 6.03k forks source link

[PYTHON] variable substitution not occurring during template generation OpenAPI spec 3.0 #10480

Open jazzapple opened 3 years ago

jazzapple commented 3 years ago
Description

I'm using server templating in my swagger.yaml and swagger-codegen-cli-3.0.21 JAR file to generate a Python SDK. In the generated configuration.py file, the host is populated as a string with the placeholders rather than being substituted. This consequently returns "name or service unknown" error when the client tries to connect to the host

Swagger-codegen version

swagger-codegen-cli-3.0.21

Swagger declaration file content or url
openapi: 3.0.0
info:
  title: my model
  version: '1.0'
servers:
- url: '{protocol}://{server}/{basePath}'
  variables:
    protocol:
      enum:
      - http
      - https
      default: http
    server:
      enum:
      - localhost:1234
      - prod_server
      default: localhost:1234
    basePath:
      default: v1/mymodel
Command line used for generation

java -jar swagger-codegen-cli-3.0.21 generate -i myclient/swagger.yaml -o myclient/ -l python

Steps to reproduce

run command line above, returns configuration.py with:

self.host = "{protocol}://{server}/{basePath}"
Related issues/PRs
Suggest a fix/enhancement

have codegen perform variable substitution on the default values and populate host accordingly in configuration.py. Similar functionality has been implemented in OpenAPI generator which handles both multiple servers and variable substitution

tyler2cr commented 3 years ago

Same

Copied from the docs:

servers:
  - url: '{protocol}://myapp.com'
    variables:
      protocol:
        enum:
          - http
          - https
        default: https
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.22</version>
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2020-11-02T12:34:38.995686-08:00[America/Los_Angeles]")@Component("myapp.ApiClient")
public class ApiClient {
    public enum CollectionFormat {
        CSV(","), TSV("\t"), SSV(" "), PIPES("|"), MULTI(null);

        private final String separator;
        private CollectionFormat(String separator) {
            this.separator = separator;
        }

        private String collectionToString(Collection<? extends CharSequence> collection) {
            return StringUtils.collectionToDelimitedString(collection, separator);
        }
    }

    private boolean debugging = false;

    private HttpHeaders defaultHeaders = new HttpHeaders();

    private String basePath = "{protocol}://myapp.com";

    private RestTemplate restTemplate;

    private Map<String, Authentication> authentications;

    private HttpStatus statusCode;
    private MultiValueMap<String, String> responseHeaders;

    private DateFormat dateFormat;

    public ApiClient() {
        this.restTemplate = buildRestTemplate();
        init();
    }

    @Autowired
    public ApiClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
        init();
    }

    protected void init() {
        // Use RFC3339 format for date and datetime.
        // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
        this.dateFormat = new RFC3339DateFormat();

        // Use UTC as the default time zone.
        this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        // Set default User-Agent.
        setUserAgent("Java-SDK");

        // Setup authentications (key: authentication name, value: authentication).
    authentications = new HashMap<String, Authentication>();
        // Prevent the authentications from being modified.
        authentications = Collections.unmodifiableMap(authentications);
    }

    /**
     * Get the current base path
     * @return String the base path
     */
    public String getBasePath() {
        return basePath;
    }

    /**
     * Set the base path, which should include the host
     * @param basePath the base path
     * @return ApiClient this client
     */
    public ApiClient setBasePath(String basePath) {
        this.basePath = basePath;
        return this;
    }
...