Skip to content

Commit

Permalink
feat(http): use smarter reparenting
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Feb 4, 2025
1 parent 0d28cd2 commit ff615cd
Show file tree
Hide file tree
Showing 8 changed files with 769 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/http-server/src/__tests__/server.oas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fetch, { RequestInit } from 'node-fetch';
import { createServer } from '../';
import { ThenArg } from '../types';

const logger = createLogger('TEST', { enabled: false });
const logger = createLogger('TEST', { enabled: true });

const oas3File = ['petstore.no-auth.oas3.yaml', 'petstore.no-auth.circular.oas3.yaml'];
const oas2File = ['petstore.no-auth.oas2.yaml', 'petstore.no-auth.circular.oas2.yaml'];
Expand Down
23 changes: 19 additions & 4 deletions packages/http/src/mocker/generator/JSONSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import faker from '@faker-js/faker';
import { cloneDeep } from 'lodash';
import { JSONSchema } from '../../types';

import { JSONSchemaFaker } from 'json-schema-faker';
Expand All @@ -8,6 +7,8 @@ import { Either, toError, tryCatch } from 'fp-ts/Either';
import { IHttpContent, IHttpOperation, IHttpParam } from '@stoplight/types';
import { pipe } from 'fp-ts/function';
import * as E from 'fp-ts/lib/Either';
import * as A from 'fp-ts/Array';
import * as R from 'fp-ts/Record';
import { stripWriteOnlyProperties } from '../../utils/filterRequiredProperties';

// necessary as workaround broken types in json-schema-faker
Expand Down Expand Up @@ -74,9 +75,23 @@ export function generate(
E.fromOption(() => Error('Cannot strip writeOnly properties')),
E.chain(updatedSource =>
tryCatch(
// necessary as workaround broken types in json-schema-faker
// @ts-ignore
() => sortSchemaAlphabetically(JSONSchemaFaker.generate({ ...cloneDeep(updatedSource), __bundled__: bundle })),
() =>
pipe(
{
...JSON.parse(JSON.stringify(updatedSource)),
__bundled__: bundle,
},
targetSchema =>
pipe(
Object.getOwnPropertyDescriptors(source),
R.toEntries,
A.filter(([, descriptor]) => !descriptor!.enumerable),
R.fromEntries,
descriptors => Object.defineProperties(targetSchema, descriptors as Record<string, PropertyDescriptor>)
),
JSONSchemaFaker.generate,
sortSchemaAlphabetically
),
toError
)
),
Expand Down
316 changes: 316 additions & 0 deletions packages/http/src/utils/__tests__/__fixtures__/schema-refs.oas3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
---
openapi: 3.0.0
info:
description: 'This is a sample server Petstore server. You can find out more about Swagger
at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
version: 1.0.0
title: Swagger Petstore
termsOfService: http://swagger.io/terms/
contact:
email: [email protected]
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: http://swagger.io
- name: store
description: Access to Petstore orders
- name: user
description: Operations about user
externalDocs:
description: Find out more about our store
url: http://swagger.io
paths:
"/pets":
post:
tags:
- pet
summary: Add a new pet to the store
description: ''
operationId: addPet
requestBody:
"$ref": "#/components/requestBodies/Pet"
responses:
'405':
description: Invalid input
security:
- petstore_auth:
- write:pets
- read:pets
put:
tags:
- pet
summary: Update an existing pet
description: ''
operationId: updatePet
requestBody:
"$ref": "#/components/requestBodies/Pet"
responses:
'400':
description: Invalid ID supplied
'404':
description: Pet not found
'405':
description: Validation exception
security:
- petstore_auth:
- write:pets
- read:pets
"/pets/{petId}":
get:
tags:
- pet
summary: Find pet by ID
description: Returns a single pet
operationId: getPetById
parameters:
- name: petId
in: path
description: ID of pet to return
required: true
schema:
$ref: "#/components/schemas/Id"
responses:
'200':
description: successful operation
content:
application/json:
schema:
"$ref": "#/components/schemas/Pet"
'400':
description: Invalid ID supplied
'404':
description: Pet not found
'418':
description: teapot response
content:
application/json:
schema:
"$ref": "#/components/schemas/Pet"
default:
description: default response
content:
application/json:
schema:
required:
- id
- code
- message
properties:
id:
$ref: "#/components/schemas/Id"
code:
type: integer
format: int32
message:
type: string
security:
- api_key: []
put:
tags:
- pet
summary: Updates a pet in the store with form data
description: ''
operationId: updatePetWithForm
parameters:
- name: petId
in: path
description: ID of pet that needs to be updated
required: true
schema:
type: integer
format: int64
requestBody:
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
name:
description: Updated name of the pet
type: string
status:
description: Updated status of the pet
type: string
responses:
'405':
description: Invalid input
security:
- petstore_auth:
- write:pets
- read:pets
delete:
tags:
- pet
summary: Deletes a pet
description: ''
operationId: deletePet
parameters:
- name: api_key
in: header
required: false
schema:
type: string
- name: petId
in: path
description: Pet id to delete
required: true
schema:
$ref: "#/components/schemas/Id"
responses:
'400':
description: Invalid ID supplied
'404':
description: Pet not found
security:
- petstore_auth:
- write:pets
- read:pets
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
servers:
- url: https://petstore.swagger.io/v2
- url: http://petstore.swagger.io/v2
components:
requestBodies:
UserArray:
content:
application/json:
schema:
type: array
items:
"$ref": "#/components/schemas/User"
description: List of user object
required: true
Pet:
content:
application/json:
schema:
"$ref": "#/components/schemas/Pet"
description: Pet object that needs to be added to the store
required: true
securitySchemes:
petstore_auth:
type: oauth2
flows:
implicit:
authorizationUrl: http://petstore.swagger.io/oauth/dialog
scopes:
write:pets: modify pets in your account
read:pets: read your pets
api_key:
type: apiKey
name: api_key
in: header
schemas:
Id:
type: integer
format: int64
Order:
type: object
properties:
id:
$ref: "#/components/schemas/Id"
petId:
$ref: "#/components/schemas/Id"
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
status:
type: string
description: Order Status
enum:
- placed
- approved
- delivered
complete:
type: boolean
default: false
xml:
name: Order
Category:
type: object
properties:
id:
$ref: "#/components/schemas/Id"
name:
type: string
xml:
name: Category
User:
type: object
properties:
id:
$ref: "#/components/schemas/Id"
username:
type: string
firstName:
type: string
lastName:
type: string
email:
type: string
password:
type: string
phone:
type: string
userStatus:
type: integer
format: int32
description: User Status
xml:
name: User
Tag:
type: object
properties:
id:
$ref: "#/components/schemas/Id"
name:
type: string
xml:
name: Tag
Pet:
type: object
required:
- name
- photoUrls
properties:
id:
$ref: "#/components/schemas/Id"
category:
"$ref": "#/components/schemas/Category"
name:
type: string
example: doggie
photoUrls:
type: array
xml:
name: photoUrl
wrapped: true
items:
type: string
tags:
type: array
xml:
name: tag
wrapped: true
items:
"$ref": "#/components/schemas/Tag"
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
xml:
name: Pet
Loading

0 comments on commit ff615cd

Please sign in to comment.