Skip to content
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

[BUG][Java] Content mediatype is hardcoded in api.mustache #11511

Open
p-daniil opened this issue Feb 3, 2022 · 2 comments
Open

[BUG][Java] Content mediatype is hardcoded in api.mustache #11511

p-daniil opened this issue Feb 3, 2022 · 2 comments

Comments

@p-daniil
Copy link

p-daniil commented Feb 3, 2022

Hi!
I'm trying to return String value.
My OAS looks like this:

openapi: 3.0.0
info:
  title: API
  description: API
  version: LATEST
tags:
  - name: helloWorld
    description: Hello World Api
paths:
  /helloWorld:
    get:
      tags:
        - helloWorld
      responses:
        '200':
          description: OK
          content:
            'text/plain':
              schema:
                type: string
      parameters:
        - name: name
          in: query
          schema:
            type: string

But it results in this method:

/**
     * GET /helloWorld
     *
     * @param name  (optional)
     * @return OK (status code 200)
     */
    @Operation(summary = "", tags={ "helloWorld", }, responses = {  @ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  String.class))) })
        @RequestMapping(
        method = RequestMethod.GET,
        value = "/helloWorld",
        produces = { "text/plain" }
    )
    default ResponseEntity<String> helloWorldGet(@Parameter(name = "name", description = "") @Valid @RequestParam(value = "name", required = false) String name

) {
        return getDelegate().helloWorldGet(name);
    }

As you can see, @Content(mediaType = "application/json"...
Root of the problem is in this line, where mediatype is hardcoded as application/json:

@ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{#baseType}}, content = @Content(mediaType = "application/json", schema = @Schema(implementation = {{{baseType}}}.class)){{/baseType}}){{^-last}},{{/-last}}

As a temporary solution, it can be replaced:
mediaType = "application/json"
on
mediaType = {{#produces}}"{{{mediaType}}}"{{/produces}}{{^produces}}"application/json"{{/produces}}

But mediaType can contain multiple values, separated with comma, because it used in @RequestMapping(produces={...}).
True way to support multiple content types will be generate @Content annotation in array, I suppose, because in @ApiResponse annotation content is type of array:

/**
     * An array containing descriptions of potential response payloads, for different media types.
     *
     * @return array of content
     **/
    Content[] content() default {};
@p-daniil p-daniil changed the title Content mediatype is hardcoded in api.mustache [BUG] Content mediatype is hardcoded in api.mustache Feb 3, 2022
@p-daniil p-daniil changed the title [BUG] Content mediatype is hardcoded in api.mustache [BUG][Java] Content mediatype is hardcoded in api.mustache Feb 3, 2022
cachescrubber added a commit to cachescrubber/openapi-generator that referenced this issue Feb 5, 2022
wing328 pushed a commit that referenced this issue Feb 5, 2022
* Content mediatype is hardcoded in api.mustache #11511

* Generate Samples

* OAS3 incorrect data type when providing a default value #11367

* Generate Samples

* Fix JsonTypeName annotation handling in Java and JavaSpring

* Generate Samples

* getIsClassnameSanitized: use null safe equals
wing328 added a commit that referenced this issue Mar 16, 2022
…11650)

* Fix #5381
added x-is-one-of-interface extension for oneOf interface in mustache
template

* Fix #5381
fixed name of model from UNKNOWN_BASE_TYPE to right one in api: operationId + OneOf

Fix #5381
parcelableModel is not required

* Fix #5381
removed not needed methods

* Fix #5381
catch NPE cases in preprocessOpenAPI
updated samples

* Fix #5381
fixed generation of oneOf Models

* Fix #5381
addOneOfInterfaceModel only for cases when useOneOfInterfaces is true and for spring

* Fix #5381
NPE fix

* Fix #5381
spring: fixed use of oneOf Models in API

* Fix #5381
implementing oneOf for spring lib overriding methods with different behavior from default

* Fix #5381
added x-is-one-of-interface extension for oneOf interface in mustache
template

* Fix #5381
fixed name of model from UNKNOWN_BASE_TYPE to right one in api: operationId + OneOf

Fix #5381
removed not needed methods

Fix #5381
fixed generation of oneOf Models

Fix #5381
addOneOfInterfaceModel only for cases when useOneOfInterfaces is true and for spring

Fix #5381
NPE fix for tests

* Fix #5381
fixed handing of composed schema with array

* Fix #5381
fixed NPE in addOneOfInterfaceModel

* Fix #5381
fixed generation of oneOf models with descriminator

* Initial merge of 5.0

* Aligned with master formatting

* Corrected spacing for class names to align with samples.

* Merged master

* Updated samples

* Consolidate methods from JavaClient and SpringCodegen (mov up to AbstractJavaCodegen)

* set useLegacyDiscriminator to false, format templates

* Suport JsonTypeName, fq class name for spring.io.Resource

* Generate Samples

* Test full qualified usage of the spring Resource interface.

* Add java-camel to samples.circleci.spring profile

* Add more complex example combining inheritance and oneof-interface

* Remove x-implements Serializable from JavaClientCodegen (moved to AbstractJavaCodegen)

* Fix spacing before opening brace after extends/implements

* Generate Samples

* Add more complex example combining inheritance and oneof-interface

* Generate Samples

* Fix JsonTypeName annotation handling in Java and JavaSpring

* Content mediatype is hardcoded in api.mustache #11511

* Generate Samples

* OAS3 incorrect data type when providing a default value #11367

* Generate Samples

* Fix JsonTypeName annotation handling in Java and JavaSpring

* Generate Samples

* getIsClassnameSanitized: use null safe equals

* Fix JsonTypeName annotation handling in Java and JavaSpring (merge)

* Generate Samples

* Generate Samples

* Add oneof sample

* Generate Samples

* Giv example oas spec a meaningful name, demo usage of oneOf in Model

* Generate Samples

* Remove unnecessary JsonTypeName include, add example for JsonTypeName (Bar_Create)

* Generate Samples

* Generate Samples

Co-authored-by: Alexej <[email protected]>
Co-authored-by: JBurgess <[email protected]>
Co-authored-by: William Cheng <[email protected]>
@mattdarwin
Copy link

I think the PR introduces a new bug.

Consider the following spec extract:

  responses:
    '200':
      description: OK
      content:
        'text/plain':
          schema:
            type: string
    '403':
      description: NOTOK
      content:
        'application/json':
          schema:
            $ref: '#/components/schema/SomeObject' 

The resulting code should have a single content type for each response, like this:

@ApiResponse(responseCode = "200", 
description = "OK", content = @Content(mediaType = "text/plain", schema = @Schema(implementation =  String.class))) ,
@ApiResponse(responseCode = "403", 
description = "NOTOK", content = @Content(mediaType = "application/json", schema = @Schema(implementation =  SomeObject.class)))
})

But in fact it gives each ApiResponse all the content types:

@ApiResponse(responseCode = "200", 
description = "OK", content = { @Content(mediaType = "text/plain", schema = @Schema(implementation =  String.class))
                                @Content(mediaType = "application/json", schema = @Schema(implementation =  SomeObject.class))}) ,
@ApiResponse(responseCode = "403", 
description = "NOTOK", content = { @Content(mediaType = "text/plain", schema = @Schema(implementation =  String.class))
                                   @Content(mediaType = "application/json", schema = @Schema(implementation =  SomeObject.class))}) 
})

@araghuraman1

This comment was marked as outdated.

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

3 participants