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

The generated openapi.json file does not correctly assign the format property to path parameters of the interfaces. #578

Closed
FeiyuEVE opened this issue Aug 22, 2023 · 19 comments
Labels
bug Something isn't working
Milestone

Comments

@FeiyuEVE
Copy link

FeiyuEVE commented Aug 22, 2023

Your Environment(您的使用环境)

  • smart-doc version: 2.7.5
  • plugin version (e.g. smart-doc-maven-plugin or smart-doc-gradle-plugin): smart-doc-maven-plugin
  • build tool version(maven or gradle): maven

Expected Behavior(您期望的结果)

Under springboot 2.7.1

@PostMapping("/{id}/update")
public Result< Void > update(@PathVariable String id) {

}

Using smart-doc to generate openapi.json, the description of this interface in

"parameters": {
  "shema" {
    "format": "string",
    "type": "string"
  }
}

Causes the function parameter id to be of String type in the springboot client request generated through this openapi.json and openapi-generator

Current Behavior(当前结果)

Under springboot 2.7.1

@PostMapping("/{id}/update")
public Result< Void > update(@PathVariable String id) {

}

Using smart-doc to generate openapi.json, the description of this interface in

"parameters": {
  "shema" {
    "format": "integer",
    "type": "string"
  }
}

The issue results in the parameter id being generated as an Integer type in the Spring Boot client requests when utilizing openapi.json alongside the openapi-generator.

@FeiyuEVE FeiyuEVE added the bug Something isn't working label Aug 22, 2023
@abing22333
Copy link
Contributor

abing22333 commented Aug 23, 2023

i can't reproduce the bug

    "schemas": {
      "string": {
        "format": "string",
        "type": "string"
      }
    }

code:

@RestController("test")
public class TestController {
    /**
     * 这个是一个更新接口
     *
     * @param id id
     * @return string
     */
    @PostMapping("/{id}/update")
    public String update(@PathVariable String id) {
        return null;
    }
}

pom.xml :

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.1</version>
        </dependency> 
    </dependencies>
<plugins>
    <plugin>
        <groupId>com.github.shalousun</groupId>
        <artifactId>smart-doc-maven-plugin</artifactId>
        <version>2.7.5</version>
        <configuration>
            <configFile>./src/main/resources/smart-doc.json</configFile>
        </configuration>
    </plugin>
</plugins> 

smart-doc.json:

{
  "outPath": "target/doc"
}

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

I'll try again

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

can you debug the plugin?I want to add a breat on the following function

    public Map<String, Object> buildParametersSchema(ApiParam apiParam) {
        Map<String, Object> schema = new HashMap<>(10);
        String openApiType = DocUtil.javaTypeToOpenApiTypeConvert(apiParam.getType());
        schema.put("type", openApiType);
        if ("file".equals(apiParam.getType())) {
            schema.put("format", "binary");
            schema.put("type", "string");
        } else if ("object".equals(openApiType)) {
            if ("enum".equals(apiParam.getType())) {
                schema.put("enum", apiParam.getEnumValues());
            }
        } else if (ARRAY.equals(apiParam.getType())) {
            if (CollectionUtil.isNotEmpty(apiParam.getEnumValues())) {
                schema.put("type", "string");
                schema.put("items", apiParam.getEnumValues());
            } else {
                schema.put("type", ARRAY);
                Map<String, String> map = new HashMap<>(4);
                map.put("type", "string");
                map.put("format", "string");
                schema.put("items", map);
            }
        } else {
            schema.put("type", apiParam.getType());
            schema.put("format", "integer");
        }
        return schema;
    }

@abing22333
Copy link
Contributor

abing22333 commented Aug 23, 2023

You can reference this document https://smart-doc-group.github.io/#/zh-cn/plugins/maven-plugin-debug

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

The breakpoint has been hit at the function I mentioned previously. For the apiParam, the type attribute is string. Since none of the preceding if statements check for type being specifically string, the flow falls through to the final else clause, where it incorrectly sets type back to string and assigns format the value integer.

@abing22333
Copy link
Contributor

abing22333 commented Aug 23, 2023

What this @PostMapper annotation? I haven't cross it before

generated by me

 "components": {
    "schemas": {
      "string": {
        "format": "string",
        "type": "string"
      }
    }
  }

It seems like my reference was to parameters, which makes me think my configuration might not be correct.

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

my mistake in the issue, I meant the @PostMapping annotation

@abing22333
Copy link
Contributor

