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

Fix Petstore example for Elm #96

Merged
merged 1 commit into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ public ElmClientCodegen() {

defaultIncludes = new HashSet<>(
Arrays.asList(
"List")
"Order",
"Never",
"List",
"Maybe",
"Result",
"Program",
"Cmd",
"Sub")
);

languageSpecificPrimitives = new HashSet<>(
Expand Down Expand Up @@ -158,7 +165,8 @@ public String toApiName(String name) {

@Override
public String toModelName(String name) {
return camelize(name);
final String modelName = camelize(name);
return defaultIncludes.contains(modelName) ? modelName + "_" : modelName;
}

@Override
Expand Down Expand Up @@ -262,10 +270,10 @@ public int compare(CodegenModel cm1, CodegenModel cm2) {
for (Map<String, Object> mo : models) {
CodegenModel cm = (CodegenModel) mo.get("model");
if (cm.isEnum) {
this.addEncoderAndDecoder(cm.vendorExtensions, cm.classname, false);
this.addEncoderAndDecoder(cm.vendorExtensions, cm.classname, false, false);
cm.vendorExtensions.put(X_UNION_TYPE, cm.classname);
} else if (cm.isAlias) {
this.addEncoderAndDecoder(cm.vendorExtensions, cm.dataType, true);
this.addEncoderAndDecoder(cm.vendorExtensions, cm.dataType, false, true);
}

List<ElmImport> elmImports = new ArrayList<>();
Expand Down Expand Up @@ -380,7 +388,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> operations)
}
op.path = ("\"" + path + "\"").replaceAll(" \\+\\+ \"\"", "");

if (op.bodyParam != null) {
if (op.bodyParam != null && !op.bodyParam.isPrimitiveType && !op.bodyParam.isMapContainer) {
final String encoder = (String) op.bodyParam.vendorExtensions.get(X_ENCODER);
if (encoder != null) {
if (!dependencies.containsKey(op.bodyParam.dataType)) {
Expand All @@ -390,6 +398,9 @@ public Map<String, Object> postProcessOperations(Map<String, Object> operations)
}
}
for (CodegenResponse resp : op.responses) {
if (resp.primitiveType || resp.isMapContainer) {
continue;
}
final String decoder = (String) resp.vendorExtensions.get(X_DECODER);
if (decoder != null) {
if (!dependencies.containsKey(resp.dataType)) {
Expand Down Expand Up @@ -486,7 +497,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
final CodegenProperty property = super.fromProperty(name, p);

final String dataType = property.isEnum ? property.baseName : property.datatype;
addEncoderAndDecoder(property.vendorExtensions, dataType, property.isPrimitiveType && !property.isEnum);
addEncoderAndDecoder(property.vendorExtensions, dataType, property.isMapContainer, property.isPrimitiveType && !property.isEnum);
if (property.isEnum) {
property.vendorExtensions.put(X_UNION_TYPE, property.datatypeWithEnum);
}
Expand All @@ -498,17 +509,24 @@ public CodegenProperty fromProperty(String name, Schema p) {
public CodegenResponse fromResponse(String responseCode, ApiResponse resp) {
final CodegenResponse response = super.fromResponse(responseCode, resp);
if (response.dataType != null) {
addEncoderAndDecoder(response.vendorExtensions, response.dataType, response.primitiveType);
addEncoderAndDecoder(response.vendorExtensions, response.dataType, response.isMapContainer, response.primitiveType);
}
return response;
}

@Override
public void postProcessParameter(CodegenParameter parameter) {
addEncoderAndDecoder(parameter.vendorExtensions, parameter.dataType, parameter.isPrimitiveType);
addEncoderAndDecoder(parameter.vendorExtensions, parameter.dataType, parameter.isMapContainer, parameter.isPrimitiveType);
}

private boolean isPrimitiveDataType(String dataType) {
return languageSpecificPrimitives.contains(dataType);
}

private void addEncoderAndDecoder(Map<String, Object> vendorExtensions, String dataType, Boolean isPrimitiveType) {
private void addEncoderAndDecoder(Map<String, Object> vendorExtensions, String dataType, Boolean isMapContainer, Boolean isPrimitiveType) {
if (isMapContainer) {
isPrimitiveType = isPrimitiveDataType(dataType);
}
final String baseName = camelize(dataType, true);
String encoderName;
String decoderName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode


type alias Byte = String
type alias Byte =
String


byteDecoder : Decoder Byte
Expand Down
7 changes: 4 additions & 3 deletions modules/openapi-generator/src/main/resources/elm/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module Request.{{classname}} exposing ({{#operations}}{{#operation}}{{^-first}}, {{/-first}}{{operationId}}{{/operation}}{{/operations}})

{{>imports}}import Http
{{>imports}}import Dict
import Http
import Json.Decode as Decode


Expand All @@ -16,13 +17,13 @@ basePath =
{-
{{notes}}
-}
{{operationId}} : {{#pathParams}}{{dataType}} -> {{/pathParams}}{{#bodyParam}}{{dataType}} -> {{/bodyParam}}Http.Request {{#responses}}{{#-first}}{{^dataType}}(){{/dataType}}{{#isListContainer}}(List {{/isListContainer}}{{dataType}}{{#isListContainer}}){{/isListContainer}}{{/-first}}{{/responses}}
{{operationId}} : {{#pathParams}}{{dataType}} -> {{/pathParams}}{{#bodyParam}}{{dataType}} -> {{/bodyParam}}Http.Request {{#responses}}{{#-first}}{{^dataType}}(){{/dataType}}{{#isMapContainer}}(Dict.Dict String {{/isMapContainer}}{{#isListContainer}}(List {{/isListContainer}}{{dataType}}{{#isListContainer}}){{/isListContainer}}{{#isMapContainer}}){{/isMapContainer}}{{/-first}}{{/responses}}
{{operationId}} {{#pathParams}}{{paramName}} {{/pathParams}}{{#bodyParam}}model {{/bodyParam}}=
{ method = "{{httpMethod}}"
, url = basePath ++ {{{path}}}
, headers = []
, body = {{#bodyParam}}Http.jsonBody <| {{vendorExtensions.x-encoder}} model{{/bodyParam}}{{^bodyParam}}Http.emptyBody{{/bodyParam}}
, expect = {{#responses}}{{#-first}}{{^dataType}}Http.expectStringResponse (\_ -> Ok ()){{/dataType}}{{#dataType}}Http.expectJson {{#isListContainer}}(Decode.list {{/isListContainer}}{{#vendorExtensions}}{{x-decoder}}{{/vendorExtensions}}{{#isListContainer}}){{/isListContainer}}{{/dataType}}{{/-first}}{{/responses}}
, expect = {{#responses}}{{#-first}}{{^dataType}}Http.expectStringResponse (\_ -> Ok ()){{/dataType}}{{#dataType}}Http.expectJson {{#isMapContainer}}(Decode.dict {{/isMapContainer}}{{#isListContainer}}(Decode.list {{/isListContainer}}{{#vendorExtensions}}{{x-decoder}}{{/vendorExtensions}}{{#isListContainer}}){{/isListContainer}}{{#isMapContainer}}){{/isMapContainer}}{{/dataType}}{{/-first}}{{/responses}}
, timeout = Just 30000
, withCredentials = False
}
Expand Down
11 changes: 0 additions & 11 deletions samples/client/petstore/elm/licenseInfo.elm

This file was deleted.

3 changes: 2 additions & 1 deletion samples/client/petstore/elm/src/Byte.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode


type alias Byte = String
type alias Byte =
String


byteDecoder : Decoder Byte
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-}


module Data.Order exposing (Order, Status(..), orderDecoder, orderEncoder)
module Data.Order_ exposing (Order_, Status(..), orderDecoder, orderEncoder)

import DateTime exposing (DateTime, dateTimeDecoder, dateTimeEncoder)
import Json.Decode as Decode exposing (Decoder)
Expand All @@ -25,7 +25,7 @@ import Maybe exposing (map, withDefault)
-}


type alias Order =
type alias Order_ =
{ id : Maybe Int
, petId : Maybe Int
, quantity : Maybe Int
Expand All @@ -42,9 +42,9 @@ type Status



orderDecoder : Decoder Order
orderDecoder : Decoder Order_
orderDecoder =
decode Order
decode Order_
|> optional "id" (Decode.nullable Decode.int) Nothing
|> optional "petId" (Decode.nullable Decode.int) Nothing
|> optional "quantity" (Decode.nullable Decode.int) Nothing
Expand All @@ -54,7 +54,7 @@ orderDecoder =



orderEncoder : Order -> Encode.Value
orderEncoder : Order_ -> Encode.Value
orderEncoder model =
Encode.object
[ ( "id", withDefault Encode.null (map Encode.int model.id) )
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/elm/src/Request/Pet.elm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Request.Pet exposing (addPet, deletePet, findPetsByStatus, findPetsByTags

import Data.Pet exposing (Pet, petDecoder, petEncoder)
import Data.ApiResponse exposing (ApiResponse, apiResponseDecoder)
import Dict
import Http
import Json.Decode as Decode

Expand Down
12 changes: 6 additions & 6 deletions samples/client/petstore/elm/src/Request/Store.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

module Request.Store exposing (deleteOrder, getInventory, getOrderById, placeOrder)

import Data.Order exposing (Order, orderDecoder, orderEncoder)
import Data.Int exposing (Int, intDecoder)
import Data.Order_ exposing (Order_, orderDecoder, orderEncoder)
import Dict
import Http
import Json.Decode as Decode

Expand Down Expand Up @@ -43,13 +43,13 @@ deleteOrder orderId =
{-
Returns a map of status codes to quantities
-}
getInventory : Http.Request Int
getInventory : Http.Request (Dict.Dict String Int)
getInventory =
{ method = "GET"
, url = basePath ++ "/store/inventory"
, headers = []
, body = Http.emptyBody
, expect = Http.expectJson intDecoder
, expect = Http.expectJson (Decode.dict Decode.int)
, timeout = Just 30000
, withCredentials = False
}
Expand All @@ -59,7 +59,7 @@ getInventory =
{-
For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generated exceptions
-}
getOrderById : Int -> Http.Request Order
getOrderById : Int -> Http.Request Order_
getOrderById orderId =
{ method = "GET"
, url = basePath ++ "/store/order/" ++ toString orderId
Expand All @@ -75,7 +75,7 @@ getOrderById orderId =
{-

-}
placeOrder : Order -> Http.Request Order
placeOrder : Order_ -> Http.Request Order_
placeOrder model =
{ method = "POST"
, url = basePath ++ "/store/order"
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/elm/src/Request/User.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
module Request.User exposing (createUser, createUsersWithArrayInput, createUsersWithListInput, deleteUser, getUserByName, loginUser, logoutUser, updateUser)

import Data.User exposing (User, userDecoder, userEncoder)
import Data.String exposing (Decode.string, String)
import Dict
import Http
import Json.Decode as Decode

Expand Down