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

File included an unresolvable reference #84

Open
nomad-software opened this issue Jul 4, 2022 · 6 comments
Open

File included an unresolvable reference #84

nomad-software opened this issue Jul 4, 2022 · 6 comments

Comments

@nomad-software
Copy link

nomad-software commented Jul 4, 2022

I'm trying to convert the following Json file: https://github.com/oracle/hospitality-api-docs/blob/main/rest-api-specs/property/lov.json

Using this command:

gnostic --grpc-out=api ../hospitality-api-docs/rest-api-specs/property/lov.json

and i'm getting this error:

Errors reading ../hospitality-api-docs/rest-api-specs/property/lov.json
Plugin error: [file "lov.proto" included an unresolvable reference to "GetChannelParametersLOVResponses"]

If I then try the --resolve-refs option like this:

gnostic --grpc-out=api --resolve-refs ../hospitality-api-docs/rest-api-specs/property/lov.json

I get another slightly different error:

Errors reading ../hospitality-api-docs/rest-api-specs/property/lov.json
Plugin error: [file "lov.proto" included an unresolvable reference to "lov.GetChannelParametersLOVOK"]

There's another file that has problems too:
https://github.com/oracle/hospitality-api-docs/blob/main/rest-api-specs/property/fofcfg.json

but the rest convert correctly. Any ideas on what is happening here as the Json files seem to be valid?

Thanks.

@nielthiart
Copy link

@nomad-software Were you able to resolve this issue for your use case, or do you know what caused this error?

@nomad-software
Copy link
Author

Nope, it was never resolved. I had to remove the offending parts by had from the Json file before conversion. Luckily I didn't need those particular endpoints.

@nielthiart
Copy link

Thanks! I found that if I use a recent version of gnostic to create a .pb binary, I can get the .proto output by running gnostic-grpc -input petstore.pb, for example.

@nomad-software
Copy link
Author

Thanks for the suggestion but that doesn't work on the original file mentioned.

@iliakonnov
Copy link

iliakonnov commented Apr 25, 2024

I believe to have found source of this issue.

Definition like this:

"DataResult": {
  "properties": {
    "object": {
      "additionalProperties": {
        "$ref": "#/definitions/DataResult"
      },
      "description": "Object value",
      "example": "",
      "type": "object"
    },
    "string": {
      "type": "string"
    },
  },
  "type": "object"
}

Is represented by gnostic-grpc like that:

message Object {
    map<string, DataResult> additional_properties = 1;
}

message DataResult {
    string string = 1;
    Object object = 2;
}

To do that, gnostic internally generates a type to store object field data.

https://github.com/google/gnostic/blob/ad271d568b713ad381ad6751cd8b950eade78d98/surface/model_openapiv2.go#L388-L393

Note that it uses namedSchema.Name as a name for a new type. In this case it happens to be an object (look at field name).

Later, gnostic-rpc begins processing a field with name object and type object:

for _, f := range t.Fields {
f.FieldName = protoFieldName(f.Name, f.Type)
f.NativeType = findNativeType(f.Type, f.Format)

Since type object is reserved it is not resolved there.

case "object":
return "message"

But later on NativeType of a field is used as a name of a referenced proto message (resulting in api_package.object):

typeName := ""
if fieldDescriptorType == dpb.FieldDescriptorProto_TYPE_MESSAGE {
typeName = packageName + "." + field.NativeType

Obviously, this results in an invalid proto file since it does not contains a definition for a api_package.object message.

I've fixed the issue for me by changing the first snippet to prefix generated type name with it's containing type name.

	fieldTypeName := fmt.Sprintf("%s_%s", name, namedSchema.Name)
	fieldInfo := b.buildFromSchemaOrReference(fieldTypeName, namedSchema.Value)
	makeFieldAndAppendToType(fieldInfo, schemaType, namedSchema.Name)

@wkrige
Copy link

wkrige commented Aug 22, 2024

Thanks! I found that if I use a recent version of gnostic to create a .pb binary, I can get the .proto output by running gnostic-grpc -input petstore.pb, for example.

Same issue. This path worked for me. Thanks for the help! Strange that the invocation of gnostic-grpc as plugin gives different results.

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

No branches or pull requests

4 participants