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.75k stars 6.56k forks source link

[C++][Pistache-server] string with pattern (regex) is not treated #1170

Open CyrilleBenard opened 6 years ago

CyrilleBenard commented 6 years ago
Description

The generator does not consider the pattern "inside" a string type. I mean, when the openapi file describes the below syntax :

    type: string
    pattern: '^(pat-[0-9]+)$'

The generator does not take into account the pattern restriction. No dedicated code is generated

openapi-generator version

Current master : 3.3.1-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Check generation of string + pattern 
  description: Internal ref filename is check_pattern.yaml 

servers:
  - url: http://localhost:8080

paths:
  /stair1:
    post:
      summary: blabla
      operationId: check_generation
      tags:
        - Stair1
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Content'
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                type: string
        default:
          description: Unexpected error

components:
  schemas:
    Content:
      type: string
      pattern: '^(pat-[0-9]+)$'
Command line used for generation

Generate :

openapi-generator-cli.sh generate -i ./openapi.yaml -g cpp-pistache-server -c ./config.json -o .

Compile :

g++ -c  -I./api -I./model -I./impl -Wall -g -std=c++11 -o obj/api/Stair1Api.o api/Stair1Api.cpp
Steps to reproduce

Generate & compile

Related issues/PRs

N/A

Suggest a fix/enhancement

Each variable should be validated with the pattern before any other treatment, in case there is a pattern to apply, of course. If the string (other type ?) does not match, return a dedicated HTTP error code 4xx : 412 ? 422 ? or the "generic" 400

As the pattern is regex compliant, the C++ std::regex lib may be used and the pattern transmitted as this to the std::regex_match method.

tcjcodes commented 5 years ago

Having the same problem for Java code generation, using: org.openapitools:openapi-generator-gradle-plugin:3.3.2

myField:
  type: string
  pattern: '[0-9]{10,11}'
  example: '12223334444'

Also tried pattern '/[0-9]{10,11}/'

develGeorge commented 5 years ago

I have created a patch providing a const validator doing pattern validation via std::regex at strings and range checks at numbers throwing an exception. Is someone already fixing it or should I share my draft ;-)

CyrilleBenard commented 5 years ago

By default, I would have said : "Share it" :)

haibinzero commented 5 years ago

Pull it :)

develGeorge commented 5 years ago

It is really tricky to provide a clean solution since arrays are not explicit generated to a model and so all validation items of an array type has to be passed using root nodes at java.

develGeorge commented 5 years ago

next issue is that at the moment any value can be returned by design.

develGeorge commented 5 years ago

this is a draft. I am not really satisfied with it. But it works fine. 0000-CPistacheServer-remove-not-generated-but-included-file-Objecth.patch.txt

0001-CPistacheServer-added-validation-stringpattern-and-numericminimummaximum-from-yaml-1170.patch.txt

flame on ;-)