swagger-api / swagger-api/swagger-codegen

generate error api for rest template spring

Open
#12,199 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

I have api:

{
    "swagger": "2.0",
    "info": {
        "description": "",
        "version": "1.0.0",
        "title": "v2",
        "contact": {
            "name": "Aleksey2093",
            "email": ""
        },
        "license": {
            "name": "Apache 2.0",
            "url": "http://www.apache.org/licenses/LICENSE-2.0"
        }
    },
    "host": "localhost:8080",
    "basePath": "/api",
    "tags": [
        {
            "name": "files-controller",
            "description": "Files Controller"
        }
    ],
    "paths": {
        "/files/info-files": {
            "post": {
                "tags": [
                    "files-controller"
                ],
                "summary": "create or update",
                "operationId": "createInfoFileUsingPOST",
                "consumes": [
                    "multipart/form-data"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "in": "formData",
                        "name": "comment",
                        "description": "comment",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "file",
                        "in": "formData",
                        "description": "file",
                        "required": false,
                        "type": "file"
                    },
                    {
                        "in": "formData",
                        "name": "icon",
                        "description": "icon",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "in": "formData",
                        "name": "isVisible",
                        "description": "isVisible",
                        "required": true,
                        "schema": {
                            "type": "boolean"
                        }
                    },
                    {
                        "in": "formData",
                        "name": "uri",
                        "description": "uri remote",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "201": {
                        "description": "good",
                        "schema": {
                            "originalRef": "PayloadResponse",
                            "$ref": "#/definitions/PayloadResponse"
                        }
                    },
                    "400": {
                        "description": "Error",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    },
                    "403": {
                        "description": "Need Auth",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    },
                    "404": {
                        "description": "Not found",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    },
                    "422": {
                        "description": "Error",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    },
                    "500": {
                        "description": "Error",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    }
                },
                "responsesObject": {
                    "201": {
                        "description": "Good",
                        "schema": {
                            "originalRef": "PayloadResponse",
                            "$ref": "#/definitions/PayloadResponse"
                        }
                    },
                    "400": {
                        "description": "Error",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    },
                    "403": {
                        "description": "Need Auth",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    },
                    "404": {
                        "description": "Not found",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    },
                    "422": {
                        "description": "Error",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    },
                    "500": {
                        "description": "Error",
                        "schema": {
                            "originalRef": "ErrorBody",
                            "$ref": "#/definitions/ErrorBody"
                        }
                    }
                },
                "deprecated": false
            }
        }
    },
    "definitions": {
        "ErrorBody": {
            "type": "object",
            "required": [
                "message"
            ],
            "properties": {
                "description": {
                    "type": "string",
                    "description": "developer"
                },
                "errors": {
                    "type": "array",
                    "description": "Errors",
                    "items": {
                        "originalRef": "ErrorItem",
                        "$ref": "#/definitions/ErrorItem"
                    }
                },
                "message": {
                    "type": "string",
                    "description": "message for user"
                }
            },
            "title": "ErrorBody",
            "description": "ErrorBody"
        },
        "ErrorItem": {
            "type": "object",
            "required": [
                "message"
            ],
            "properties": {
                "description": {
                    "type": "string",
                    "description": "developer"
                },
                "field": {
                    "type": "string",
                    "description": "field name from request"
                },
                "message": {
                    "type": "string",
                    "description": "message for user"
                }
            },
            "title": "ErrorItem",
            "description": "Error"
        },
        "InputStream": {
            "type": "object",
            "title": "InputStream"
        },
        "PayloadResponse": {
            "type": "object",
            "required": [
                "message",
                "success"
            ],
            "properties": {
                "payload": {
                    "type": "object",
                    "description": "data"
                },
                "success": {
                    "type": "boolean",
                    "description": "success?"
                },
                "message": {
                    "type": "string",
                    "description": "message for user"
                }
            },
            "title": "PayloadResponse",
            "description": "response"
        }
    }
}

generate project for api:

java -DmodelTests=false -DapiTests=false -jar ./swagger-codegen-cli-3.0.35.jar generate \
        -i ./api-docs.json \
        -l java \
        --library resttemplate \
        --invoker-package aleksey2093.back.end.v2.api.invoker \
        --model-package aleksey2093.back.end.v2.api.model \
        --api-package aleksey2093.back.end.v2.api.controller \
        --group-id aleksey2093 \
        --artifact-id test \
        --artifact-version test \
        -o target/gen-swagger-api --additional-properties dateLibrary=java8-localdatetime,java8=true

generate error code:

public ResponseEntity<PayloadResponse> createInfoFileUsingPOSTWithHttpInfo(Object comment, File file, Object icon, Object isVisible, Object uri) throws RestClientException {
        Object postBody = null;
        // verify the required parameter 'comment' is set
        if (comment == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'comment' when calling createInfoFileUsingPOST");
        }
        // verify the required parameter 'file' is set
        if (file == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'file' when calling createInfoFileUsingPOST");
        }
        // verify the required parameter 'icon' is set
        if (icon == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'icon' when calling createInfoFileUsingPOST");
        }
        // verify the required parameter 'isVisible' is set
        if (isVisible == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'isVisible' when calling createInfoFileUsingPOST");
        }
        // verify the required parameter 'uri' is set
        if (uri == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'uri' when calling createInfoFileUsingPOST");
        }
        String path = UriComponentsBuilder.fromPath("/files/info-files").build().toUriString();

but comment, file and uri are not required fields.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the shown swagger-codegen-cli command with api-docs.json and compare its generated createInfoFileUsingPOSTWithHttpInfo method with the parameter required flags in the specification. Trace the Java RestTemplate generation entry point and related template, then verify that only parameters marked required are validated as required in the generated code.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.