swagger-api / swagger-api/swagger-codegen
[typescript-fetch] Provide runtime checks that constraints are enforced
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When generating a typescritpt-fetch client library, the api constraints are not enforced. Servers may return values that are non-conformant but the client library will NOT fail fast.
Suppose that we have a swagger def like this:
definitions:
StoreRating:
required:
- "store"
properties:
store:
type: "string"
description: "This is a store"
example: "Storey Storis Store"
rating:
type: "number"
description: "How much we like the store"
example: "0.881"
minimum: 0
maximum: 1
type: "object"
The following is generated:
/**
*
* @export
* @interface StoreRating
*/
export interface StoreRating {
/**
* This is a store
* @type {string}
* @memberof StoreRating
*/
store: string;
/**
* How much we like the store
* @type {number}
* @memberof StoreRating
*/
rating?: number;
}
This does not do validation of the constraints that are imposed in the API. It should
do the following validations:
- verify that storeRating.store is not null
- verify that storeRating.rating is null OR a number between 0-1
Maybe this could be done by generating getters and setters, like this:
/**
*
* @export
* @interface StoreRating
*/
export class StoreRating {
/**
* This is a store
* @type {string}
* @memberof StoreRating
*/
_store: string;
get store(): string {
return _store;
}
set store(store: string) {
if (!store)
throw new Error('Something bad happened');
this._store = store
}
/**
* How much we like the store
* @type {number}
* @memberof StoreRating
*/
_rating?: number;
get rating(): number {
return _rating;
}
set rating(rating: number) {
if (rating && (rating > 1 || rating < 0 ) )
throw new Error('Something bad happened');
this._rating = rating
}
}
Swagger-codegen version
$ java -jar ../swagger-codegen-cli.jar version
2.3.1
Swagger declaration file content or url
---
swagger: "2.0"
basePath: "/api"
paths:
/storeRating:
post:
responses:
200:
description: "Success"
schema:
$ref: "#/definitions/StoreRating"
operationId: "baz"
parameters:
tags:
- "predict"
info:
title: "Store Service"
version: "1.0"
produces:
- "application/json"
consumes:
- "application/json"
definitions:
StoreRating:
required:
- "store"
properties:
store:
type: "string"
description: "This is a store"
example: "Storey Storis Store"
rating:
type: "number"
description: "How much we like the store"
example: "0.881"
minimum: 0
maximum: 1
type: "object"
responses:
Command line used for generation
java -jar ../swagger-codegen-cli.jar generate -i /tmp/baz.yaml -l typescript-fetch
Steps to reproduce
- Run code generation
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the supplied Swagger definition through the typescript-fetch generator first, then inspect the generated StoreRating model and the generator entry point used by the command. Done means generated clients enforce required, type, minimum, and maximum constraints at runtime, with the behavior verified against the reproduction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100