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

[TS][Fetch] Add support for openapi maps/dictionaries to be generated as typescript map #2913

Merged

Conversation

ehvattum
Copy link
Contributor

@ehvattum ehvattum commented May 16, 2019

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

cc. @TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @nicokoenig @topce

Description of the PR

	Change isContainer -> isListContainer for existing array support.
	Add isMapContainer control flow, adding map support.
	Add utility function to help map openapi map/dictionaries to ts maps.
	Close OpenAPITools#1878
@macjohnny
Copy link
Member

can you provide an minimal example .yml file and the corresponding output?

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

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

LGTM

@ehvattum
Copy link
Contributor Author

@macjohnny Given an example 2.0 spec in json as the first code block here, this PR allows generation of code files like the bottom block.

{
    "swagger": "2.0",
    "info": { "version": "1.0", "title": "map-spec 1.0" },
    "paths":{},
    "definitions": {
        "Measurements": {
            "type": "object",
            "properties": {
                "meteringPointId": { "type": "string" },
                "year": { "$ref": "#/definitions/AggregatedMeasurements" },
                "month": { "type": "object", "additionalProperties": { "$ref": "#/definitions/AggregatedMeasurements" } },
                "day": { "type": "object", "additionalProperties": { "$ref": "#/definitions/AggregatedMeasurements" } },
                "hour": { "type": "object", "additionalProperties": { "$ref": "#/definitions/AggregatedMeasurements" } }
            }
        },
        "AggregatedMeasurements": {
            "type": "object",
            "properties": {
                "min": { "format": "double", "type": "number" },
                "max": { "format": "double", "type": "number" },
                "avg": { "format": "double", "type": "number" },
                "values": { "type": "object", "additionalProperties": { "$ref": "#/definitions/Volume" } }
            }
        },
        "Volume": {
            "type": "object",
            "properties": {
                "value": { "format": "double", "type": "number" },
                "status": { "enum": ["OK", "Est"], "type": "string" },
                "resolution": { "enum": ["Quarter", "Hour", "Month"], "type": "string" }
            }
        }
    }
}

AggregatedMeasurements.ts

// tslint:disable
/**
 * map-spec 1.0
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0
 * 
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

import { exists, mapValues } from '../runtime';
import {
    AggregatedMeasurements,
    AggregatedMeasurementsFromJSON,
    AggregatedMeasurementsToJSON,
} from './';

/**
 * 
 * @export
 * @interface Measurements
 */
export interface Measurements {
    /**
     * 
     * @type {string}
     * @memberof Measurements
     */
    meteringPointId?: string;
    /**
     * 
     * @type {AggregatedMeasurements}
     * @memberof Measurements
     */
    year?: AggregatedMeasurements;
    /**
     * 
     * @type {{ [key: string]: AggregatedMeasurements; }}
     * @memberof Measurements
     */
    month?: { [key: string]: AggregatedMeasurements; };
    /**
     * 
     * @type {{ [key: string]: AggregatedMeasurements; }}
     * @memberof Measurements
     */
    day?: { [key: string]: AggregatedMeasurements; };
    /**
     * 
     * @type {{ [key: string]: AggregatedMeasurements; }}
     * @memberof Measurements
     */
    hour?: { [key: string]: AggregatedMeasurements; };
}

export function MeasurementsFromJSON(json: any): Measurements {
    return {
        'meteringPointId': !exists(json, 'meteringPointId') ? undefined : json['meteringPointId'],
        'year': !exists(json, 'year') ? undefined : AggregatedMeasurementsFromJSON(json['year']),
        'month': !exists(json, 'month') ? undefined : mapValues(json['month'], AggregatedMeasurementsFromJSON),
        'day': !exists(json, 'day') ? undefined : mapValues(json['day'], AggregatedMeasurementsFromJSON),
        'hour': !exists(json, 'hour') ? undefined : mapValues(json['hour'], AggregatedMeasurementsFromJSON),
    };
}

export function MeasurementsToJSON(value?: Measurements): any {
    if (value === undefined) {
        return undefined;
    }
    return {
        'meteringPointId': value.meteringPointId,
        'year': AggregatedMeasurementsToJSON(value.year),
        'month': value.month === undefined ? undefined : mapValues(value.month, AggregatedMeasurementsToJSON),
        'day': value.day === undefined ? undefined : mapValues(value.day, AggregatedMeasurementsToJSON),
        'hour': value.hour === undefined ? undefined : mapValues(value.hour, AggregatedMeasurementsToJSON),
    };
}

Copy link
Contributor

@TiFu TiFu left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Looks good to me.

@wing328 wing328 added this to the 4.0.1 milestone May 20, 2019
@wing328 wing328 merged commit 87c9de2 into OpenAPITools:master May 20, 2019
@wing328 wing328 changed the title Feature/typescript fetch/map templating [TS][Fetch] Add support for openapi maps/dictionaries to be generated as typescript map May 20, 2019
@wing328
Copy link
Member

wing328 commented Jun 3, 2019

@ehvattum thanks again for your contribution, which has been included in the v4.0.1 release (https://twitter.com/oas_generator/status/1135534738658062336)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants