Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jazzapple opened this issue Sep 29, 2020 · 1 comment

Comments

@jazzapple
Copy link

jazzapple commented Sep 29, 2020

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
Copy link

tyler2cr commented Nov 2, 2020

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;
    }
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants