OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.4k stars 6.48k forks source link

[BUG] [Spring] -Dmodels="" causes different API generated output #1886

Open demtnt opened 5 years ago

demtnt commented 5 years ago
Description

I would like to generate spring server only API (with out model). And I have found out that generated V1Api.java interface have different content depending of presens system property -Dmodels=""

openapi-generator version

3.3.4

OpenAPI declaration file content
openapi: 3.0.1
info:
  title: asd
  version: "1.2"

paths:
  /v1/cancellation-authorisations:
    get:
      summary: aaa
      description: bbb
      operationId: methodGet

      responses:
        '200':
          description: OK

          content:
            application/json:
              schema:
                description: descr resource.
                type: array
                items:
                  $ref: "#/components/schemas/cancellationId"
components:
  schemas:
    cancellationId:
      description: Identification for cancellation resource
      type: string
Command line used for generation

java -jar openapi-generator-cli.jar generate -g spring -Dapis="",supportingFiles="ApiUtil.java" --additional-properties sourceFolder=,interfaceOnly=true -i /path/to/my/yaml -v -o output_path or java -jar openapi-generator-cli.jar generate -g spring -Dapis="",supportingFiles="ApiUtil.java",models="" --additional-properties sourceFolder=,interfaceOnly=true -i /path/to/my/yaml -v -o output_path

Steps to reproduce

run 2 commands and compare method "methodGet" signature in V1Api.java

import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Validated
@Api(value = "v1", description = "the v1 API")
public interface V1Api {

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

    @ApiOperation(value = "aaa", nickname = "methodGet", notes = "bbb", response = String.class, responseContainer = "List", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK", response = String.class, responseContainer = "List") })
    @RequestMapping(value = "/v1/cancellation-authorisations",
        produces = { "application/json" }, 
        method = RequestMethod.GET)
    default ResponseEntity<List<String>> methodGet() {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    ApiUtil.setExampleResponse(request, "application/json", "null");
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

}

and

package lv.citadele.psd2.api;

import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Validated
@Api(value = "v1", description = "the v1 API")
public interface V1Api {

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

    @ApiOperation(value = "aaa", nickname = "methodGet", notes = "bbb", response = String.class, responseContainer = "List", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK", response = String.class, responseContainer = "List") })
    @RequestMapping(value = "/v1/cancellation-authorisations",
        produces = { "application/json" }, 
        method = RequestMethod.GET)
    default ResponseEntity<List<CancellationId>> methodGet() {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    ApiUtil.setExampleResponse(request, "application/json", "null");
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

}
auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

jimschubert commented 5 years ago

This is possibly affecting all generators, as model exclusion is done in DefaultGenerator and setting -Dmodels="" appears to not only prevent models from being written but also prevents them from being processed into the internal structures we use for API generation.