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 NPE in Elm path parameter #4116

Merged
merged 4 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion bin/utils/ensure-up-to-date
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ declare -a scripts=(
"./bin/dart-petstore.sh"
"./bin/dart2-petstore.sh"
"./bin/java-play-framework-petstore-server-all.sh"
#"./bin/elm-petstore-all.sh"
"./bin/elm-petstore-all.sh"
"./bin/meta-codegen.sh"
# OTHERS
"./bin/utils/export_docs_generators.sh"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ public int compare(CodegenModel cm1, CodegenModel cm2) {

final String propertyName = cm.discriminator.getPropertyName();
final List<CodegenProperty> allVars = child.allVars.stream()
.filter(var -> !var.baseName.equals(propertyName))
.collect(Collectors.toList());
.filter(var -> !var.baseName.equals(propertyName))
.collect(Collectors.toList());
child.allVars.clear();
child.allVars.addAll(allVars);

Expand Down Expand Up @@ -415,17 +415,17 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
}

private static boolean anyOperationParam(final List<CodegenOperation> operations, final Predicate<CodegenParameter> predicate) {
return operations.stream()
.flatMap(operation -> Stream.of(
operation.bodyParams.stream(),
operation.queryParams.stream(),
operation.pathParams.stream(),
operation.headerParams.stream()
))
.flatMap(a -> a)
.filter(predicate)
.findAny()
.isPresent();
return operations.stream()
.flatMap(operation -> Stream.of(
operation.bodyParams.stream(),
operation.queryParams.stream(),
operation.pathParams.stream(),
operation.headerParams.stream()
))
.flatMap(a -> a)
.filter(predicate)
.findAny()
.isPresent();
}

@Override
Expand All @@ -437,23 +437,23 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
final Set<String> dependencies = new HashSet<>();

for (CodegenOperation op : ops) {
if (ElmVersion.ELM_018.equals(elmVersion)) {
if (ElmVersion.ELM_018.equals(elmVersion)) { // elm 0.18
String path = op.path;
for (CodegenParameter param : op.pathParams) {
final String var = paramToString("params", param, false, null);
path = path.replace("{" + param.paramName + "}", "\" ++ " + var + " ++ \"");
wing328 marked this conversation as resolved.
Show resolved Hide resolved
}
op.path = ("\"" + path + "\"").replaceAll(" \\+\\+ \"\"", "");
} else {
} else { // elm 0.19 or later
final List<Object> pathParams = Arrays.asList(op.path.substring(1).split("/")).stream()
.map(str -> {
if (str.startsWith("{") && str.endsWith("}")) {
return op.pathParams.stream().filter(p -> str.equals("{" + p.paramName + "}")).findFirst().orElse(null);
} else {
return "\"" + str + "\"";
}
})
.collect(Collectors.toList());
.map(str -> {
if (str.startsWith("{") && str.endsWith("}")) {
return op.pathParams.stream().filter(p -> str.equals("{" + p.baseName + "}")).findFirst().orElse(null);
} else {
return "\"" + str + "\"";
}
})
.collect(Collectors.toList());
op.vendorExtensions.put("pathParams", pathParams);
}

Expand Down Expand Up @@ -737,10 +737,10 @@ private void addEncoderAndDecoder(final Map<String, Object> vendorExtensions, fi
}

private enum DataTypeExposure {
EXPOSED,
INTERNAL,
EXTERNAL,
PRIMITIVE
EXPOSED,
INTERNAL,
EXTERNAL,
PRIMITIVE
}

private static class ElmImport {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.3-SNAPSHOT
4.2.0-SNAPSHOT
13 changes: 3 additions & 10 deletions samples/client/petstore/elm-0.18/src/Data/ApiResponse.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import Json.Encode as Encode
{-| Describes the result of uploading an image resource
-}
type alias ApiResponse =
{ code : Maybe (Int)
, type_ : Maybe (String)
, message : Maybe (String)
{ code : Maybe Int
, type_ : Maybe String
, message : Maybe String
}


Expand All @@ -35,22 +35,15 @@ decoder =
|> optional "message" (Decode.nullable Decode.string) Nothing



encode : ApiResponse -> Encode.Value
encode model =
Encode.object
[ ( "code", Maybe.withDefault Encode.null (Maybe.map Encode.int model.code) )
, ( "type", Maybe.withDefault Encode.null (Maybe.map Encode.string model.type_) )
, ( "message", Maybe.withDefault Encode.null (Maybe.map Encode.string model.message) )

]



toString : ApiResponse -> String
toString =
Encode.encode 0 << encode




11 changes: 2 additions & 9 deletions samples/client/petstore/elm-0.18/src/Data/Category.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import Json.Encode as Encode
{-| A category for a pet
-}
type alias Category =
{ id : Maybe (Int)
, name : Maybe (String)
{ id : Maybe Int
, name : Maybe String
}


Expand All @@ -33,21 +33,14 @@ decoder =
|> optional "name" (Decode.nullable Decode.string) Nothing



encode : Category -> Encode.Value
encode model =
Encode.object
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "name", Maybe.withDefault Encode.null (Maybe.map Encode.string model.name) )

]



toString : Category -> String
toString =
Encode.encode 0 << encode




23 changes: 6 additions & 17 deletions samples/client/petstore/elm-0.18/src/Data/Order_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import Json.Encode as Encode
{-| An order for a pets from the pet store
-}
type alias Order_ =
{ id : Maybe (Int)
, petId : Maybe (Int)
, quantity : Maybe (Int)
, shipDate : Maybe (DateTime)
, status : Maybe (Status)
, complete : Maybe (Bool)
{ id : Maybe Int
, petId : Maybe Int
, quantity : Maybe Int
, shipDate : Maybe DateTime
, status : Maybe Status
, complete : Maybe Bool
}


Expand All @@ -37,7 +37,6 @@ type Status
| Delivered



decoder : Decoder Order_
decoder =
decode Order_
Expand All @@ -49,7 +48,6 @@ decoder =
|> optional "complete" (Decode.nullable Decode.bool) (Just False)



encode : Order_ -> Encode.Value
encode model =
Encode.object
Expand All @@ -59,18 +57,14 @@ encode model =
, ( "shipDate", Maybe.withDefault Encode.null (Maybe.map DateTime.encode model.shipDate) )
, ( "status", Maybe.withDefault Encode.null (Maybe.map encodeStatus model.status) )
, ( "complete", Maybe.withDefault Encode.null (Maybe.map Encode.bool model.complete) )

]



toString : Order_ -> String
toString =
Encode.encode 0 << encode




statusDecoder : Decoder Status
statusDecoder =
Decode.string
Expand All @@ -91,7 +85,6 @@ statusDecoder =
)



encodeStatus : Status -> Encode.Value
encodeStatus model =
case model of
Expand All @@ -103,7 +96,3 @@ encodeStatus model =

Delivered ->
Encode.string "delivered"




21 changes: 5 additions & 16 deletions samples/client/petstore/elm-0.18/src/Data/Pet.elm
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import Json.Encode as Encode
{-| A pet for sale in the pet store
-}
type alias Pet =
{ id : Maybe (Int)
, category : Maybe (Category)
{ id : Maybe Int
, category : Maybe Category
, name : String
, photoUrls : (List String)
, tags : Maybe ((List Tag))
, status : Maybe (Status)
, photoUrls : List String
, tags : Maybe (List Tag)
, status : Maybe Status
}


Expand All @@ -38,7 +38,6 @@ type Status
| Sold



decoder : Decoder Pet
decoder =
decode Pet
Expand All @@ -50,7 +49,6 @@ decoder =
|> optional "status" (Decode.nullable statusDecoder) Nothing



encode : Pet -> Encode.Value
encode model =
Encode.object
Expand All @@ -60,18 +58,14 @@ encode model =
, ( "photoUrls", (Encode.list << List.map Encode.string) model.photoUrls )
, ( "tags", Maybe.withDefault Encode.null (Maybe.map (Encode.list << List.map Tag.encode) model.tags) )
, ( "status", Maybe.withDefault Encode.null (Maybe.map encodeStatus model.status) )

]



toString : Pet -> String
toString =
Encode.encode 0 << encode




statusDecoder : Decoder Status
statusDecoder =
Decode.string
Expand All @@ -92,7 +86,6 @@ statusDecoder =
)



encodeStatus : Status -> Encode.Value
encodeStatus model =
case model of
Expand All @@ -104,7 +97,3 @@ encodeStatus model =

Sold ->
Encode.string "sold"




11 changes: 2 additions & 9 deletions samples/client/petstore/elm-0.18/src/Data/Tag.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import Json.Encode as Encode
{-| A tag for a pet
-}
type alias Tag =
{ id : Maybe (Int)
, name : Maybe (String)
{ id : Maybe Int
, name : Maybe String
}


Expand All @@ -33,21 +33,14 @@ decoder =
|> optional "name" (Decode.nullable Decode.string) Nothing



encode : Tag -> Encode.Value
encode model =
Encode.object
[ ( "id", Maybe.withDefault Encode.null (Maybe.map Encode.int model.id) )
, ( "name", Maybe.withDefault Encode.null (Maybe.map Encode.string model.name) )

]



toString : Tag -> String
toString =
Encode.encode 0 << encode




23 changes: 8 additions & 15 deletions samples/client/petstore/elm-0.18/src/Data/User.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import Json.Encode as Encode
{-| A User who is purchasing from the pet store
-}
type alias User =
{ id : Maybe (Int)
, username : Maybe (String)
, firstName : Maybe (String)
, lastName : Maybe (String)
, email : Maybe (String)
, password : Maybe (String)
, phone : Maybe (String)
, userStatus : Maybe (Int)
{ id : Maybe Int
, username : Maybe String
, firstName : Maybe String
, lastName : Maybe String
, email : Maybe String
, password : Maybe String
, phone : Maybe String
, userStatus : Maybe Int
}


Expand All @@ -45,7 +45,6 @@ decoder =
|> optional "userStatus" (Decode.nullable Decode.int) Nothing



encode : User -> Encode.Value
encode model =
Encode.object
Expand All @@ -57,15 +56,9 @@ encode model =
, ( "password", Maybe.withDefault Encode.null (Maybe.map Encode.string model.password) )
, ( "phone", Maybe.withDefault Encode.null (Maybe.map Encode.string model.phone) )
, ( "userStatus", Maybe.withDefault Encode.null (Maybe.map Encode.int model.userStatus) )

]



toString : User -> String
toString =
Encode.encode 0 << encode




2 changes: 1 addition & 1 deletion samples/client/petstore/elm-0.18/src/DateTime.elm
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ decodeIsoString str =

toString : DateTime -> String
toString =
toIsoString
toIsoString
Loading