abing22333 commented Aug 23, 2023

my mistake in the issue, I meant the @PostMapping annotation

can you provide the configuration for smart-doc please?

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

{
"serverUrl",
"outPath",
"isStrict": false,
"allInOne": true,
"coverOld": true,
"allInOneDocFileName": "index.html",
"packageFilters",
"style":"xt256",
"inlineEnum": true,
"responseBodyAdvice": {
"className": "com.test.test.model.vo.Result"
}
}

Result is a unified wrapper class for return values.

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

Indeed, there is a "string" defined in the "components". However, the "parameters" I mentioned are situated within the properties indexed by the interface's name. Namely,

“/test/{id}/update”{
"post":{
"parameters":[
{
"schema": {
"format": "integer",
"type":"string"
}
}
]
}
}

@abing22333
Copy link
Contributor

abing22333 commented Aug 23, 2023

I'm not overly familiar with generating OpenAPI specifications myself, and my OpenAPI output doesn't even include the details you've mentioned.
我对生成openapi也不太熟,我的openapi甚至都没有你的这个输出

@FeiyuEVE
Copy link
Author

029AAA61

@abing22333
Copy link
Contributor

abing22333 commented Aug 23, 2023

029AAA61

If it's convenient, you can upload the code to GitHub, and I can pull the code from there.
如果方便的话,你可以把代码上传到github,我拉代码

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

Alright, I'll set up a project to test it. Please give me a moment. Thank you for your patience.

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

I might not be able to upload the code to GitHub easily due to company policies. Instead, I forked your smart-doc-demo repository. Then, I adjusted the Spring Boot version to 2.7.1, set the Java version to 1.8, and updated the smart-doc version to 2.7.5, keeping all other configurations unchanged. Afterwards, I made adjustments to the code.
我在公司可能不太好往github传代码
我fork了你的smart-doc-demo
然后调整Springboot版本为2.7.1,java版本为1.8,smart-doc版本为2.7.5,其他配置不变
调整代码:

import javax.annotation.Resource;
import javax.validation.Valid;

@PostMapping("/api/v1/{id}/users")
public ResponseResult<User> createUser(@PathVariable String id, @Valid @RequestBody User user) {
     userRepository.add(user);
     return ResponseResult.ok();
 }

然后生成的openapi文档里:
    "/api/v1/{id}/users": {
      "post": {
        "summary": "Create user.",
        "deprecated": false,
        "description": "Create user.",
        "tags": [
          "The type User controller."
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/User_request"
              },
              "examples": {
                "json": {
                  "summary": "test data",
                  "value": "{\n  \"id\": 1,\n  \"firstName\": \"\",\n  \"lastName\": \"\",\n  \"email\": \"\"\n}"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "*/*": {
                "schema": {
                  "$ref": "#/components/schemas/ResponseResultUser_response"
                },
                "examples": {
                  "json": {
                    "summary": "test data",
                    "value": "{\n  \"success\": true,\n  \"status\": \"200\",\n  \"message\": \"\",\n  \"data\": {\n    \"id\": 1,\n    \"firstName\": \"\",\n    \"lastName\": \"\",\n    \"email\": \"\"\n  },\n  \"timestamp\": \"\"\n}"
                  }
                }
              }
            }
          }
        },
        "operationId": "api-v1-{id}-users-POST",
        **"parameters": [
          {
            "schema": {
              "format": "integer",
              "type": "string"
            },**
            "in": "path",
            "description": "No comments found.",
            "required": true,
            "name": "id"
          }
        ]
      }
    }

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

There is a field like this, inside which lies the schema I mentioned.

"parameters": [
{
"schema": {
"format": "integer",
"type": "string"
},

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 23, 2023

@abing22333
Copy link
Contributor

abing22333 commented Aug 23, 2023

https://github.com/FeiyuEVE/smart-doc-demo/tree/codespace-effective-giggle-r9wrxx5w4525469 The modifications are in this branch now.

reproduce it,however ,I'm not quit familiar with OpenAPI yet; I'll go and learn more about it first

@FeiyuEVE
Copy link
Author

FeiyuEVE commented Aug 24, 2023

shalousun added a commit that referenced this issue Aug 24, 2023
@shalousun shalousun added this to the 2.7.6 milestone Sep 2, 2023
@shalousun shalousun changed the title 生成的openapi.json中接口的路径参数没有正确的赋予format The generated openapi.json file does not correctly assign the format property to path parameters of the interfaces. May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants