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

fix default-flat-param-object doesn't work when using http body #2291

Merged
merged 1 commit into from
Jul 10, 2023

Conversation

NaccOll
Copy link
Contributor

@NaccOll NaccOll commented Jul 1, 2023

Related issue:

  1. default-flat-param-object doesn't work when using http body #2282
  2. default-flat-param-object does not take effect #2181

Related pr:

  1. fixed DefaultFlatParamObject to work with annotated parameters. Fixes… #2183

error behavior:
image

@PreAuthorize("hasPermission(0L,'GLOBAL','cms/column:add')")
@Operation(summary = "创建栏目")
@PostMapping
public CmsColumnDto create(@RequestBody CmsColumnCreateCommand command) {
     ...
}

command is a json object but converted to multiple query param.

@bnasslahsen
Copy link
Contributor

@NaccOll,

You should provide the complete code of your HelloController.
CmsColumnCreateCommand declaration is missing.

@NaccOll
Copy link
Contributor Author

NaccOll commented Jul 1, 2023

@bnasslahsen
Hold on, I'll provide a repository. But this is unnecessary, because the core of the problem is that the associated pr that Spring recognizes as a JSON object is incorrectly converted into a query object. This resulted in an error in the generated API docs

@NaccOll
Copy link
Contributor Author

NaccOll commented Jul 1, 2023

@bnasslahsen
image
This screenshot comes from this repository

package com.example.springdoc.defaultflatparam;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test1")
public class HelloController {

    @GetMapping
    public String test1(RequestObject object) {
        return null;
    }

    @PostMapping
    public String test2(@RequestBody RequestObject obj) {
        return null;
    }
}
package com.example.springdoc.defaultflatparam;

public class RequestObject {
    private String id;

    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

The same object is used for the GET method and the POST method of the same path, where the POST method notifies Spring that it should parse it from the HTTP request body by adding the RequestBody annotation to the parameter. But unfortunately, it was flattened into multiple query parameters, the reason is that when default-flat-param-object is true, it caused problems due to pr ignoring all annotations.

I'm actually confused, I'm the original developer of this feature (https://github.com/springdoc/springdoc-openapi/pull/1805/files), someone misused the feature and modified it, I'm correcting this mistake.

It is hoped that this error will be corrected relatively quickly. I also found another error because of default-support-form-data. When I realized the conversion between Scheme and Paramter, the array was set to null, which caused a null exception when I used this attribute later, but I checked the issue has been now fixed. I hope this pr can be released 2.1.1 after being merged, and fix these two problems of 2.1.0.

@bnasslahsen bnasslahsen merged commit 401371e into springdoc:main Jul 10, 2023
@xiaoymin
Copy link

xiaoymin commented Aug 5, 2023

2.1.1 When will it be released?

@kylerjensen
Copy link

@bnasslahsen Is there any chance this fix could be back-ported and released as a patch for v1? We're stuck on v1 while we work on our project's Spring Boot 3 upgrade and this issue is blocking us from being able to move forward.

@bnasslahsen
Copy link
Contributor

Hi @kylerjensen,

It's already backported in the supported version.
springdoc-openapi v1.7.0 is the latest Open Source release supporting Spring Boot 2.x and 1.x.
An extended support for springdoc-openapi v1 project is now available for organizations that need support beyond 2023.
For more details, feel free to reach out: [email protected]

@kylerjensen
Copy link

kylerjensen commented Nov 15, 2023

It's already backported in the supported version.
springdoc-openapi v1.7.0 is the latest Open Source release supporting Spring Boot 2.x and 1.x.

Hi @bnasslahsen, thanks for your quick response! I am using v1.7.0 and I am still experiencing this issue. I'm using springdoc.default-flat-param-object=true and all my @RequestBody-annotated method parameters are being converted to flattened query params. I'm using Spring Boot v2.3.0.RELEASE. Do you have any other suggestions? Adding @ParameterObject everywhere is a non-starter because of the size of the project.

@bnasslahsen
Copy link
Contributor

@kylerjensen,

As stated Earlier, if you need support for this version you should reach out to [email protected].

@kylerjensen
Copy link

Okay, as far as I can tell this fix is definitely not present in v1.7.0. Luckily, there is a fork with this fix backported. For anyone coming here looking for a fix for v1, I was able to make it work in my Gradle project through JitPack:

repositories {
  mavenCentral()
  maven { url 'https://jitpack.io' }
}

dependencies {
  implementation 'com.github.eshizhan.springdoc-openapi:springdoc-openapi-ui:e986414ba5'
  implementation 'com.github.eshizhan.springdoc-openapi:springdoc-openapi-webmvc-core:e986414ba5'
  // ...
}

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

Successfully merging this pull request may close these issues.

4 participants