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

Feature: Document generator #16

Merged
merged 9 commits into from
Jul 1, 2022
Merged

Feature: Document generator #16

merged 9 commits into from
Jul 1, 2022

Conversation

oscar60310
Copy link
Contributor

@oscar60310 oscar60310 commented Jun 22, 2022

Description

Generate OAS document for our restful API.

Demo
openapi: 3.0.3
info:
  title: An API schema for testing
  version: 1.2.3
  description: Some description with **markdown** supported.
paths:
  /user/{id}:
    get:
      description: Get user information
      responses:
        '200':
          description: The default response
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    description: The unique-id of the user
                    type: string
                  username:
                    description: The username of the user
                    type: string
                  age:
                    description: The age of the user
                    type: number
                  groups:
                    description: The groups that the user has joined
                    type: object
                    properties:
                      id:
                        description: The unique-id of the group
                        type: string
                      groupName:
                        description: The groupName of the group
                        type: string
                      public:
                        description: Whether the group was public
                        type: boolean
                    required:
                      - id
                      - groupName
                required:
                  - id
                  - username
            text/csv:
              schema:
                type: string
        '400':
          description: Client error
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: string
                  message:
                    type: string
              examples:
                USER_NOT_FOUND:
                  description: We can't find any user with the provided id
                  value:
                    code: USER_NOT_FOUND
                    message: We can't find any user with the provided id
                AGENT_NOT_ALLOW:
                  description: The agent is not allow
                  value:
                    code: AGENT_NOT_ALLOW
                    message: The agent is not allow
        5XX:
          description: Server error
      parameters:
        - name: id
          in: path
          schema:
            type: string
            minLength: 3
            maxLength: 10
            pattern: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
          required: true
        - name: agent
          in: header
          schema:
            type: number
            minimum: 10
            maximum: 15
          required: false
        - name: force
          in: query
          schema:
            type: boolean
          required: false
        - name: username
          in: query
          schema:
            type: string
            enum:
              - ivan
              - eason
              - freda
          required: false
  /user/{id}/order/{oid}:
    get:
      responses:
        '200':
          description: The default response
          content:
            application/json:
              schema:
                type: object
                properties: {}
            text/csv:
              schema:
                type: string
        5XX:
          description: Server error
      parameters:
        - name: id
          in: path
          schema:
            type: string
          required: true
        - name: oid
          in: path
          schema:
            type: string
          required: true
  /users:
    get:
      responses:
        '200':
          description: The default response
          content:
            application/json:
              schema:
                type: object
                properties: {}
            text/csv:
              schema:
                type: string
        5XX:
          description: Server error
      parameters: []

We extract these values for the schema to document:

  1. Project config
    • Version
    • Name
    • Description
  2. API information
    • URL
    • Description
    • Error codes
  3. Parameters
    • Type
    • Description
    • Validators
    • Name
  4. Response
    • Name
    • Type
    • Description

About Validators' Constraints

For generating documents, we need to know what the validator do inside validateData function, so I add a method getConstraints to the validator interface, it should tell us what it does with some standard rules:

export class StringValidator implements IValidator {
  public name = 'StringValidator';
  public validateSchema(args: any): boolean {
    return true;
  }
  public validateData(data: string, args: any): boolean {
    if (data.length > args.maxLength || data.length < args.minLength)
      return false;
    return true;
  }
  public getConstraints(args: any) {
    return [
      Constraint.MaxLength(args.maxLength),
      Constraint.MinLength(args.minLength),
    ];
  }
}

According to Meeting #8
Instead of hard-cord constraints in this function, we can use some transform libraries like joi-to-json-schema. But I think we can do it later after previous PRs merged.

Duplicated codes

d61b0d2 is a duplicated commit from previous PR, it'll be removed after rebased.

Snapshots

Document generated by redoc
image

@oscar60310 oscar60310 force-pushed the feature/document-generator branch from 1a5f0b8 to 3e151e7 Compare June 22, 2022 10:03
@oscar60310 oscar60310 force-pushed the feature/path-alias-replacement branch from 6607919 to 32d8b72 Compare June 22, 2022 10:11
@oscar60310 oscar60310 force-pushed the feature/document-generator branch from 3e151e7 to 74cc97b Compare June 22, 2022 10:11
@oscar60310 oscar60310 changed the title [WIP] Feature: Document generator Feature: Document generator Jun 28, 2022
@oscar60310 oscar60310 marked this pull request as ready for review June 28, 2022 08:32
@oscar60310 oscar60310 requested review from kokokuo and wwwy3y3 June 28, 2022 08:32
Copy link
Contributor

@kokokuo kokokuo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beside one question, other LGTM 👍 👍

// Arrange
const generator = await getGenerator();
// Act, Arrange
const spec = generator.getSpec();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does here miss the assert behavior, not see the expect checker, e.g: expect(xxxx).not.toThrow() for not throwing error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, added it.

@oscar60310 oscar60310 force-pushed the feature/path-alias-replacement branch 2 times, most recently from 48db8eb to 67598d7 Compare July 1, 2022 10:05
- Add data type generator
- Add data type normalizer
- Add fieldIn normalizer
- Add pathparameters generator
- Add required validator auto creator
@oscar60310 oscar60310 force-pushed the feature/document-generator branch from d3f5eb3 to e962e25 Compare July 1, 2022 10:19
- Add constraints API for validators, implemented Required and MinValue constraint for
demonstration.
- Implemented schema middleware to extract constraints.
- Update generator and normalizer for new responses.
- Define response interfaces.
- Generate response of OAS document.
add MaxValue, MinLength, MaxLength, Regex, Enum constraints
@oscar60310 oscar60310 force-pushed the feature/document-generator branch from e962e25 to 7149563 Compare July 1, 2022 10:21
@oscar60310
Copy link
Contributor Author

Hi @kokokuo , I've fixed all issues.

Base automatically changed from feature/path-alias-replacement to develop July 1, 2022 10:30
@kokokuo kokokuo merged commit 5e71b5a into develop Jul 1, 2022
@kokokuo kokokuo deleted the feature/document-generator branch July 1, 2022 10:30
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.

2 participants